【发布时间】:2014-09-22 02:06:13
【问题描述】:
我正在使用 Python matplotlib,这是我的代码:
plt.title('Temperature \n Humidity')
我怎样才能只增加温度的字体大小而不是增加温度和湿度?
这不起作用:
plt.title('Temperature \n Humidity', fontsize=100)
【问题讨论】:
标签: python matplotlib graph
我正在使用 Python matplotlib,这是我的代码:
plt.title('Temperature \n Humidity')
我怎样才能只增加温度的字体大小而不是增加温度和湿度?
这不起作用:
plt.title('Temperature \n Humidity', fontsize=100)
【问题讨论】:
标签: python matplotlib graph
fontsize 可以在字典 fontdict 中分配,它提供了额外的参数 fontweight、verticalalignment、horizontalalignment
下面的 sn-p 应该可以工作
plt.title('Temperature \n Humidity', fontdict = {'fontsize' : 100})
【讨论】:
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 tk 或Follow this link 安装tk
【讨论】:
figtext 可能无法实现;除了相应地调整字体大小和位置参数。您应该发布更多带有一些示例数据的代码。现在我发布了不同的代码;但是如果你在一个项目中/已经绘制了数据,那将是有问题的/烦人的。查看更新的代码。
这在 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) 之前调用,否则无论出于何种原因,标题大小偶尔会保持不变。
【讨论】:
假设您正在使用 matplotlib 来渲染一些绘图。
您可能想结帐Text rendering With LaTeX — Matplotlib
以下是您的案例的一些代码行
plt.rc('text', usetex=True)
plt.title(r"\begin{center} {\Large Temperature} \par {\large Humidity} \end{center}")
希望对您有所帮助。
【讨论】:
只需执行以下操作:
ax.set_title('This is the title',fontsize=20)
【讨论】: