【问题标题】:Multiple font sizes in plot title绘图标题中的多种字体大小
【发布时间】:2015-03-03 15:51:24
【问题描述】:

我正在使用 Matplotlib.pyplot 在 IPython IDE 中进行绘图,并添加了一个标题:

plt.title('Mean WRFv3.5 LHF\n(September 16 - October 30, 2012)', fontsize=40)

但是,我希望第一行的大小为 40,第二行的大小为 18。这在 matplotlib 中是否可行?我看到了 \tiny\Huge 的 LaTeX 使用,但想要更多控制权。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    试试:

    import matplotlib.pyplot as plt
    
    plt.rc('text', usetex=True)
    plt.title(r'\fontsize{30pt}{3em}\selectfont{}{Mean WRFv3.5 LHF\r}{\fontsize{18pt}{3em}\selectfont{}(September 16 - October 30, 2012)}')
    
    plt.show()
    

    \r 可能想成为您系统上的\n

    我用Joel's answer to address your question.

    【讨论】:

    • 在 Mac 中出现错误 RuntimeError: LaTeX was not able to process the following string: 'lp'。似乎是solution is to install texlive-latex-extra。我试过了,但没有用。
    • @JeeYem 好像是 Mac matplotlib + python 的问题。试试看这个帖子:github.com/dfm/daft/issues/65
    • ...selectfont{}(September 16... 似乎在(September... 之前错过了{
    • @MaosiChen 你说得对,大括号不对,但不是缺少大括号,而是多了一个大括号:r' 之后的大括号,或者更确切地说是那个大括号。我已经编辑并更正了原始答案的代码。
    【解决方案2】:

    不确定这是否是您想要的,但您可以添加 suptitletext 并设置不同的字体大小,如下所示:

    plt.title('Mean WRFv3.5 LHF\n', fontsize=40)
    plt.suptitle('(September 16 - October 30, 2012)\n', fontsize=18)
    plt.text(0.5, 1, 'the third line', fontsize=13, ha='center')
    

    希望这会有所帮助。

    【讨论】:

    • 在我的例子中,title 和 suptitle 位于同一行,即它们是重叠的。如何控制它们之间的相对位置?
    • 您的意思是要分开行吗?您可以尝试使用换行符,即“\n”
    • 准确地说,两行是不同的对象,但标题和副标题位于同一位置。但它有效。在字幕中使用“\n”,它可以定位在标题对象下。技术不错。
    • 还可以为title或者suptitle添加一个"y"参数,表示它的垂直位置,0 --> 1往下 --> top:plt.title(title1, fontsize=12, y=0.95)
    猜你喜欢
    • 2021-09-04
    • 1970-01-01
    • 2021-07-31
    • 2014-10-14
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多