On Tue, Feb 8, 2011 at 7:50 AM, sanders <sand...@knmi.nl> wrote:
> I realize that I have not been clear enough.
>
> I have already created a legend instance in my_own_plot_function, for
> example, a legend with one column by default:
>
> fig = plt.figure()
> ax = fig.add_subplot(111)
> my_own_plot_function(ax, data) # gives, for example, one column legend
> by default
>
> So ax is an axes instance containing the legend.
>
> Incidentally, after inspecting the automatically created plots, I want a
> particular figure to have a two column legend. I would like to do this
> without adding an extra kwarg for the number of columns to
> my_own_plot_function. It should be possible to do something like this:
>
>
> legend = ax.get_legend()
> *legend.set_ncol(2)* # something like this
>
> Once again, thanks for any help!
>
> Bram
>
>
>
> On 02/08/2011 12:35 PM, Thomas Lecocq wrote:
>
> Bram,
>
>
> fig = plt.figure()
> ax = fig.add_subplot(111)
> plot1 = plot.plot(X,Y,label='1')
> plot2 = plot.plot(X,Y,label='2')
> ...
> plotN = plot.plot(X,Y,label='N')
>
> legend = plt.legend(ncol=2)
>
> should work...
>
> so, for your "own_plot_function", you have to return the legend and set it
> accordingly...
>
> Thomas
>
>
>
> **********************
> Thomas Lecocq
> Geologist
> Ph.D.Student (Seismology)
> Royal Observatory of Belgium
> **********************
>
>
>
> ------------------------------
> Date: Tue, 8 Feb 2011 11:25:58 +0100
> From: sand...@knmi.nl
> To: matplotlib-users@lists.sourceforge.net
> Subject: [Matplotlib-users] set ncol for legend
>
> Hi,
>
> I want to update the number of columns in my legend. How should I do that?
>
> I'm looking for something like:
>
> fig = plt.figure()
> ax = fig.add_subplot(111)
> my_own_plot_function(ax, data) # gives, for example, one column legend
> by default
> legend = ax.get_legend()
> *legend.set_ncol(2)* # something like this
>
>
> However, *ncol* is not in the legend.properties() list for properties to
> be set through legend.set.
>
>
> Thanks for any help,
> Bram
>
>
>
Bram,
As a point of style (and something I got myself caught in when I first
started using matplotlib), it is not a good idea to create a single plotting
function that does everything. In some of my original programs using
matplotlib, I would create large functions that would plot not only a radar
image, the county map, rivers and roads, but would also produce the colorbar
and label the axes and put out a title for the plot. While this seemed like
a good idea at the time because I was able to produce the one kind of image
I wanted, this then became a problem when I needed plots that did not have a
colorbar, or a title, or something else.
So, while it is often nice to have a convenience function that can produce a
particular style plot for you in a single call, it is still a good idea to
break up that call into smaller methods that handle parts of the plot. So,
when you need a plot that has a 2-column legend as opposed to one, you can
still do the regular plot, but then call plt.legend(ncols=2), or anything
else for that matter.
I hope this helps!
Ben Root
------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users