如何在matplotlib中将图例标题格式化为等宽字体?
我想把图例的标题设置成等宽字体。我的代码是:
width = 8
print_slope = "{0:.5F}".format(slope)
print_intercept = "{0:.5F}".format(intercept)
print_r_squared = "{0:.5F}".format(r_value**2)
ax.legend(prop={'family': 'monospace'},title='Slope: '+str(print_slope.rjust(width))+'\nIntercept: '+str(print_intercept.rjust(width))+'\nR-squared: '+str(print_r_squared.rjust(width)))
但是我得到的结果是图例标题是可变宽度的,而系列的部分是等宽字体:
我该怎么做才能让标题也变成等宽字体呢?谢谢。
2 个回答
0
你可以使用关键字参数 title_fontproperties
来设置 legend()
的标题样式:
ax.legend(..., title_fontproperties={'family': 'monospace'})
4
我觉得你可以通过单独的命令来设置图例的标题。
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [3, 2, 1], 'k-', label='data')
leg = ax.legend(prop={'size': 20})
leg.set_title('Legend Title', prop={'size': 14, 'weight': 'heavy'})