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,

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: sanders@…3329…
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

------------------------------------------------------------------------------ The ultimate all-in-one performance toolkit: Intel® 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@…1220…sts.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users

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
···

sanders@…3329…
matplotlib-users@lists.sourceforge.net

legend.set_ncol(2)
ncol

http://p.sf.net/sfu/intel-dev2devfeb
Matplotlib-users@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/matplotlib-users

Bram,
Although I don’t quite see the point, I would simply suggest that you re-call ax.legend(ncol=2) , that would delete the previous and redraw a new one
from matplotlib doc: When the legend command is called, a new legend instance is created and old ones are removed from the axes.

Thomas

···

Thomas Lecocq

Geologist
Ph.D.Student (Seismology)
Royal Observatory of Belgium



Date: Tue, 8 Feb 2011 14:50:00 +0100
From: sanders@…3329…
To: thlecocq@…1836…954…
CC: matplotlib-users@lists.sourceforge.net
Subject: Re: [Matplotlib-users] set ncol for legend

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: sanders@…3329…
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





  ------------------------------------------------------------------------------

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@…564…net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

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

···

On Tue, Feb 8, 2011 at 7:50 AM, sanders <sanders@…3435…> 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: sanders@…3329…
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