3

I have the same problem as this problem, namely that when using matplotlib and networkx on Mac OS X 10.8.5, labels are not displayed.

The solution in that thread is "Switching to one of the Agg based backends should 'resolve' the problem by avoiding it" by @tcaswell here. Can someone tell me what that means/ how to do it?

I'm sorry to start a new thread, but I don't have enough reputation to comment or ask any questions on the previous post! If someone could merge this post and the previous, that would be best.

Thanks Bobby

Community
  • 1
  • 1
travelingbones
  • 7,919
  • 6
  • 36
  • 43

2 Answers2

8

This should work (note that use needs to be called before pyplot is imported):

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

There are more details in other questions if you search for "Agg" or "backend", e.g. this one.

Edit: Sorry, maybe I didn't understand exactly what you are trying to do. As sebix points out, Agg is a backend for plotting to a file (i.e. non-interactive plots), and if you want an interactive plot to appear on the screen, then use one of the interactive backends. e.g. Qt4Agg (note this needs PyQt4).

Community
  • 1
  • 1
spaceghost
  • 485
  • 2
  • 12
  • Thank you. I can now `import matplotlib.pyplot as plt` without any errors! But it doesn't show the plots :( For example: `import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt x = range(10) y = [a**2 for a in x] plt.plot(x,y) plt.show()` finishes with no errors and no plots. Any ideas? – travelingbones Oct 28 '14 at 14:49
  • Sorry, I don't know how to make a code block when commenting. – travelingbones Oct 28 '14 at 14:51
  • I should mention that switching `plt.show()` for `plt.savefig(...)` works! So if I keep the Agg backend, I can't display plots (they're supposed to be interactive), and if I don't use 'Agg' I get the error from [this](http://stackoverflow.com/questions/25865975/pylab-networkx-no-node-labels-displayed-after-update/25870916) – travelingbones Oct 28 '14 at 15:01
  • 2
    The 'Agg'-backend is supposed to show nothing - it works headless. If you want and interactive interface, look at `TkAgg`, `GtkAgg` or `QtAgg`. – sebix Oct 28 '14 at 17:41
0

You can also use

plt.switch_backend('Qt4Agg')
loknar
  • 539
  • 5
  • 12