From: Engelsma, D. <D.E...@La...> - 2004-05-03 20:15:58
|
Hello -- I'm having a dickens of a time getting this to work... I believe I understand what you mean about maintaining a list of FigureCanvasAggs and using string & bitmap methods to get the figures into my wxDialog. Where I'm stuck is in plotting a histogram to a FigureCanvasAgg... I've checked out your examples, but there doesn't appear to be anything that directly uses FigureCanvasAgg. I've tried many different ways (mostly based on the embedded_in_wx.py and histogram_demo.py examples). Based on the example "histogram_demo.py" could you please give some pointers as to how to plot the histogram to a FigureCanvasAgg? I think I can handle things after that... Thanks for your assistance! Dave Engelsma Lacks Wheel Trim Systems > -----Original Message----- > From: John Hunter [mailto:jdh...@ac...] > Sent: Saturday, April 24, 2004 9:53 AM > To: Engelsma, Dave > Cc: mat...@li... > Subject: Re: [Matplotlib-users] wxList and FigureCanvasWx > > >>>>> "Engelsma," == Engelsma, Dave <D.E...@La...> writes: > > Dave> Hello - I''ve got a wxDialog where, among other > Dave> controls, I have a wxList and a > Dave> FigureCanvasWx. Depending on what single item is > Dave> selected in the wxList, the FigureCanvasWx should show > Dave> the appropriate graph. It's important that the > Dave> matplotlib-generated graph stay in the dialog along > Dave> with the other controls (I don't want to generate a > Dave> separate frame for the graph). > > I'm not a wx guru, so this may not be the best way, but it I think it > is a pretty good way. Instead of putting a FigureCanvasWX in your, > use a wxPanel instead. Maintain a list of FigureCanvasAgg and use > them to draw your figures. Transfer these to the wxPanel using string > and bitmap methods. This is effectively what wxagg does. The main > difference is that you will have one canvas and a list of > FigureCanvasAggs. > > You can get the width and height of the figure you need to draw with > > > > Suppose agg below is an FigureCanvasAgg instance > > agg.draw() > s = agg.tostring_rgb() > l,b,w,h = agg.figure.get_window_extent().get_bounds() > image = wxEmptyImage( w,h ) > image.SetData(s) > bitmap = image.ConvertToBitmap() > > You can then transfer the bitmap to the wxpanel. If you want to do > all the 'draw' calls upfront so you don't have to repeatedly draw if > the user selects one figure, then another, then the original, that > will be fine. > > Agg is a pretty fast renderer, which may more than make up for the > performance hit of having to go through the string->bitmap->canvas. > > Let me know how it goes. > > JDH |
From: Engelsma, D. <D.E...@La...> - 2004-05-04 19:54:14
|
John -- Thanks for the help! Your example cleared up a lot of problems. I'll send you a screen dump of what I'm doing when it's ready. Dave Engelsma Lacks Wheel Trim Systems > -----Original Message----- > From: John Hunter [mailto:jdh...@ac...] > Sent: Monday, May 03, 2004 4:52 PM > To: Engelsma, Dave > Cc: mat...@li... > Subject: Re: [Matplotlib-users] wxList and FigureCanvasWx > > >>>>> "John" == John Hunter <jdh...@ac...> writes: > > John> The example is included below. I'll add it to the examples > John> dir for people who want to work directly with the Agg canvas > John> and renderer. > > oops, left out a critical line - adding the axes to the figure. > > Here is a modified example, which also demonstrates initializing a > Numeric array from the string and passing the image off to PIL.... > > > """ > This is an example that shows you how to work directly with the agg > figure canvas to create a figure using the pythonic API. > > In this example, the contents of the agg canvas are extracted to a > string, which can in turn be passed off to PIL or put in a numeric > array > > > """ > #!/usr/bin/env python > from matplotlib.backends.backend_agg import FigureCanvasAgg > from matplotlib.figure import Figure > from matplotlib.axes import Subplot > from matplotlib.mlab import normpdf > from matplotlib.numerix import randn > > fig = Figure(figsize=(5,4), dpi=100) > ax = Subplot(fig, 111) > fig.add_axis(ax) > canvas = FigureCanvasAgg(fig) > > mu, sigma = 100, 15 > x = mu + sigma*randn(10000) > > # the histogram of the data > n, bins, patches = ax.hist(x, 50, normed=1) > > # add a 'best fit' line > y = normpdf( bins, mu, sigma) > line, = ax.plot(bins, y, 'r--') > line.set_linewidth(1) > > ax.set_xlabel('Smarts') > ax.set_ylabel('Probability') > ax.set_title(r'$\rm{Histogram of IQ: }\mu=100, \sigma=15$') > > ax.set_xlim( (40, 160)) > ax.set_ylim( (0, 0.03)) > > canvas.draw() > > s = canvas.tostring_rgb() # save this and convert to bitmap as needed > > # get the figure dimensions for creating bitmaps or numeric arrays, > # etc. > l,b,w,h = fig.bbox.get_bounds() > w, h = int(w), int(h) > > if 0: > # convert to a Numeric array > X = fromstring(s, UInt8) > X.shape = h, w, 3 > > if 0: > # pass off to PIL > import Image > im = Image.fromstring( "RGB", (w,h), s) > im.show() > |
From: John H. <jdh...@ac...> - 2004-05-03 20:44:09
|
>>>>> "Engelsma," == Engelsma, Dave <D.E...@La...> writes: Dave> Hello -- I'm having a dickens of a time getting this to Dave> work... I totally understand. A more extensive users guide is sorely needed. There is just so much to do on the development front that I haven't made time for it. Dave> I believe I understand what you mean about maintaining Dave> a list of FigureCanvasAggs and using string & bitmap Dave> methods to get the figures into my wxDialog. Dave> Where I'm stuck is in plotting a histogram to a Dave> FigureCanvasAgg... I've checked out your examples, but Dave> there doesn't appear to be anything that directly uses Dave> FigureCanvasAgg. I've tried many different ways (mostly Dave> based on the embedded_in_wx.py and histogram_demo.py Dave> examples). Dave> Based on the example "histogram_demo.py" could you Dave> please give some pointers as to how to plot the Dave> histogram to a FigureCanvasAgg? I think I can handle Dave> things after that... The example is included below. I'll add it to the examples dir for people who want to work directly with the Agg canvas and renderer. Let me know if you need some more help. Note that after you get the RGB string from Agg, you may want to destroy the figure and canvas if the figure is static to conserve memory. As repayment, if you have a nice screenshot showing your application in action, with the list box and figures etc, that I can include on the screenshots page, send it my way. from matplotlib.backends.backend_agg import FigureCanvasAgg from matplotlib.figure import Figure from matplotlib.axes import Subplot from matplotlib.mlab import normpdf from matplotlib.numerix import randn fig = Figure(figsize=(5,4), dpi=100) ax = Subplot(fig, 111) canvas = FigureCanvasAgg(fig) mu, sigma = 100, 15 x = mu + sigma*randn(10000) # the histogram of the data n, bins, patches = ax.hist(x, 50, normed=1) # add a 'best fit' line y = normpdf( bins, mu, sigma) line, = ax.plot(bins, y, 'r--') line.set_linewidth(1) ax.set_xlabel('Smarts') ax.set_ylabel('Probability') ax.set_title(r'$\rm{Histogram of IQ: }\mu=100, \sigma=15$') ax.set_xlim( (40, 160)) ax.set_ylim( (0, 0.03)) canvas.draw() s = canvas.tostring_rgb() # save this and convert to bitmap as needed |
From: John H. <jdh...@ac...> - 2004-05-03 21:14:50
|
>>>>> "John" == John Hunter <jdh...@ac...> writes: John> The example is included below. I'll add it to the examples John> dir for people who want to work directly with the Agg canvas John> and renderer. oops, left out a critical line - adding the axes to the figure. Here is a modified example, which also demonstrates initializing a Numeric array from the string and passing the image off to PIL.... """ This is an example that shows you how to work directly with the agg figure canvas to create a figure using the pythonic API. In this example, the contents of the agg canvas are extracted to a string, which can in turn be passed off to PIL or put in a numeric array """ #!/usr/bin/env python from matplotlib.backends.backend_agg import FigureCanvasAgg from matplotlib.figure import Figure from matplotlib.axes import Subplot from matplotlib.mlab import normpdf from matplotlib.numerix import randn fig = Figure(figsize=(5,4), dpi=100) ax = Subplot(fig, 111) fig.add_axis(ax) canvas = FigureCanvasAgg(fig) mu, sigma = 100, 15 x = mu + sigma*randn(10000) # the histogram of the data n, bins, patches = ax.hist(x, 50, normed=1) # add a 'best fit' line y = normpdf( bins, mu, sigma) line, = ax.plot(bins, y, 'r--') line.set_linewidth(1) ax.set_xlabel('Smarts') ax.set_ylabel('Probability') ax.set_title(r'$\rm{Histogram of IQ: }\mu=100, \sigma=15$') ax.set_xlim( (40, 160)) ax.set_ylim( (0, 0.03)) canvas.draw() s = canvas.tostring_rgb() # save this and convert to bitmap as needed # get the figure dimensions for creating bitmaps or numeric arrays, # etc. l,b,w,h = fig.bbox.get_bounds() w, h = int(w), int(h) if 0: # convert to a Numeric array X = fromstring(s, UInt8) X.shape = h, w, 3 if 0: # pass off to PIL import Image im = Image.fromstring( "RGB", (w,h), s) im.show() |