PDA

View Full Version : Matplotlib - Introduction


tobin
09-01-2006, 10:49 PM
While past versions of Vizard have been compatible with Matplotlib and other 3rd party add-on toolkits, R3 is pre-configured to make the process easy—just as if you were installing them on an official Python.org release.

Matplotlib is a combination of NumPy and PyLab that together gives you a very “Matlab” like environment for generating publication quality figures. You can output to raster formats (e.g., PNG, TIFF, JPG) immediately in a pop-up window or to vector (e.g, EPS, PS) formats that are saved as files. The style of graphs and syntax for creating them—especially when combined with the added Python language constructs through NumPy—will be very familiar to users of Matlab.

Unfortunately, it seems versions across the different contributing packages is important so R3 requires a particular combination of these 3rd party packages. We’ve mirrored these downloads so that you don’t have to try and find them. These links and links to the packages home web pages are included below.

Limitations:
Displaying an interactive figure (i.e., using Matplotlib's show() command) is a blocking command and Vizard will halt until the window is closed. Saving figures to the disk does not have this problem, and raster formats can be read from disk and then displayed as textures if needed for certain scenarios.

Installing

Download and install numpy-0.9.5.win32-py2.4.exe (http://www.worldviz.com/download/files/numpy-0.9.5.win32-py2.4.exe)
Download and install wxPython2.6-win32-ansi-2.6.3.3-py24.exe (http://www.worldviz.com/download/files/wxPython2.6-win32-ansi-2.6.3.3-py24.exe)
Download and install matplotlib-0.87.1.win32-py2.4.exe (http://www.worldviz.com/download/files/matplotlib-0.87.1.win32-py2.4.exe)


Configuring
You need to modify lines 29 & 30 in the file called “matplotlibrc”, which is found in the Vizard30\bin\lib\site-packages\matplotlib\mpl-data folder. Change them to read:


backend : WXAgg
numerix : numpy # numpy, Numeric or numarray

Testing
Restart Vizard, and then paste the following into an empty Vizard script window and run:


from pylab import *

t = arange(0.0, 2.0, 0.01)
s = sin(2*pi*t)
plot(t, s, linewidth=1.0)

xlabel('time (s)')
ylabel('voltage (mV)')
title('About as simple as it gets, folks')
grid(True)
show()


Going deeper:

Matplotlib
Matplotlib’s homepage (http://matplotlib.sourceforge.net/)
Useful tutorial (http://matplotlib.sourceforge.net/tutorial.html)
More examples (http://matplotlib.sourceforge.net/screenshots.html)

NumPy
NumPy’s homepage (http://numpy.scipy.org/#older_array)
Users guide (http://www.scipy.org/NumPy_for_Matlab_Users)

ptjt255
06-02-2010, 09:50 AM
Hi,

I have downloaded and installed numpy, wxPython and matplotlib, but when I run your example code, I keep getting the following error message:

ImportError: No module named pylab

I have checked and the pylab module is in the matplotlib folder, so I am not sure where I'm going wrong.

Any help would be much appreciated.

Best wishes,
Catherine.

Jeff
06-02-2010, 11:08 AM
Is your matplotlib folder in Vizard's site-packages folder?

ptjt255
06-03-2010, 02:43 AM
Yes. the matplotlib folder is in Vizard;s site-packages folder.

Thanks,
Catherine.

ptjt255
06-03-2010, 03:25 AM
Attached is the error I get when I run the pylab source file in case this helps.

Many thanks,
Catherine.

373

Gladsomebeast
06-03-2010, 10:58 AM
Just a guess, but try re-installing numpy as a module in that package is not right according to the screenshot you posted.

ptjt255
06-04-2010, 01:01 AM
I've tried re-installing everything and I am still getting the error:

ImportError: No module named pylab

Any thoughts?

Many thanks,
Catherine.

Jeff
06-04-2010, 01:35 PM
Try un-installing all those packages through Windows add/remove programs. Then download and run these more recent installers.

matplotlib-0.98.3.win32-py2.4.exe (http://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-0.98.3/matplotlib-0.98.3.win32-py2.4.exe/download)
numpy-1.2.1-win32-superpack-python2.4.exe (http://sourceforge.net/project/downloading.php?group_id=1369&use_mirror=superb-west&filename=numpy-1.2.1-win32-superpack-python2.4.exe&a=17402598)
wxPython2.8-win32-ansi-2.8.9.2-py24.exe (http://sourceforge.net/projects/wxpython/files/wxPython/2.8.9.2/wxPython2.8-win32-ansi-2.8.9.2-py24.exe/download)

Make sure they all find Vizard's python installation. Do you still have the same problem?

ptjt255
06-05-2010, 12:16 AM
I have managed to get pylab working, which is great news, thanks for all of your help.

I am now trying to convert my MatLab code, which is:

dir = input('Path: ');
filename = input('Filename: ');
path = strcat(dir,filename);
eval(['data = textread(path,'''',''headerlines'',1);']);

A = []; B = [];

w = find(data(:,5) == 0);

b = find(data(w,9) == 4);
b1 = data(b,12);
c = find(data(w,9) == 5);
c1 = data(c,12);
d = find(data(w,9) == 6);
d1 = data(d,12);

A = [A; b1(1) c1(1) d1(1)]

carvscar = mean(A)* 2.23693629
ccnorm = 29.41;
ccnorm2 = 27.09;
ccnorm3 = 18.67;
ccnorm4 = 10.97;

x = find(data(:,5) == 2);

e = find(data(x,9) == 4);
e1 = data(e,12);
f = find(data(x,9) == 5);
f1 = data(f,12);
g = find(data(x,9) == 6);
g1 = data(g,12);

B = [B; e1(1) f1(1) g1(1)]

subplot(2,2,[1 3]); bar([carvscar, ccnorm, ccnorm2,ccnorm3,ccnorm4],'FaceColor',[0 0.5 0],'BarWidth',0.5)
subplot(2,2,[1 3]);title(strcat('Car vs Car'));ylabel('Speed Error (mph)','FontSize',11);ylim([0 70])
set(gca,'XTickLabel',{'Your Result';'6-7 year olds';'8-9 year olds';'10-11 year olds';'Adults'})

if data(x,7) == 2 % Truck
carvslorry = mean(B)*2.23693629
clnorm = 54.47;
clnorm2 = 38.81;
clnorm3 = 40.24;
clnorm4 = 15.25;
subplot(2,2,[2 4]); bar([carvscar, clnorm, clnorm2,clnorm3,clnorm4],'FaceColor',[0 0 0.5],'BarWidth',0.5); hold on;
subplot(2,2,[2 4]);ylabel('Speed Error (mph)','FontSize',11);title(strcat('Faster Car vs Slower Truck'));ylim([0 70]);
set(gca,'XTickLabel',{'Your Result';'6-7 year olds';'8-9 year olds';'10-11 year olds';'Adults'})
elseif data(x,7) == 1 % Motorbike
carvsbike = mean(B)*2.23693629
cbnorm = 10; % fictional speed difference, need to get data from Damian
subplot(2,2,[2 4]); bar([carvscar, cbnorm],'FaceColor',[0 0 0.5],'BarWidth',0.5); hold on;
subplot(2,2,[2 4]);ylabel('Speed Error (mph)','FontSize',11);title(strcat('Faster Car vs Slower Motorbike'));ylim([0 70]);
set(gca,'XTickLabel',{'Your Result';'Adults'})
end

I have figured out that to perform the open file procedure it's:

dir = 'C:\\Data'
filename = input('Filename: ')
data = open(dir + filename + '.jnk', 'w')

But I can't figure out what the equivelent to:

w = find(data(:,5) == 0);

is?

Many thanks!