Re: [Matplotlib-users] savefig problem
Ben: That's what I did: plt.show() then plt.savefig(). I now know to reverse the order. Thanks! - Roy On Fri, Jul 29, 2011 at 7:32 PM, Benjamin Root wrote: > On Fri, Jul 29, 2011 at 6:19 PM, Roy Lowrance wrote: > >> I'm using matplotlib on ubuntu 11.04. >> >> I create a figure and an axes and then show it via plt.show(). >> >> From the window that plt.show() opens, I save the file to plot-3.png. This >> works as I can open the file with evince. >> >> However, when my program executes plt.savefig('plot-3.png'), something is >> saved as a file is created, but when I open the file with evince (or GIMP), >> I see just the canvas, not the figure. >> >> How do I save under program control? >> >> - Roy >> >> > There are a few common mistakes that might have happened. First, if you > did a plt.show() and then closed the displayed figure, and *then* saved the > figure, you will be saving the image of a new figure as you have already > "destroyed" the old figure. If this is the case, put the savefig command > *before* plt.show(). > > Another possibility is that another figure was somehow created between the > last plotting > function and the call to savefig(). plt.savefig() assumes the current > (active) figure, and so if another figure is accidentially created before > calling savefig(), then you will be saving a blank figure. > > I hope this helps, and let us know if you have further questions! > Ben Root > > -- Roy Lowrance home: 212 674 9777 mobile: 347 255 2544 -- Got Input? Slashdot Needs You. Take our quick survey online. Come on, we don't ask for help often. Plus, you'll get a chance to win $100 to spend on ThinkGeek. http://p.sf.net/sfu/slashdot-survey___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
[Matplotlib-users] Fwd: parametric line color
Oops. I meant to reply to the list. -Tony -- Forwarded message -- From: Tony Yu Date: Sat, Jul 30, 2011 at 10:46 AM Subject: Re: [Matplotlib-users] parametric line color To: Alan G Isaac On Sat, Jul 30, 2011 at 12:03 AM, Alan G Isaac wrote: > I have many lines to plot. > Each line has an associated parameter value. > I'd like to pick the color for each line > based on its parameter value by mapping > possible parameter values to the colors > in a color map (e.g., gist_rainbow), > retrieving colors one at a time (based > on the parameter value for a line). > > If two lines have the same parameter value, > I need them to have exactly the same color. > > Hints? > > Thanks, > Alan Isaac > > Here's a function to do what you want. (I haven't tested it much so it could be buggy) Best, -Tony # import matplotlib.pyplot as plt def make_color_manager(parameter_range, cmap='YlOrBr', start=0, stop=255): """Return color manager, which returns color based on parameter value. Parameters -- parameter_range : 2-tuple minimum and maximum value of parameter cmap : str name of a matplotlib colormap (see matplotlib.pyplot.cm) start, stop: int limit colormap to this range (0 <= start < stop <= 255) """ colormap = getattr(plt.cm, cmap) pmin, pmax = parameter_range def color_manager(val): """Return color based on parameter value `val`.""" assert pmin <= val <= pmax val_norm = (val - pmin) * float(stop - start) / (pmax - pmin) idx = int(val_norm) + start return colormap(idx) return color_manager if __name__ == '__main__': cm = make_color_manager((5, 10), start=100) plt.plot([0, 1], color=cm(5)) plt.plot([0.5, 0.5], color=cm(7.5)) plt.plot([1, 0], color=cm(10)) plt.legend(('val = 5', 'val = 7.5', 'val = 10')) plt.show() -- Got Input? Slashdot Needs You. Take our quick survey online. Come on, we don't ask for help often. Plus, you'll get a chance to win $100 to spend on ThinkGeek. http://p.sf.net/sfu/slashdot-survey___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] parametric line color
On 07/29/2011 06:03 PM, Alan G Isaac wrote: > I have many lines to plot. > Each line has an associated parameter value. > I'd like to pick the color for each line > based on its parameter value by mapping > possible parameter values to the colors > in a color map (e.g., gist_rainbow), > retrieving colors one at a time (based > on the parameter value for a line). > > If two lines have the same parameter value, > I need them to have exactly the same color. > > Hints? Tony's reply may be the right approach for you; but an alternative, if you are plotting a set of lines all at once, is to use a LineCollection, which takes a colors kwarg. (Like all Collections, a LineCollection includes the cm.ScalarMappable mixin.) See http://matplotlib.sourceforge.net/examples/pylab_examples/line_collection2.html but note that the lines within the collection don't have to have the same number of points. Also, Tony's method could be modified easily to use a norm (e.g., an instance of colors.Normalize or of a subclass) in addition to a cmap. Eric > > Thanks, > Alan Isaac > > -- > Got Input? Slashdot Needs You. > Take our quick survey online. Come on, we don't ask for help often. > Plus, you'll get a chance to win $100 to spend on ThinkGeek. > http://p.sf.net/sfu/slashdot-survey > ___ > Matplotlib-users mailing list > Matplotlib-users@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/matplotlib-users -- Got Input? Slashdot Needs You. Take our quick survey online. Come on, we don't ask for help often. Plus, you'll get a chance to win $100 to spend on ThinkGeek. http://p.sf.net/sfu/slashdot-survey ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users