我想更改图例中各个元素的字体大小。我知道如何更改整个图例的字体属性,但我无法找到单独更改它们的方法。
例如:在下面的示例中,我绘制了两条线,并且我希望 line1 的标签具有比 line 2 更大的字体大小
import numpy as np
from matplotlib import pyplot as plt
def line (m , c):
return m*x + c
x = np.arange(0, 10, .25)
plt.plot(x, line(1, 0), label = "Line 1")
plt.plot(x, line(1,1), label = "Line 2")
plt.legend(prop={'family': 'Georgia', 'size': 15})
plt.show()
使用prop={'family': 'Georgia', 'size': 15}
我可以同时修改两个标签的字体大小,但是有没有办法控制图例中各个标签的字体属性?
这里有一些很好的答案:一个是设置字体属性,另一个是使用 Latexh 表示法进行标签设置。我会用字体属性自定义的方法来回答。
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.font_manager import FontProperties
def line (m , c):
return m*x + c
x = np.arange(0, 10, .25)
plt.plot(x, line(1, 0), label = "Line 1")
plt.plot(x, line(1,1), label = "Line 2")
leg = plt.legend(prop={'family': 'Georgia', 'size': 15})
label1, label2 = leg.get_texts()
label1._fontproperties = label2._fontproperties.copy()
label1.set_size('medium')
plt.show()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句