|
From: Christopher F. <ch...@fo...> - 2003-12-15 20:22:48
|
First off, I want to congratulate the authors of matplotlib for making
a great package. This is the python plotting library I've been waiting
for.
I'm trying to use matplotlib on OSX (so with WX rather than gtk) in
conjunction with my own code that produces Markov chain Monte Carlo
simulations. At the end of the simulation, I try to plot traces and
histograms of all of the sampled values. However, matplotlib gets stuck
on the first plot. After calling show(), it does not move to the next
line of code for some reason, but instead hangs. When I try and close
the plot manually, it terminates the python code. How can I get
matplotlib to produce a new plot each time it is called, moving on
after each plot is produced, displayed and saved to file. Here is a
sample function from my code:
def
plot_histogram(data,name,nbins=None,xlab='Value',ylab='Frequency',suffix
=''):
'Internal histogram specification for handling nested arrays'
'If there is only one data array, go ahead and plot it ... '
if len(shape(data))==1:
print 'Generating histogram of',name
try:
figure(1)
nbins = min(nbins or 10,len(unique(data)))
'Generate histogram'
hist(data,nbins)
'Plot options'
xlabel(name)
ylabel("Frequency")
show()
'Save to file'
savefig("%s%s.png" % (name,suffix))
close(1)
except OverflowError:
print '... cannot generate histogram'
else:
'... otherwise, plot recursively'
tdata = swapaxes(data,0,1)
for i in range(len(tdata)):
Thanks for any advice,
C.
--
Christopher J. Fonnesbeck ( c h r i s @ f o n n e s b e c k . o r g )
Georgia Cooperative Fish & Wildlife Research Unit, University of Georgia
|
|
From: John H. <jdh...@ac...> - 2003-12-15 20:37:27
|
>>>>> "Christopher" == Christopher Fonnesbeck <ch...@fo...> writes:
Christopher> First off, I want to congratulate the authors of
Christopher> matplotlib for making a great package. This is the
Christopher> python plotting library I've been waiting for.
Thanks!
Christopher> I'm trying to use matplotlib on OSX (so with WX
Christopher> rather than gtk) in conjunction with my own code that
Not necessarily. pygtk, gtk-2, etc... have been packaged for fink.
I'm going to try and install them on my powerbook when I get a few
minutes.
Christopher> produces Markov chain Monte Carlo simulations. At the
Christopher> end of the simulation, I try to plot traces and
Christopher> histograms of all of the sampled values. However,
Christopher> matplotlib gets stuck on the first plot. After
Christopher> calling show(), it does not move to the next line of
Christopher> code for some reason, but instead hangs. When I try
Christopher> and close the plot manually, it terminates the python
Christopher> code. How can I get matplotlib to produce a new plot
Christopher> each time it is called, moving on after each plot is
Christopher> produced, displayed and saved to file. Here is a
Christopher> sample function from my code:
show must be the last line of your script. Is it possible to do all
the figures in the loop and then call show? If your number of figures
is very large, you could run into memory problems this way. There is
a work-around, but if you can do it all in memory that is the easiest
solution
for i in range(1,10):
figure(i)
# do plot i
savefig('plot%d'%i)
show()
This is an area that I am actively working on (in fact I was working
on it when you emailed!) so if this isn't a viable solution for you
let me know. I'm implementing some features to make it easier to use
matplotlib in with xvfb (virtual x) so you can produce plots in wx or
gtk without launching the GUI windows.
JDH
|
|
From: John H. <jdh...@ac...> - 2003-12-15 21:07:06
|
>>>>> "Christopher" == Christopher Fonnesbeck <ch...@fo...> writes:
Christopher> code for some reason, but instead hangs. When I try
Christopher> and close the plot manually, it terminates the python
Christopher> code. How can I get matplotlib to produce a new plot
Christopher> each time it is called, moving on after each plot is
Christopher> produced, displayed and saved to file. Here is a
Christopher> sample function from my code:
For the GTK backend, there is another solution. The call to
ShowOn().set(1) tells matplotlib to execute the commands as they are
issued, which was originally designed for people using matplotlib
from the python shell but also applies to your case, where you want to
generate a bunch of figures one at a time
import matplotlib
matplotlib.use('GTK')
from matplotlib.matlab import *
from matplotlib.backends.backend_gtk import ShowOn
ShowOn().set(1)
t = arange(0.0, 3.0, 0.01)
for i in range(1,10):
figure(1)
s = sin(2*pi*i*t)
plot(t,s)
savefig('plot%02d' % i)
close(1)
*Note there is no call to show at the end*
The same trick *does not* currently work with the WX backend, though
perhaps Jeremy can take a look at it and determine why not. Somehow
the call the ShowOn().set(1) seems to screw it up.
We'll look into it...
JDH
|
|
From: Christopher F. <ch...@fo...> - 2003-12-15 21:13:55
|
On Dec 15, 2003, at 3:29 PM, John Hunter wrote:
>
>
> Christopher> I'm trying to use matplotlib on OSX (so with WX
> Christopher> rather than gtk) in conjunction with my own code that
>
> Not necessarily. pygtk, gtk-2, etc... have been packaged for fink.
> I'm going to try and install them on my powerbook when I get a few
> minutes.
I like to avoid fink unless I absolutely have to use it -- it ends up
installing a whole bunch of packages that I already have elsewhere on
my machine, thereby creating the need to maintain multiple
installations of the same package.
... I'm not sure why I'd want to use gtk instead of wx anyhow, given
the choice.
>
> show must be the last line of your script. Is it possible to do all
> the figures in the loop and then call show? If your number of figures
> is very large, you could run into memory problems this way. There is
> a work-around, but if you can do it all in memory that is the easiest
> solution
>
> for i in range(1,10):
> figure(i)
> # do plot i
> savefig('plot%d'%i)
> show()
>
> This is an area that I am actively working on (in fact I was working
> on it when you emailed!) so if this isn't a viable solution for you
> let me know. I'm implementing some features to make it easier to use
> matplotlib in with xvfb (virtual x) so you can produce plots in wx or
> gtk without launching the GUI windows.
>
>
I'm trying to keep my plotting in a separate module from everything
else, so that I don't have to make changes all over the place when I
change my plotting code. For example, plot_histogram() can be called
from any other module without knowing how the plot is implemented. If I
have to call show() from each module that uses the plotting module, it
will start to get messy. Outside of the Plot module, the rest of my
code shouldn't have to know anything about matplotlib. So, I'd like to
be able to gererate and save a plot, then move on to the next piece of
code. I'm trying to use your package in place of scipy, which is what I
have been using to generate plots up until now; hopefully I can make it
work.
--
Christopher J. Fonnesbeck ( c h r i s @ f o n n e s b e c k . o r g )
Georgia Cooperative Fish & Wildlife Research Unit, University of Georgia
|
|
From: John H. <jdh...@ac...> - 2003-12-15 21:40:47
|
>>>>> "Christopher" == Christopher Fonnesbeck <ch...@fo...> writes:
Christopher> ... I'm not sure why I'd want to use gtk instead of
Christopher> wx anyhow, given the choice.
No compelling reason. The GTK backend was the first and probably the
most stable for that reason. This is the first release with the wx
backend, so there are likely some bugs lurking that will have to be
found and stamped out.
Christopher> I'm trying to keep my plotting in a separate module
Christopher> from everything else, so that I don't have to make
Christopher> changes all over the place when I change my plotting
Christopher> code. For example, plot_histogram() can be called
Christopher> from any other module without knowing how the plot is
Christopher> implemented. If I have to call show() from each
Christopher> module that uses the plotting module, it will start
Christopher> to get messy. Outside of the Plot module, the rest of
Christopher> my code shouldn't have to know anything about
Christopher> matplotlib. So, I'd like to be able to gererate and
Christopher> save a plot, then move on to the next piece of
Christopher> code. I'm trying to use your package in place of
Christopher> scipy, which is what I have been using to generate
Christopher> plots up until now; hopefully I can make it work.
Would you like this to run 'offline' so that no windows pop up as you
create the figures, or is it enough simply that you can create them
one at a time in batch mode with the plot functionality encapsulated?
I just installed wxpython and matplotlib on OS X, so I'll have a
chance to delve into this issue a bit....
John Hunter
|
|
From: John H. <jdh...@ac...> - 2003-12-15 22:42:53
|
>>>>> "Christopher" == Christopher Fonnesbeck <ch...@fo...> writes:
Christopher> ... I'm not sure why I'd want to use gtk instead of
Christopher> wx anyhow, given the choice.
One more option. If all you want is the batch generation of figures
offline with no display, you can use either the PS or GD backends. PS
only requires Numeric. gd, however, takes some energy to build.
But it works: I just built all the prereqs on OSX from source and it
worked perfectly.
You need
libjpeg - http://freshmeat.net/redir/libjpeg/5665/url_tgz/jpegsrc.v6b.tar.gz
./configure
sudo make
sudo cp libjpeg.a /usr/local/lib/
sudo cp *.h /usr/local/include/
libpng - http://umn.dl.sourceforge.net/sourceforge/libpng/libpng-1.2.5.tar.gz
cp scripts/makefile.macosx Makefile
sudo make install
You then need to follow the build instructions for gd, gdmodule and
dependencies at
http://matplotlib.sourceforge.net/backends.html#GD
Here's an example script ....
import matplotlib
matplotlib.use('GD')
from matplotlib.matlab import *
t = arange(0.0, 3.0, 0.01)
for i in range(1,10):
figure(1)
s = sin(2*pi*i*t)
plot(t,s)
savefig('plot%02d' % i)
close(1)
I put all this here mainly for archival purposes in case someone wants
to use matplotlib / OSX / gdmodule. We'll get the wx thing figured
out too ....
JDH
|