简体   繁体   English

如何在matplotlib中将图例标题格式化为字体类型等宽字体?

[英]How to format legend title to font type monospace in matplotlib?

I want to format the legend title to be monospace. 我想将图例标题的格式设置为等宽。 I have: 我有:

    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)))

Yet the output I get gives me the legend title in variable width, with the series in monospace: 然而,我得到的输出为我提供了可变宽度的图例标题,以及等宽字体中的系列:

在此处输入图片说明

How do I get the title to be monospace also? 如何获得标题也是等宽字体? Thanks. 谢谢。

I think you should just set the legend's title with a separate call. 我认为您应该通过单独的调用来设置图例的标题。

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'})

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM