更改 matplotlib 图例中单个标签的字体大小

时间:2021-04-26 01:46:00

标签: python matplotlib legend font-size

我想更改图例中各个元素的字体大小。我知道如何更改整个图例的字体属性,但我无法找到单独更改它们的方法。

例如:在下面的示例中,我绘制了两条线,并且我希望第 1 行的标签具有比第 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} 我可以同时修改两个标签的字体大小,但有没有办法控制图例中各个标签的字体属性?

感谢任何和所有帮助表示赞赏。 enter image description here

1 个答案:

答案 0 :(得分:1)

这里有一些great answers:一个是设置字体属性,另一个是使用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()

enter image description here

相关问题