Python 如何增加 plt.title 字体大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25036699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to increase plt.title font size?
提问by
I'm using Pythons matplotliband this is my code:
我正在使用 Python matplotlib,这是我的代码:
plt.title('Temperature \n Humidity')
How can I just increase the font size of temperature instead of both the temperature & the humdity?
我怎样才能增加温度的字体大小而不是温度和湿度?
This does NOT work:
这不起作用:
plt.title('Temperature \n Humidity', fontsize=100)
采纳答案by Tanmaya Meher
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()
Probably you want this. You can easily tweak the fontsizeof both and adjust there placing by changing the first two figtextpositional parameters.
ha is for horizontal alignment
可能你想要这个。您可以fontsize通过更改前两个figtext位置参数轻松调整两者并调整其位置。ha 用于水平对齐
Alternatively,
或者,
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()
Output of alternative code:
替代代码的输出:


PS: if this code give error like ImportError: libtk8.6.so: cannot open shared object fileesp. in Arch like systems. In that case, install tkusing sudo pacman -S tkor Follow this link
PS:如果这段代码给出像ImportError: libtk8.6.so: cannot open shared object fileesp这样的错误。在Arch like systems。在这种情况下,请tk使用sudo pacman -S tk或按照此链接安装
回答by yipeipei
Assuming you are using matplotlib to render some plots.
假设您正在使用 matplotlib 来渲染一些图。
You might want to checkout Text rendering With LaTeX — Matplotlib
你可能想用 LaTeX签出文本渲染 — Matplotlib
Here are some lines of code for your case
以下是您的案例的一些代码行
plt.rc('text', usetex=True)
plt.title(r"\begin{center} {\Large Temperature} \par {\large Humidity} \end{center}")


Hope that helps.
希望有帮助。
回答by Patrick Pribyl
This has been mostly working for me across recent versions of Matplotlib (currently 2.0.2). It is helpful for generating presentation graphics:
在最近版本的 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)
The odd for-loop construction seems to be necessary to adjust the size of eachtic label.
Also, the above function should be called just before the call to plt.show(block=True), otherwise for whatever reason the title size occasionally remains unchanged.
奇怪的 for 循环结构似乎是调整每个tic 标签大小所必需的。此外,应在调用 之前调用上述函数plt.show(block=True),否则无论出于何种原因,标题大小偶尔会保持不变。
回答by Shyam A
fontsizecan be assigned inside dictionary fontdictwhich provides additional parameters fontweight, verticalalignment , horizontalalignment
fontsize可以在字典fontdict中分配,它提供了额外的参数 fontweight、verticalalignment、horizontalalignment
The below snippet should work
下面的片段应该工作
plt.title('Temperature \n Humidity', fontdict = {'fontsize' : 100})
plt.title('Temperature \n Humidity', fontdict = {'fontsize' : 100})
回答by Wojciech Moszczyński
I don't want to go into this chart exactly Universal method: step one - make a distance between the main title and the chart:
我不想完全进入这个图表 通用方法:第一步 - 在主标题和图表之间留出距离:
from matplotlib import rcParams
rcParams['axes.titlepad'] = 20
then insert subtitle by setting coordinates:
然后通过设置坐标插入字幕:
ax.text(0.3, -0.56, 'subtitle',fontsize=12)

