Legend outside plot?

Good evening,

I've been trying to find a way to move the legend outside
the plot so it doesn't cover it up. I've seen some things
online but I can quite get them to work (probably just my
lack of knowledge about matplotlib).

The section of code creating the plot looks like,

       fig = plt.figure()
       ax = fig.add_subplot(1,1,1);

       p1 = ax.bar(ind, IO_Time_Plot, width, color="r", align='center');
       p2 = ax.bar(ind, Diff_Plot, width, color="y", bottom=IO_Time_Plot, align='center');

       ax.set_ylabel('Time (secs)');
       ax.set_title('Elapsed Time and IO Time',fontstyle='italic');
       ax.set_xticks(ind);

       group_labels = [];
       for item in names:
          group_labels.append(item);

       ax.set_xticklabels(group_labels);

       fig.autofmt_xdate();

       #ax.legend( bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.);
       ax.legend( (p1[0], p2[0]), ('IO Time', 'Total Elapsed Time'));

You can see my attempt at moving the legend outside the bounding box
but when I try this, the legend never appears.

TIA!

Jeff

I hate to be the first one to comment on this post but I forgot to give
the error message and version of matplotlib. The error is,

Traceback (most recent call last):
   File "./multi_file_test_2.py", line 460, in <module>
     ax.legend( (p1[0], p2[0]), ('IO Time', 'Total Elapsed Time'), bbox_to_anchor=(1.05, 1), loc=2);
   File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line 3617, in legend
     self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'bbox_to_anchor'

The version of matplotlib I'm using is, 0.98.3-4ubuntu1

Jeff

···

   Good evening,

I've been trying to find a way to move the legend outside
the plot so it doesn't cover it up. I've seen some things
online but I can quite get them to work (probably just my
lack of knowledge about matplotlib).

The section of code creating the plot looks like,

        fig = plt.figure()
        ax = fig.add_subplot(1,1,1);

        p1 = ax.bar(ind, IO_Time_Plot, width, color="r", align='center');
        p2 = ax.bar(ind, Diff_Plot, width, color="y",
bottom=IO_Time_Plot, align='center');

        ax.set_ylabel('Time (secs)');
        ax.set_title('Elapsed Time and IO Time',fontstyle='italic');
        ax.set_xticks(ind);

        group_labels = ;
        for item in names:
           group_labels.append(item);

        ax.set_xticklabels(group_labels);

        fig.autofmt_xdate();

        #ax.legend( bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.);
        ax.legend( (p1[0], p2[0]), ('IO Time', 'Total Elapsed Time'));

You can see my attempt at moving the legend outside the bounding box
but when I try this, the legend never appears.

TIA!

Jeff

------------------------------------------------------------------------------
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
matplotlib-users List Signup and Options

Hi,

I would simply try to attach the legend to the figure object instead of the axis.

···

On Thu, Feb 10, 2011 at 4:20 PM, Jeff Layton <laytonjb@…872…> wrote:

Good evening,

I’ve been trying to find a way to move the legend outside
the plot so it doesn’t cover it up. I’ve seen some things

online but I can quite get them to work (probably just my
lack of knowledge about matplotlib).

The section of code creating the plot looks like,

  fig = plt.figure()
  ax = fig.add_subplot(1,1,1);


  p1 = ax.bar(ind, IO_Time_Plot, width, color="r", align='center');
  p2 = ax.bar(ind, Diff_Plot, width, color="y",

bottom=IO_Time_Plot, align=‘center’);

  ax.set_ylabel('Time (secs)');

  ax.set_title('Elapsed Time and IO Time',fontstyle='italic');
  ax.set_xticks(ind);

  group_labels = [];
  for item in names:
     group_labels.append(item);

  ax.set_xticklabels(group_labels);


  fig.autofmt_xdate();

  #ax.legend( bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.);
  ax.legend( (p1[0], p2[0]), ('IO Time', 'Total Elapsed Time'));

You can see my attempt at moving the legend outside the bounding box

but when I try this, the legend never appears.

TIA!

Jeff


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


Gökhan

Matplotlib is great! I created all my figures in this paper [0] using matplotlib. I am impressed with the quality you get. I attach the plotting script for fig. 9, which does exactly what you want, I believe. There is a lot of other junk in there, so look for the "legend" function, it all happens in one line.

Cheers,
Paul.

[0] http://arxiv.org/abs/1011.3399, the paper should appear in phys. rev. B soon.

plot.py (2.48 KB)

I hate to be the first one to comment on this post but I forgot to give
the error message and version of matplotlib. The error is,

Traceback (most recent call last):
File "./multi_file_test_2.py", line 460, in <module>
ax.legend( (p1[0], p2[0]), ('IO Time', 'Total Elapsed Time'),
bbox_to_anchor=(1.05, 1), loc=2);
File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line
3617, in legend
self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'bbox_to_anchor'

The version of matplotlib I'm using is, 0.98.3-4ubuntu1

Jeff

I'm quite sure that the *bbox_to_anchor* parameter is introduced later
than 0.98.
I think you have a few options,

* use "loc" parameter.
* see if "figlegend" helps.
* create a empty axes just for the purpose of drawing the legend.
* upgrade to 1.0.1 and I highly recommend it if you can.

Regards,

-JJ

···

On Fri, Feb 11, 2011 at 8:38 AM, Jeff Layton <laytonjb@...872...> wrote:

Good evening,

I've been trying to find a way to move the legend outside
the plot so it doesn't cover it up. I've seen some things
online but I can quite get them to work (probably just my
lack of knowledge about matplotlib).

The section of code creating the plot looks like,

    fig = plt\.figure\(\)
    ax = fig\.add\_subplot\(1,1,1\);

    p1 = ax\.bar\(ind, IO\_Time\_Plot, width, color=&quot;r&quot;, align=&#39;center&#39;\);
    p2 = ax\.bar\(ind, Diff\_Plot, width, color=&quot;y&quot;,

bottom=IO_Time_Plot, align='center');

    ax\.set\_ylabel\(&#39;Time \(secs\)&#39;\);
    ax\.set\_title\(&#39;Elapsed Time and IO Time&#39;,fontstyle=&#39;italic&#39;\);
    ax\.set\_xticks\(ind\);

    group\_labels = \[\];
    for item in names:
       group\_labels\.append\(item\);

    ax\.set\_xticklabels\(group\_labels\);

    fig\.autofmt\_xdate\(\);

    \#ax\.legend\( bbox\_to\_anchor=\(1\.05, 1\), loc=2, borderaxespad=0\.\);
    ax\.legend\( \(p1\[0\], p2\[0\]\), \(&#39;IO Time&#39;, &#39;Total Elapsed Time&#39;\)\);

You can see my attempt at moving the legend outside the bounding box
but when I try this, the legend never appears.

TIA!

Jeff

------------------------------------------------------------------------------
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
matplotlib-users List Signup and Options

------------------------------------------------------------------------------
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
matplotlib-users List Signup and Options

I was worried about that. I will try to update my system but it's
an old Ubuntu 8.10 system. Looks like I have an excuse to
upgrade Ubuntu :slight_smile:

Thanks!

Jeff

···

On 02/11/2011 03:48 AM, Jae-Joon Lee wrote:

On Fri, Feb 11, 2011 at 8:38 AM, Jeff Layton<laytonjb@...872...> wrote:

  I hate to be the first one to comment on this post but I forgot to give
the error message and version of matplotlib. The error is,

Traceback (most recent call last):
   File "./multi_file_test_2.py", line 460, in<module>
     ax.legend( (p1[0], p2[0]), ('IO Time', 'Total Elapsed Time'),
bbox_to_anchor=(1.05, 1), loc=2);
   File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line
3617, in legend
     self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'bbox_to_anchor'

The version of matplotlib I'm using is, 0.98.3-4ubuntu1

Jeff

I'm quite sure that the *bbox_to_anchor* parameter is introduced later
than 0.98.
I think you have a few options,

  * use "loc" parameter.
  * see if "figlegend" helps.
  * create a empty axes just for the purpose of drawing the legend.
  * upgrade to 1.0.1 and I highly recommend it if you can.

Regards,

-JJ