As the 例子显示 http://matplotlib.org/xkcd/examples/showcase/xkcd.html,你需要把plt.xkcd()
在代码的开头,所有绘图命令之前。因此:
from matplotlib import pyplot as plt
import numpy as np
x = np.arange(10)
y = np.sin(x)
plt.xkcd()
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax1 = fig.add_subplot(1,1,1)
ax1.plot(x,y)
ax1.xaxis.set_visible(False)
ax1.yaxis.set_visible(False)
plt.axvline(x=2.3, color='k', ls='dashed')
plt.axvline(x=6, color='k', ls='dashed')
ax.text(4,4,'Culture Shock', size=16)
plt.title('Test title')
plt.show()
这对我来说是这个数字:
正如您所看到的,摆动线在那里,但字体是错误的,仅仅是因为它在我的 Linux 机器上不可用(我还在命令行上收到了有关此问题的警告)。
推杆plt.xkcd()
代码末尾的结果是简单的 matplotlib 图形,没有不稳定的线条。
以下是内容的摘要pyplot.xkcd()
在幕后做的事;它只是设置了很多资源参数:
rcParams['font.family'] = ['Humor Sans', 'Comic Sans MS']
rcParams['font.size'] = 14.0
rcParams['path.sketch'] = (scale, length, randomness)
rcParams['path.effects'] = [
patheffects.withStroke(linewidth=4, foreground="w")]
rcParams['axes.linewidth'] = 1.5
rcParams['lines.linewidth'] = 2.0
rcParams['figure.facecolor'] = 'white'
rcParams['grid.linewidth'] = 0.0
rcParams['axes.unicode_minus'] = False
rcParams['axes.color_cycle'] = ['b', 'r', 'c', 'm']
rcParams['xtick.major.size'] = 8
rcParams['xtick.major.width'] = 3
rcParams['ytick.major.size'] = 8
rcParams['ytick.major.width'] = 3