簡體   English   中英

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

[英]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、horizo​​ntalalignment

下面的片段應該工作

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)

我不想完全進入這個圖表 通用方法:第一步 - 在主標題和圖表之間留出距離:

from matplotlib import rcParams
rcParams['axes.titlepad'] = 20 

然后通過設置坐標插入字幕:

ax.text(0.3, -0.56, 'subtitle',fontsize=12)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM