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

2024-05-20 16:25:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用Python,这是我的代码:

  plt.title('Temperature \n Humidity')

我怎样才能增加温度的字号而不是同时增加温度和湿度?

这不起作用:

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

Tags: 代码titleplt温度temperature湿度humidityfontsize
3条回答
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位置参数来调整放置位置。 ha代表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()

替代代码输出:

enter image description here

注:如果这段代码给出类似ImportError: libtk8.6.so: cannot open shared object file的错误,特别是Arch like systems。在这种情况下,使用sudo pacman -S tkFollow this link安装tk

这在最近的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)之前调用,否则无论出于什么原因,标题大小偶尔都会保持不变。

fontsize可以在字典中指定,它提供了其他参数fontwweight、verticalalignment、horizontalignment

下面的代码片段应该可以工作

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

相关问题 更多 >