如何在 matplotlib 作圖時更改 x 刻度標記的字型大小?


若要更改 matplotlib 圖表xticks 的字型大小,我們可以使用fontsize 引數。

步驟

  • 匯入 matplotlibnumpy

  • 設定圖形大小並調整子圖之間的邊距和周圍的邊距。

  • 使用 numpy 建立 **x** 和 **y** 資料點。

  • 使用 plot() 方法繪製 xy 資料點。

  • 使用 xticks() 方法設定 xticks 的字型大小。

  • 若要顯示圖形,請使用 show() 方法。

示例

from matplotlib import pyplot as plt
import numpy as np

# Set the figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# x and y data points
x = np.linspace(-5, 5, 100)
y = np.sin(x)

plt.plot(x, y)

# Set the font size of xticks
plt.xticks(fontsize=25)

# Display the plot
plt.show()

輸出

將生成以下輸出 -

更新於: 08-9 月-2023

41K+ 檢視

開啟你的 職業生涯

完成課程獲得認證

開始使用
廣告