如何增加plt.title字体大小?

15 python graph matplotlib

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

  plt.title('Temperature \n Humidity')
Run Code Online (Sandbox Code Playgroud)

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

这不起作用:

 plt.title('Temperature \n Humidity', fontsize=100)
Run Code Online (Sandbox Code Playgroud)

Tan*_*her 20

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()
Run Code Online (Sandbox Code Playgroud)

可能你想要这个.您可以fontsize通过更改前两个figtext位置参数轻松调整两者并调整放置位置.ha用于水平对齐

或者,

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()
Run Code Online (Sandbox Code Playgroud)

替代代码的输出:

在此输入图像描述

PS:如果这段代码给出了像ImportError: libtk8.6.so: cannot open shared object fileesp 这样的错误.在Arch like systems.在这种情况下,请tk使用sudo pacman -S tk按照此链接进行安装


小智 8

字体大小可以在字典fontdict内部分配,字典提供额外的参数fontweight,verticalalignment,horizo​​ntalalignment

下面的代码段应该工作

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

  • 这应该是公认的答案。比接受的答案简单 20 倍。 (5认同)

Pat*_*byl 5

在最近版本的 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)
Run Code Online (Sandbox Code Playgroud)

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