3

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()

Thanks for any help.

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.

  • Possible duplicate of [Getting xkcd plots using matplotlib](http://stackoverflow.com/questions/19663986/getting-xkcd-plots-using-matplotlib) – bastelflp Nov 28 '15 at 21:37

1 Answers1

4

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()

which results in this figure for me:

enter image description here

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.

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
  • I had actually tried that but made another attempt. No success. I'm wondering if it was changed to avoid looking exactly like the comic. – emrys golden Sep 05 '14 at 06:20
  • @user3741467 If you get a figure with wobbly lines but without the proper font (like the figure I get in my updated answer), then very likely, the font is not installed on your machine. –  Sep 05 '14 at 08:11
  • 1
    That was it. I'm on a new machine so I downloaded the font and removed the font cache. Thank you! – emrys golden Sep 05 '14 at 15:20
  • 2
    Noob here. How do you get that font downloaded, and how do you remove the font cache? – eran Oct 01 '14 at 18:42
  • After the font is installed the matplotlib font cache has to be cleared: Win7: `%userprofile%\.matplotlib\fontList.cache` - Ubuntu: `~/.cache/matplotlib` - source: http://stackoverflow.com/a/26106170/5276734 – bastelflp Nov 28 '15 at 21:27