如何在 Matplotlib 中使用 XKCD 字体?
若要让xkcd字体生效,我们可以使用plt.xkcd()开启草图式绘图模式。
步骤
- 设置图形尺寸并调整子图彼此之间及周围的边距。
- 使用 numpy 创建x和y数据点。
- 使用plt.xkcd()开启草图式绘图模式。
- 创建一个新图形或激活现有图形。
- 将一个轴添加到图形中作为子图安排的一部分。
- 使用plot()方法绘制x和y数据点。
- 在绘制图形上放置一个文本和标题。
- 若要显示图形,请使用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()输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP