Can't get Pylab plots to display from code

This is the place for queries that don't fit in any of the other categories.

Can't get Pylab plots to display from code

Postby Albert » Wed Dec 04, 2013 5:59 am

Hello folks. I have a frustrating problem with the Canopy distribution of Python 2.7. If I try to plot graphs within code, nothing happens. The graphing window doesn't appear, but the program reports no errors. But if I type the very same sequence of commands in order at the iPython command prompt, the graph does appear! Furthermore, the graphs from my program are somehow being buffered although not displayed, because if I then plot something from the iPython command line, lo and behold, the graph from my program will also appear with it. This means that I can display my graph by the following means: after running my program, I type pylab.plot() on the command line, and my coded graph will appear. This seems to "flush" the buffer. But this is annoying, I want the program to display to graph, and not have to type another command after the program has finished. Anyway, here is a test program that has the problem:

Code: Select all
import numpy
import pylab

def main():
   
    x=numpy.linspace(0,2.0*numpy.pi,1000)
    y=numpy.sin(x)
   
    pylab.plot(y)
   
main()


Can someone tell me why this isn't plotting the data (unless after it runs I type pylab.plot() at the iPython command prompt)?

Thanks!
Albert
 
Posts: 5
Joined: Tue Nov 26, 2013 5:04 am

Re: Can't get Pylab plots to display from code

Postby stranac » Wed Dec 04, 2013 10:39 am

You probably need to call pylab.show().
I'm guessing ipython does that automatically(although it probably also depends on os, backend used, etc...).
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1135
Joined: Thu Feb 07, 2013 3:42 pm

Re: Can't get Pylab plots to display from code

Postby tnknepp » Wed Dec 04, 2013 5:01 pm

stranac is probably right about .show().

I find it it useful to predefine my plot windows, which comes in handy for adding axes, etc.

Code: Select all
import numpy as np
from pylab import *

fig = figure( figsize=(9,5) ) # size is width x height
ax = fig.add_subplot(111)

# make plot
ax.plot(x,y,etc)

# add legend
ax.legend()

# adjust axes
ax.set_ylim( (min,max) )
ax.set_yticks(np.range(0,11),minor=False)

# etc.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 119
Joined: Mon Mar 11, 2013 7:41 pm

Re: Can't get Pylab plots to display from code

Postby Albert » Thu Dec 05, 2013 4:05 am

Thanks stranac and tnknepp! Working together you've solved the problem. The pylab.show() command didn't work by itself, and putting in the line pylab.figure(figsize=(9,5)) didn't work by itself. However, when I combined them, it works perfectly and reliably! Here is the code that does work:

Code: Select all
import numpy
import pylab

def main():
   
    pylab.figure(figsize=(9,5))
   
    x=numpy.linspace(0,2.0*numpy.pi,1000)
    y=numpy.sin(x)
   
    pylab.plot(y)
    pylab.show()
   
main()


Another thing I discovered: in addition to displaying the sine wave graph, the program will also pop up any graph windows which had previously been buffered, if there are any. For example, if I put the line pylab.clf() immediately before the pylab.figure line in the code above, when I run the program, it actually pops up two graph windows, one at standard size and with nothing in it, as well as the sine wave graph size 9x5.

Not sure if this is a bug in the pylab software, as I assume it is meant to just pop up the graph window when pylab.plot is called, but anyway it works well now with this fix, so I'm happy! :D

Thanks for your help guys! Greatly appreciated.
Albert
 
Posts: 5
Joined: Tue Nov 26, 2013 5:04 am

Re: Can't get Pylab plots to display from code

Postby tnknepp » Thu Dec 05, 2013 1:31 pm

Since there is no current figure when you run pylab.clf() pylab creates one. Personally, I do not see the use in this, though it is likely a way of avoiding troublesome errors that may arise when trying to clear a figure that doesn't exist (i.e. if a current figure does not exist, create one and clear it!).

I recommend using close('all') (or just the close command) in pylab to close your figures. I like this better than just clearing a figure.
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 119
Joined: Mon Mar 11, 2013 7:41 pm

Re: Can't get Pylab plots to display from code

Postby Albert » Thu Dec 05, 2013 11:39 pm

Thanks tnknepp!
Albert
 
Posts: 5
Joined: Tue Nov 26, 2013 5:04 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests