Matplotlib文本不会以xkcd字体显示 [英] Matplotlib text won't display in xkcd font

查看:446
本文介绍了Matplotlib文本不会以xkcd字体显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用带有matplotpib的xkcd()时,没有任何字体以通常的漫画字体显示。是否有变化或我做错了什么?

When using xkcd() with matplotpib, none of the font is displaying in the usual comic font. Did something change or am I doing something wrong?

    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的东西。

I should clarify that the graph will plot in the xkcd style, just not any of the font. It prints something similar to Times New Roman.

推荐答案

作为示例显示,您需要在开始时输入 plt.xkcd()在所有绘图命令之前的代码。因此:

As the examples show, you'll need to put plt.xkcd() at the start of the code, before all the plotting commands. Thus:

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数字,没有摇摆不定的行。

As you see, the wobbly lines are there, but the font is wrong, simply because it's not available on my Linux machine (I also get a warning about this on the command line). Putting plt.xkcd() at the end of the code results in the straightforward matplotlib figure, without the wobbly lines.

以下是 pyplot.xkcd()的内容摘要;它只是设置了很多资源参数:

Here is a summary of what pyplot.xkcd() does under the hood; it just sets a lot of resource parameters:

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

这篇关于Matplotlib文本不会以xkcd字体显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆