如何在 Matplotlib 中使用 XKCD 字型?


若要讓xkcd字型生效,我們可以使用plt.xkcd()開啟草圖式繪圖模式。

步驟

  • 設定圖形尺寸並調整子圖彼此之間及周圍的邊距。
  • 使用 numpy 建立xy資料點。
  • 使用plt.xkcd()開啟草圖式繪圖模式。
  • 建立一個新圖形或啟用現有圖形。
  • 將一個軸新增到圖形中作為子圖安排的一部分。
  • 使用plot()方法繪製xy資料點。
  • 在繪製圖形上放置一個文字和標題。
  • 若要顯示圖形,請使用show()方法。

示例

from matplotlib import pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

x = np.arange(10)
y = np.sin(x)

plt.xkcd()

fig = plt.figure()

ax = fig.add_subplot(1, 1, 1)
ax.plot(x, y)
ax.text(4, 4, '(4, 4)', size=16)

plt.title('Y=sin(x)')

plt.show()

輸出

更新於:2021 年 7 月 7 日

948 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.