如何在 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.