fig.legend method and axes.legend method behaves differently?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

fig.legend method and axes.legend method behaves differently?

Henry east

Hi,
I tried to make a plot with two subplots but only one legend since the two subplots express the same set of information.
I put the legend outside of the two subplots for better interpretation. I have found that by using fig.legend instead of
the ax.legend method. In the saved plot, the legend box are just cut off.

I tried to fix this by using bbox_extra_artists argument in the savefig method, following the instruction
here.
But it does not work. The following code illustrate the problem,

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

x = np.arange(-5, 5, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)

y3 = np.sin(2*x)
y4 = np.cos(2*x)

fig, (ax1, ax2) = plt.subplots(ncols=1, nrows=2, figsize=(10, 6))

ax1.plot(x, y1, label='sin')
ax1.plot(x, y2, label='cos')

ax2.plot(x, y3, label='sin')
ax2.plot(x, y4, label='cos')

handles, labels = ax1.get_legend_handles_labels()

# legend = fig.legend(handles, labels, loc='lower left', ncol=2, frameon=False,
#               bbox_to_anchor=(0.12, 0.88))
# plt.savefig('test.jpg', bbox_extra_artists=(legend,), bbox_inches='tight')

legend = ax1.legend(handles, labels, ncol=2, frameon=False,
                    loc='lower left', bbox_to_anchor=(-0.01, 1.0))
plt.savefig('test.jpg', bbox_inches='tight')

If I use the above code, the generated image is fine. But if I use the commented code instead, the legend is gone in the generated image.


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: fig.legend method and axes.legend method behaves differently?

Eric Firing
It looks like the bbox_to_anchor argument is causing the problem.  If
you try this:

legend = fig.legend(handles, labels,
                     loc='upper center',
                     ncol=2, frameon=False,)
plt.savefig('test.jpg', bbox_extra_artists=(legend,),
             bbox_inches='tight')

it works, with 'upper center' or 'lower center'.

Eric

On 2018/01/07 6:16 AM, Henry east wrote:

> Hi,
> I tried to make a plot with two subplots but only one legend since the
> two subplots express the same set of information.
> I put the legend outside of the two subplots for better interpretation.
> I have found that by using |fig.legend| instead of
> the |ax.legend| method. In the saved plot, the legend box are just cut off.
>
> I tried to fix this by using |bbox_extra_artists| argument in the
> |savefig| method, following the instruction
> here
> <https://stackoverflow.com/questions/10101700/moving-matplotlib-legend-outside-of-the-axis-makes-it-cutoff-by-the-figure-box>.
> But it does not work. The following code illustrate the problem,
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users