2

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.

Community
  • 1
  • 1
Stefan Bollmann
  • 640
  • 4
  • 12
  • 32
  • For what it's worth, I cannot replicate this result with matplotlib 1.3.0 with Python 2.7.5 using IPython 1.0.0 on Ubuntu 13.04. – sodd Aug 11 '13 at 18:40
  • Hm, I used matplotlib 1.3.0 with Python 2.7.5 and Python 3.3.2. I saw this behaviour first with iPython, but use right now the bash. In a few hours I can test myself on Ubuntu. – Stefan Bollmann Aug 12 '13 at 09:16

1 Answers1

3

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.

sodd
  • 12,482
  • 3
  • 54
  • 62
cge
  • 9,552
  • 3
  • 32
  • 51
  • By accident I solved this and then read your explanation. The output is exactly as pointed out in your link. However, it is actually not possible for me to use your solution because of some error messages: – Stefan Bollmann Aug 12 '13 at 09:23
  • By accident I solved this and then read your explanation. The output is exactly as pointed out in your link. However, it is actually not possible for me to use your solution because of some error messages. import matplotlib.pyplot after matplotlib.use('QT4Agg') throws errors. The solution working for me right now is using pylab.savefig('example.pdf'). I will try your suggestion, but somehow I've got the feeling that I need to ask again for these errors... – Stefan Bollmann Aug 12 '13 at 09:32
  • That's almost certainly because you don't have Qt4 and PyQt4 installed. Using an ipython notebook will work, and, as you note, saving the figure, which doesn't use the MacOSX backend, will also work. – cge Aug 12 '13 at 18:38