如何設定 Matplotlib 座標系圖例的字型大小?


要設定 matplotlib 座標系圖例的字型大小,我們可以進行以下步驟 −

  • 使用 numpy 建立 x 和 y 的點。

  • 使用 plot() 方法並標記 y=sin(x) 來繪製 x 和 y。

  • 使用 title() 方法為該圖命名。

  • 要設定字型大小,我們可以將 rcParams 圖例字型大小覆蓋為 20。

  • 使用 legend() 方法,並將該圖例放在右上方的位置。

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

示例

import numpy as np
from matplotlib import pyplot as plt
import matplotlib
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(1, 10, 50)
y = np.sin(x)
plt.plot(x, y, c="red", lw=7, label="y=sin(x)")
plt.title("Sine Curve")
matplotlib.rcParams['legend.fontsize'] = 20
plt.legend(loc=1)
plt.show()

輸出

更新時間: 09-4 月-2021

2K+ 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告