[英]How to increase plt.title font size?
我正在使用 Pythons matplotlib
,這是我的代碼:
plt.title('Temperature \n Humidity')
我怎樣才能增加溫度的字體大小而不是溫度和濕度?
這不起作用:
plt.title('Temperature \n Humidity', fontsize=100)
fontsize可以在字典fontdict中分配,它提供了額外的參數 fontweight、verticalalignment、horizontalalignment
下面的片段應該工作
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()
可能你想要這個。 您可以通過更改前兩個figtext
位置參數輕松調整兩者的fontsize
並調整其位置。 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()
替代代碼的輸出:
PS:如果此代碼給出類似ImportError: libtk8.6.so: cannot open shared object file
esp. 在Arch like systems
。 在這種情況下,請使用sudo pacman -S tk
或按照此鏈接安裝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)
之前調用上述函數,否則無論出於何種原因,標題大小偶爾會保持不變。
假設您正在使用 matplotlib 來渲染一些圖。
你可能想用 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.