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.

2
  • 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. Commented Aug 11, 2013 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. Commented Aug 12, 2013 at 9:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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:
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...
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.