【问题标题】:How to increase plt.title font size?如何增加 plt.title 字体大小?
【发布时间】:2014-09-22 02:06:13
【问题描述】:

我正在使用 Python matplotlib,这是我的代码:

  plt.title('Temperature \n Humidity')

我怎样才能只增加温度的字体大小而不是增加温度和湿度?

这不起作用:

 plt.title('Temperature \n Humidity', fontsize=100)

【问题讨论】:

    标签: python matplotlib graph


    【解决方案1】:

    fontsize 可以在字典 fontdict 中分配,它提供了额外的参数 fontweight、verticalalignment、horizo​​ntalalignment

    下面的 sn-p 应该可以工作

    plt.title('Temperature \n Humidity', fontdict = {'fontsize' : 100})

    【讨论】:

    【解决方案2】:
    import matplotlib.pyplot as plt
    plt.figtext(.5,.9,'Temperature', fontsize=100, ha='center')
    plt.figtext(.5,.8,'Humidity',fontsize=30,ha='center')
    plt.show()
    

    可能你想要这个。您可以轻松调整两者的fontsize,并通过更改前两个figtext 位置参数来调整那里的位置。 哈是horizontal alignment

    或者,

    import matplotlib.pyplot as plt
    
    fig = plt.figure() # Creates a new figure
    fig.suptitle('Temperature', fontsize=50) # Add the text/suptitle to figure
    
    ax = fig.add_subplot(111) # add a subplot to the new figure, 111 means "1x1 grid, first subplot"
    fig.subplots_adjust(top=0.80) # adjust the placing of subplot, adjust top, bottom, left and right spacing  
    ax.set_title('Humidity',fontsize= 30) # title of plot
    
    ax.set_xlabel('xlabel',fontsize = 20) #xlabel
    ax.set_ylabel('ylabel', fontsize = 20)#ylabel
    
    x = [0,1,2,5,6,7,4,4,7,8]
    y = [2,4,6,4,6,7,5,4,5,7]
    
    ax.plot(x,y,'-o') #plotting the data with marker '-o'
    ax.axis([0, 10, 0, 10]) #specifying plot axes lengths
    plt.show()
    

    替代代码的输出:

    PS:如果此代码给出像ImportError: libtk8.6.so: cannot open shared object file esp 这样的错误。在Arch like systems。在这种情况下,使用sudo pacman -S tkFollow this link 安装tk

    【讨论】:

    • 嗨 Meher,0.5,.9 是什么?
    • 我已经指定了。这些是标题的位置参数。你也可以说它是填充。一个用于 x 轴,一个用于 y 轴。只要试着操纵它们,你就会明白;例如,文本“湿度”就在“温度”下方,所以我通过将 y 轴位置值设为 0.8 将其放置在下方,其中 x 轴位置参数与“温度”的参数相同,即.5 .
    • 嗨 Meher,我怎样才能让 figtext 显示在图形顶部而不是图形上
    • 很抱歉,figtext 可能无法实现;除了相应地调整字体大小和位置参数。您应该发布更多带有一些示例数据的代码。现在我发布了不同的代码;但是如果你在一个项目中/已经绘制了数据,那将是有问题的/烦人的。查看更新的代码。
    • 嗨 Meher,我收到一个错误,提示 ax 未定义
    【解决方案3】:

    这在 Matplotlib 的最新版本(当前为 2.0.2)中主要为我工作。它有助于生成演示图形:

    def plt_resize_text(labelsize, titlesize):
        ax = plt.subplot()
        for ticklabel in (ax.get_xticklabels()):
            ticklabel.set_fontsize(labelsize)
        for ticklabel in (ax.get_yticklabels()):
            ticklabel.set_fontsize(labelsize)
        ax.xaxis.get_label().set_fontsize(labelsize)
        ax.yaxis.get_label().set_fontsize(labelsize)
        ax.title.set_fontsize(titlesize)
    

    奇怪的for循环结构似乎是调整每个 tic标签的大小所必需的。 此外,上述函数应在调用plt.show(block=True) 之前调用,否则无论出于何种原因,标题大小偶尔会保持不变。

    【讨论】:

      【解决方案4】:

      假设您正在使用 matplotlib 来渲染一些绘图。

      您可能想结帐Text rendering With LaTeX — Matplotlib

      以下是您的案例的一些代码行

      plt.rc('text', usetex=True)
      plt.title(r"\begin{center} {\Large Temperature} \par {\large Humidity} \end{center}")
      

      希望对您有所帮助。

      【讨论】:

        【解决方案5】:

        只需执行以下操作: ax.set_title('This is the title',fontsize=20)

        【讨论】:

          【解决方案6】:

          我不想完全进入这个图表 通用方法: 第一步 - 在主标题和图表之间拉开距离:

          from matplotlib import rcParams
          rcParams['axes.titlepad'] = 20 
          

          然后通过设置坐标插入字幕:

          ax.text(0.3, -0.56, 'subtitle',fontsize=12)
          

          【讨论】:

            猜你喜欢
            • 2013-04-03
            • 2017-10-07
            • 1970-01-01
            • 2012-11-07
            • 2018-07-16
            • 2015-02-01
            • 1970-01-01
            • 2014-12-15
            • 2015-12-05
            相关资源
            最近更新 更多