Stefan Bollmann
Stefan Bollmann

Reputation: 720

lines do not break in xkcd annotations in python using matplotlib

I wonder how linebreaks in plots using xkcd do work. If I use

import matplotlib.pyplot as plt
#plt.xkcd()
plt.annotate('Testing\nThis\nOut', xy=(0.5, 0.5))

plt.show()

(Example from here)

The output is as expected, but without xkcding. Commenting plt.xkcd() in, the annotation after 'Testing' vanishes.

I tried it with python3.3 and 2.7, py33 & py27-matplotlib installed with port on Mac0SX.

Upvotes: 2

Views: 521

Answers (1)

cge
cge

Reputation: 9888

plt.xkcd() is not well supported with the MacOSX matplotlib backend, which I assume you are using. This extends beyond line breaks; the axes are also straight, not wavy. See this issue for more information.

As suggested there, using another backend is the recommended solution. If you're working in an IPython notebook, the inline backend will work well; for example, using

ipython notebook --pylab inline

your code will work properly with plt.xkcd().

The QT4Agg backend will also work. To use that backend, you could add the following to the very beginning of your code:

import matplotlib
matplotlib.use('QT4Agg')

However, that will require that you have that backend installed, which requires Qt4/PyQt4.

Upvotes: 3

Related Questions