Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Headless MatplotLib / Pyplot [duplicate]

I'm trying to make my data analysis and reports less eye stabbing and more graphical with automatically generated graph-files, and to do this I've been playing with matplotlib/pyplot/pylab. Works brilliantly, but when I try to run it on a headless server...

tkinter.TclError: no display name and no $DISPLAY environment variable 

For this application I only use PyLab, but after a little google, I added the below to before the pylab import:

import matplotlib matplotlib.use("Agg") 

Which should have changed the backend, but to no effect.

Any ideas what I'm doing wrong?

The remote machine does have X-forwarding capabilities, but since this application shouldn't NEED to display anything, I believe the usual ssh -X hack is overkill.

Examples:Fiendish Deception

Example working code on same machine

import matplotlib matplotlib.use("Agg") import numpy as np import pylab as pl   xvals=np.arange(100) yvals=np.cumsum(np.random.random(100)) yvals[-10:]=0 yvals=np.log(yvals) pl.close()  pl.plot(xvals,yvals) pl.xlabel("X") pl.ylabel("Y") pl.title("Title")  pl.savefig("testgraph.png") 

Non-working real code

import matplotlib matplotlib.use("Agg") import numpy as np import pylab as pl import utility as util import os  ... non graph stuff... def graph_p(self):     pl.close()     channels=range(self.p.shape[0])     for line in range(self.p.shape[1]):         yvals=np.ma.masked_invalid(map(util.watts_to_dbmhz,self.p[:,line]))         pl.plot(channels,yvals) #Error says it occurs here     pl.xlabel("Subchannel Index")     pl.ylabel("Power (dbmhz)")     pl.title("Plot of per-tone power assignments for %d lines"%self.p.shape[1])     pl.savefig(self.dest+self.scenario+'-power.png') 

Nothing else touches pyplot.

like image 418
Bolster Avatar asked Mar 31 '11 17:03

Bolster


People also ask

What is matplotlib use (' AGG ')?

The last, Agg, is a non-interactive backend that can only write to files. It is used on Linux, if Matplotlib cannot connect to either an X display or a Wayland display.

Why is %Matplotlib inline?

%matplotlib inline turns on “inline plotting”, where plot graphics will appear in your notebook. This has important implications for interactivity: for inline plotting, commands in cells below the cell that outputs a plot will not affect the plot.

How do you fix Userwarning matplotlib is currently using AGG which is a non-GUI backend so Cannot show the figure?

You need to install a GUI toolkit, so that Matplotlib can display graphics (outside of Jupyter, where the browser handles display). For example, if you install the pyqt package then PyPlot should automatically use the qt5agg backend. Hope this will work.

What is TkAgg backend?

With the TkAgg backend, which uses the Tkinter user interface toolkit, you can use matplotlib from an arbitrary non-gui python shell. Just set your backend : TkAgg and interactive : True in your matplotlibrc file (see Customizing matplotlib) and fire up python.


1 Answers

Everything you describe sounds correct. What happens when you run this:

import matplotlib matplotlib.use('Agg') import pylab pylab.plot([1,2], [3,4], linestyle='-') pylab.savefig('foo.png') 

In my environment it produces this (I scaled it down):

enter image description here

like image 75
samplebias Avatar answered Sep 24 '22 23:09

samplebias