# Plotting with Pylab

2 Tallied Votes 2K Views

You can download the matplotlib free from: http://matplotlib.sourceforge.net/
It has, amongst many other things, the module pylab that allows for high quality plotting of data. All you have to do is to feed it a list of x values and the corresponding list of y values calculated from the x data and you are ready to enjoy the plot in its separate window/frame. From there you can zoom the graph, drag the graph, change the margins, and save the graph to a popular image file like PNG.

``````# plotting with the pylab module from matplotlib
# free from: http://matplotlib.sourceforge.net/
# used windows istaller matplotlib-0.90.0.win32-py2.5.exe
# tested with Python25     EU      4/21/2007

import math
import pylab  # matplotlib

# create the x list data
# arange() is just like range() but allows float numbers
x_list = pylab.arange(0.0, 5.0, 0.01)

# calculate the y list data
y_list = []
for x in x_list:
y = math.cos(2*math.pi*x) * math.exp(-x)
y_list.append(y)

pylab.xlabel("x")
pylab.ylabel("cos(2pi * x) * exp(-x)")

# draw the plot with a blue line 'b' (is default)
# using x,y data from the x_list and y_list
# (these lists can be brought in from other programs)
#
# other drawing styles -->
# 'r' red line, 'g' green line, 'y' yellow line
# 'ro' red dots as markers, 'r.' smaller red dots, 'r+' red pluses
# 'r--' red dashed line, 'g^' green triangles, 'bs' blue squares
# 'rp' red pentagons, 'r1', 'r2', 'r3', 'r4' well, check out the markers
#
pylab.plot(x_list, y_list, 'b')

# save the plot as a PNG image file (optional)
pylab.savefig('Fig1.png')

# show the pylab plot window
# you can zoom the graph, drag the graph, change the margins, save the graph
pylab.show()``````

Nice example ...... if you use scipy (from scipy import *) instead of math then more exotic functions can be plotted ...... anyways ..... the output is very very Matlab-ish ....... may be soon Matlab will be out of business :D

Thanks dear.but i have problem importing the pylab.
i am working with the windows and python 2.5.and have installed the pylab.but it returns me "No module named pylab".I have such problem with qt.please guide me.thanks.

after pylab.show() I don't get the >>> in IDLE!
The program stalls on that last pylab.show() statement.

vegaseat 1,735

after pylab.show() I don't get the >>> in IDLE!
The program stalls on that last pylab.show() statement.

For heaven sake don't run the code from the Python shell!
Run it from the editor!

The python shell is used for testing very short Python code concepts, mostly one-liners!

Thanks dear I had some basic problem witch solved.
greet.

OK, let's put it different!
If I run a program as i.e. the program above, of course in the editor and NOT in the shell, than after the pylab.show() statement I don't get the >>> back in the shell.
Also, if I add a print statement behind the pylab.show() statement above, i.e. print 'bla bla', then 'bla bla' is NOT printed!

I have a slighlty different problem but maybe you can help.
I created a script which load a csv file and draw it using pylab. My aim is to call the function from excel and to pass the file name as argument. If I run the script from Ipython it works well. When I use the function from excel the first instance works well : it open a TK and display my plot. When I close my plot I am back to Excel and everything works well. If I try to call my function a second time the plot display again but this time I cannot close it without a crash : RunTime Error.
I have the same problem if I try to run it from Pythonwin, looks like it only works with Ipython!! Help will be really appreciate.
Thank you

vegaseat 1,735

OK, let's put it different!
If I run a program as i.e. the program above, of course in the editor and NOT in the shell, than after the pylab.show() statement I don't get the >>> back in the shell.
Also, if I add a print statement behind the pylab.show() statement above, i.e. print 'bla bla', then 'bla bla' is NOT printed!
Since pylab uses Tkinter itself, there could be a problem with the IDLE IDE. I tested it using the matplotlib-0.99.0.win32-py2.5.exe installer, Python25 and Windows XP. I simply do not get the problems you observed.

I'm having the exact same problem, did you find a solution?

Can someone help in telling me how to install pylab with python version 5 and not 6? Because MGL tools works with Python ve 5 ...

Thanks

vegaseat 1,735

Do you mean Python25 versus Python26?

Well, at least give us your Operating System info.

You can add pylab.close() as the last line. When the pop-up figure is closed, you will have control over the IDE again.

Ctrl-c works too (keyboard interrupt).

TrustyTony 888

Close the pylab window and the prompt comes back and your print will print.

Perhaps a SOLUTION for people, who have problem with calling plt.show() the second time:
This is a WINDOWS problem only.. I use Archlinux (Python26) at home and there is no problem with calling plt.show() more than one time.
At work I use Windows XP and Python26 and there is that problem.
My solution avoids calling plt.show()

Dummy-Code:

``````import numpy as np
import matplotlib
import matplotlib.pylab as plt
...
matplotlib.use('TkAgg')
...
nS = np.arange(0, 10, 1)
plt.figure()
plt.get_current_fig_manager().window.state('zoomed')
plt.get_current_fig_manager().window.title('Demo')
plt.get_current_fig_manager().window.focus_force()
plt.ion()
nData = np.array([...])
plt.plot(nS, nData, 'r-,', label='org1000')
plt.legend()
plt.draw()
...
#open a lot more figures
...
while (len(plt.get_fignums()) > 0):
plt.get_current_fig_manager().window.mainloop()

print ('continued')``````

You can open a lot of figures.. if all figures are closed, the python-script will continue...

vegaseat commented: thanks for the tip +10
Tech B 48

Maybe you guys didn't install the pylab the correct way.
I've never used pylab, but usually when its an import error python can't find the lib's you installed.

Try installing pylab with the setup.py instead of the installer .exe (assuming you used the installer .exe) for thoese of you who have import errors.

vegaseat 1,735

matplotlib-0.99.3.win32-py2.6.exe
for Python26 on my Windows box

This will work ....
import pylab
but on some other OS this may be better ...
import matplotlib.pylab as pylab
since pylab.py is in package matplotlib

I'm having the exact same problem, did you find a solution?

Use `ion()` method instead of `show()` . It turns interactive mode on.

I've found that the key problem is that plt.show() a second time before restarting the shell, or twice in the same script is what creates the lockup/crash in my IDLE environment. If the IDLE environment gets reset the show() command will work again, but using it twice is definitely the cause of the problem.

I've found that using the code snippet from the previous email

while (len(plt.get_fignums()) > 0):
plt.get_current_fig_manager().window.mainloop()

instead of using plt.show() prevents the crash from happening and allows creating more plots after closing a first set in a single script.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.