Matplotlib xkcd () does not work

Ok, I know this question has been asked many times, but I do not get this:

I am trying to use the xkcd style in matplotlib on Ubuntu 16.04 LTS 64-bit with python 2.7.12 64-bit and I am using the sample code from matplotlib.org/xkcd/examples (see below), but I still get this !

What else have i done

  • install matplotlib (version 2.2.2) via pip
  • install xkcd and xkcd script fonts (from ipython / xkcd-font )
  • install Humor Sans font (from imkevinxu / xkcdgraphs )
  • update font cache database (fc-cache -fv)
  • delete ~ / .cache / matplotlib (whole directory)
  • Reboot the computer.
  • Checked that fonts are in ~ / .cache / matplotlib / fontList.json

Any clues how can I get this to work? I appreciate any hint!

Cheers, Micha

I am using example code from matplotlib.org/xkcd/examples :

from matplotlib import pyplot as plt import numpy as np plt.xkcd() fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') plt.xticks([]) plt.yticks([]) ax.set_ylim([-30, 10]) data = np.ones(100) data[70:] -= np.arange(30) plt.annotate( 'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED', xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10)) plt.plot(data) plt.xlabel('time') plt.ylabel('my overall health') fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.bar([-0.125, 1.0-0.125], [0, 100], 0.25) ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.set_xticks([0, 1]) ax.set_xlim([-0.5, 1.5]) ax.set_ylim([0, 110]) ax.set_xticklabels(['CONFIRMED BY\nEXPERIMENT', 'REFUTED BY\nEXPERIMENT']) plt.yticks([]) plt.title("CLAIMS OF SUPERNATURAL POWERS") plt.show() 
+5
source share
1 answer

Something seems to have changed in the way the matplotlib context is used. The working version should be to manually use the context,

 with plt.xkcd(): # your plot here plt.show() 

An example would look like this:

 from matplotlib import pyplot as plt import numpy as np with plt.xkcd(): fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') plt.xticks([]) plt.yticks([]) ax.set_ylim([-30, 10]) data = np.ones(100) data[70:] -= np.arange(30) plt.annotate( 'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED', xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10)) plt.plot(data) plt.xlabel('time') plt.ylabel('my overall health') plt.show() 

enter image description here

This corresponds to the current version of the example . the example related to the question is out of date.

+4
source

Source: https://habr.com/ru/post/1275995/


All Articles