gpt4 book ai didi

python - Matplotlib 文本不会以 xkcd 字体显示

转载 作者:行者123 更新时间:2023-11-28 16:36:19 32 4
gpt4 key购买 nike

将 xkcd() 与 matplotpib 一起使用时,没有任何字体以通常的漫画字体显示。有什么变化还是我做错了什么?

    x = df['Time']
y = df['Adjustment']

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.xkcd()
plt.show()

感谢您的帮助。

我应该澄清一下,图表将以 xkcd 样式绘制,而不是任何字体。它打印出类似于 Times New Roman 的内容。

最佳答案

作为examples show ,您需要将 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()

这对我来说是这个数字:

enter image description here

如您所见,那里有摇摆不定的线条,但字体是错误的,只是因为它在我的 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

关于python - Matplotlib 文本不会以 xkcd 字体显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25679403/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com