lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

matplotlib plot not showing up.

Wed Nov 05, 2014 8:42 pm

Code: Select all

		import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use('Agg')

from matplotlib import pyplot
from matplotlib import dates
               fig = pyplot.figure()
		
		ax = fig.add_subplot(111)
		ax.xaxis.set_major_locator(dates.HourLocator(interval=6))
		ax.xaxis.set_major_formatter(hfmt)
		pylab.xticks(rotation='vertical')

		pyplot.subplots_adjust(bottom=.3)
		pylab.plot(t, s, color='b',label="Inside Temp (C) ",linestyle="-",marker=".")
		pylab.plot(t, v, color='g',label="Outside Temp (C)",linestyle="-",marker=".")
		pylab.xlabel("Hours")
		pylab.ylabel("degrees C")
		pylab.legend(loc='upper left')
		pylab.axis([min(t), max(t), 0, 50])
		ax2 = pylab.twinx()
		pylab.ylabel("% ")
		pylab.plot(t, x, color='y',label="Outside Hum %",linestyle="-",marker=".")
		pylab.plot(t, u, color='r',label="Inside Hum %",linestyle="-",marker=".")
		pylab.axis([min(t), max(t), 0, 100])
		pylab.legend(loc='lower left')
		pylab.figtext(.5, .05, ("Environmental Statistics Last %i Days" % days),fontsize=18,ha='center')

		#pylab.grid(True)

		pyplot.setp( ax.xaxis.get_majorticklabels(), rotation=70)
		ax.xaxis.set_major_formatter(dates.DateFormatter('%m/%d-%H'))
		pyplot.show()

When I execute this program python example.py, nothing shown up. I am expecting a graph to pop up.
what's wrong?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: matplotlib plot not showing up.

Wed Nov 05, 2014 10:43 pm

I'll say what immediately springs to mind: your indentation looks wrong but I'd expect that to throw errors when you run the script. However, I suspect this is just a copy and paste error in the forum rather than your actual problem.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: matplotlib plot not showing up.

Thu Nov 06, 2014 8:17 am

I'm not a power user of matplotlib and don't have it installed on my RPi but I use it occasionally on this ubuntu laptop. Generally show() creates a new x window with the plot in it. If I add the use('Agg') function call at the beginning then no new window pops up. Which makes sense to me as the manual says
Renderer Filetypes Description
AGG png raster graphics – high quality images using the Anti-Grain Geometry engine
PS ps eps vector graphics – Postscript output
PDF pdf vector graphics – Portable Document Format
SVG svg vector graphics – Scalable Vector Graphics
Cairo png ps pdf svg ... vector graphics – Cairo graphics
GDK png jpg tiff ... raster graphics – the Gimp Drawing Kit

And here are the user interfaces and renderer combinations supported; these are interactive backends, capable of displaying to the screen and of using appropriate renderers from the table above to write to a file:
Backend Description
GTKAgg Agg rendering to a GTK 2.x canvas (requires PyGTK)
GTK3Agg Agg rendering to a GTK 3.x canvas (requires PyGObject)
GTK GDK rendering to a GTK 2.x canvas (not recommended) (requires PyGTK)
GTKCairo Cairo rendering to a GTK 2.x canvas (requires PyGTK and pycairo)
GTK3Cairo Cairo rendering to a GTK 3.x canvas (requires PyGObject and pycairo)
WXAgg Agg rendering to to a wxWidgets canvas (requires wxPython)
WX Native wxWidgets drawing to a wxWidgets Canvas (not recommended) (requires wxPython)
TkAgg Agg rendering to a Tk canvas (requires TkInter)
Qt4Agg Agg rendering to a Qt4 canvas (requires PyQt4)
macosx Cocoa rendering in OSX windows (presently lacks blocking show() behavior when matplotlib is in non-interactive mode)
see http://matplotlib.org/faq/usage_faq.htm ... -a-backend
I notice that other people also set the backend to AGG but they are all saving files to view from remote browsers. Does the default GTKagg not work on the pi? I've used tkinter so I know that works (but I don't know that the matplotlib functionality to draw to tk works)

PS see http://trevorappleton.blogspot.co.uk/20 ... raphs.html
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”