4

I'm running Ubuntu 14.04 (server edition) on my home server and I have Python 2.7.6 installed with matplotlib 1.3.1 using TkAgg as the backend. I'm connecting to it via ssh -Y.

The problem I'm having is that creation of the plot object is very slow. So for example:

import matplotlib.pyplot as plt
x=[0,1,2,3,4]
y=[0,1,4,9,16]
plt.plot(x,y)

This code takes about 10 seconds to run when it should take a fraction of a second. Notice that it isn't even showing the plot, if I add plt.show() it makes no significant difference in time. Running the code in the python interpreter or from a file also makes no difference. The plot does get displayed on my local screen just fine once I wait long enough. Also plotting 5 points or 50 points doesn't seem to make any difference.

Any ideas?

Yelneerg
  • 161
  • 2
  • 11
  • my guess is that most of that time is spent importing matplotlib and its dependencies. – Paul H Aug 26 '14 at 20:42
  • If I run it line by line in the python interpreter the import command takes no time, it's just the plt.plot(x,y) command that takes up pretty much the whole ten seconds. Also on my local machine there isn't that lag, so why would it exist on the remote machine? – Yelneerg Aug 26 '14 at 20:49
  • 1
    TkAgg is a graphical, interactive backend. But your server is headless. Try a different backend. – Paul H Aug 26 '14 at 20:50
  • Do you have a recommendation for which backend to try? – Yelneerg Aug 26 '14 at 20:59
  • 1
    Your choices are: GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo CocoaAgg MacOSX Qt4Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG. If you're only saving to PNG files, I'd go with Agg. Other than that, I use PDF or SVG depending on the situation. – Paul H Aug 26 '14 at 21:02
  • I guess I still don't see why it should matter if there is a gui running on the machine so long as all the matplotlib dependencies are installed. – Yelneerg Aug 26 '14 at 21:27
  • It's just a guess, but it seems like initializing a gui with buttons and zoom capability and caches and editing functionality, even if it can't be displayed, would take some time. – Paul H Aug 26 '14 at 21:35

1 Answers1

1

To build on @Paul H's answer, this code outputs the plot as a SVG graphic, then displays it using the external feh utility.

Setting the backend is important -- check What is a Backend for more info.

source

import matplotlib
# see http://matplotlib.org/faq/usage_faq.html#what-is-a-backend
# or
# https://matplotlib.org/stable/users/explain/backends.html
matplotlib.use('Svg')

import os
import matplotlib.pyplot as plt

x=[0,1,2,3,4]
y=[0,1,4,9,16]
plt.plot(x,y)
plt.savefig('plot.svg')
os.system('feh plot.svg')       # or "eog" for Gnome
pedrosaurio
  • 4,708
  • 11
  • 39
  • 53
johntellsall
  • 14,394
  • 4
  • 46
  • 40
  • But it's already creating the plot and forwarding it over my ssh tunnel, it's just taking way longer than it should. – Yelneerg Aug 26 '14 at 21:12
  • You could try tweaking the ssh parameters. Or, plot directly to the terminal -- fast! http://stackoverflow.com/questions/20295646/python-ascii-plots-in-terminal – johntellsall Aug 26 '14 at 21:17
  • What makes me that that it isn't the ssh connection is that the command `plt.plot(x,y)` takes 10 seconds but isn't doing any x11 forwarding while `plt.show()` is nearly instantaneous even though it is forwarding the graphics over the ssh connection. – Yelneerg Aug 26 '14 at 21:25
  • 1
    you need to change the backend away from `TkAgg`. Add `import matplotlib; matplotlib.use('Agg')`. Be sure it's executed before you import `pyplot` – Paul H Aug 26 '14 at 21:36
  • Well that did make `plt.plot(x,y)` work much faster, but now `plt.show()` doesn't display the plot. – Yelneerg Aug 26 '14 at 21:40
  • `plt.savefig('plot.png')` works but it's kinda a pain to have to open the plot in an external program each time I want to look at what I've generated. – Yelneerg Aug 26 '14 at 21:47
  • I've modified the code so it displays after saving. – johntellsall Aug 26 '14 at 21:49
  • Ah, good thought, although I think I'll use feh instead of eog so I don't have to install the ridiculous number of gnome dependencies. – Yelneerg Aug 26 '14 at 21:51
  • Good call. I use 'eog' because the ridiculous dependencies are already installed :) If the answer works for you please mark it Accepted. – johntellsall Aug 26 '14 at 21:54
  • Can you edit the code to include changing the backend? Then I'll accept it. – Yelneerg Aug 26 '14 at 22:11
  • I've fleshed out the code and description -- thanks @Yelneerg – johntellsall Aug 26 '14 at 22:30
  • same problem here. following – Hanan Shteingart Dec 19 '18 at 20:00
  • `feh` cannot display vector graphics like `.svg` files. You should change that to `.png` or some other raster image format that `feh` can display. – Dorian Jan 21 '21 at 12:58