如何在 Matplotlib 說明中獲得不同的字型大小?
要在同一說明方法中新增不同的字型大小,我們可以執行以下步驟
- 製作可以放置文字的 x 和 y 資料點的列表。
- 初始化變數“標籤”,即字串。
- 列出字型的大小。
- 使用subplots() 方法建立圖形和一組子圖。
- 迭代以上列表並註釋每個標籤的文字並設定其字型大小。
- 要顯示該圖形,請使用show() 方法。
示例
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True X = [0.1, .2, .3, .4, .5, .6, 0.8] Y = [0.1, 0.12, 0.13, 0.20, 0.23, 0.25, 0.27] labels = 'Welcome' sizes = [10, 20, 30, 40, 50, 60, 70] fig, ax = plt.subplots() for x, y, label, size in zip(X, Y, labels, sizes): ax.annotate(f"$\it{label}$", (x, y), fontsize=size, color='red') plt.show()
輸出
廣告