[Matplotlib-users] Making a solid legend

vincent.adrien at gmail.com vincent.adrien at gmail.com
Thu Mar 23 11:01:53 EDT 2017


I am not an expert of the legend codebase but it seems like you can achieve this
with the following snippet heavily insprired from this [SO
thread](http://stackoverflow.com/questions/25829736/matplotlib-how-to-adjust-zorder-of-second-legend)
and the Matplotlib [tutorial on
legend](http://matplotlib.org/users/legend_guide.html). In a nutshell, you build
a legend-like artist similar to the one of the first axes and display it into
the second axis.

```
import matplotlib.pyplot as plt

plt.rcParams['legend.framealpha'] = 1.0
plt.rcParams['legend.facecolor'] = '0.95'

fig, ax1 = plt.subplots()
ax1.plot([0, 1], [0, 2], label="Plot 1")

ax2 = ax1.twinx()
ax2.plot([0, 1], [2.2, 0], "--", label="Plot 2")
ax2.legend(bbox_to_anchor=(1, 1))

# Manually add *leg1*, the legend of *ax1*, to *ax2* so it displays "on top"
handles1, labels1 = ax1.get_legend_handles_labels()
leg1 = plt.legend(handles1, labels1, loc="upper left", bbox_to_anchor=(0, 1))
ax2.add_artist(leg1)
# `loc="upper left"` (or anything but "best") is highly recommended, as it
# determines which part of the bbox is anchored to the desired coordinates.
# "best" (the new default btw.) could lead to weird behaviors when changing
# just a bit the anchor position...

# Just in case: here is a simple way to display a single common legend.
# Note the it will replace the previous legend on *ax2*.
#handles1, labels1 = ax1.get_legend_handles_labels()
#handles2, labels2 = ax2.get_legend_handles_labels()
#ax2.legend(handles1+handles2, labels1+labels2, loc="lower center")

plt.grid()
plt.show()
```

Best,
Adrien

PS: I made a mistake in my previous email. The correct rcParams key are
'legend.framealpha' and *'legend.facecolor'* (not 'legend.framecolor').

On 23/03/2017 15:10, Florian Lindner wrote:
> Hello,
> 
> 
> Am 23.03.2017 um 14:38 schrieb vincent.adrien at gmail.com:
>> Hello Florian,
>>
>> A `get_frame()` call seems to be missing in your example. Try with
>> ```
>> leg.get_frame().set_facecolor('r')
>> ```
>>
>> Note that you can also directly set this when calling `legend`:
>> ```
>> import matplotlib.pyplot as plt
>>
>> fig, ax = plt.subplots()
>> ax.plot([0, 1], label='Plot 1')
>>
>> leg = ax.legend(loc='upper left', framealpha=1.0, facecolor='red')
>>
>> ax.grid()
>> plt.show()
>> ```
>>
>> If you want to fix this for every plot you do, you may want to tweak your
>> matplotlibrc file or at least the related rcParams in your scrpit, which are
>> 'legend.framecolor' and 'legend.framealpha'.
> 
> Oh, cool, that is what I was looking for too.
> 
> I found out that my original issue is also related to twinx:
> 
> import matplotlib.pyplot as plt
> 
> fig, ax1 = plt.subplots()
> ax1.plot([0, 1], [0, 2], label = "Plot 1 with some more text")
> ax1.legend(framealpha = 1, bbox_to_anchor = (0.1, 1))
> 
> ax2 = ax1.twinx()
> ax2.plot([0, 1], [2.2, 0], "--", label = "Plot 2 with some more text")
> ax2.legend(framealpha = 1, bbox_to_anchor = (1.1,1))
> 
> plt.grid()
> plt.show()
> 
> The second legend is over the right y-axis, the first legend is not over the left y-axis.
> 
> Can I set legends in a way they are always above any element?
> 
> Thanks,
> Florian
> 
> 
> 
>> Best regards,
>> Adrien
>>
>> On 23/03/2017 14:24, Florian Lindner wrote:
>>> Hello,
>>>
>>> I try to make the legend non-translucent, having a solid color and laying above the plot itself.
>>>
>>> That's what I try:
>>>
>>>
>>> import matplotlib.pyplot as plt
>>>
>>> fig, ax1 = plt.subplots()
>>> ax1.plot([0, 1], [0, 2], label = "Plot 1")
>>>
>>> leg = ax1.legend(loc = 1)
>>> leg.get_frame().set_facecolor("r")
>>> leg.set_alpha(0)
>>>
>>> plt.grid()
>>> plt.show()
>>>
>>>
>>> (you may have to pan the plot)
>>>
>>> Taken from:
>>> http://matplotlib.org/examples/api/legend_demo.html
>>> http://matplotlib.org/1.3.0/examples/pylab_examples/legend_translucent.html
>>>
>>> The set_alpha seems to have no effect at all. facecolor works, but it is always translucent.
>>>
>>> Thanks,
>>> Florian
>>>
>>> _______________________________________________
>>> Matplotlib-users mailing list
>>> Matplotlib-users at python.org
>>> https://mail.python.org/mailman/listinfo/matplotlib-users
>>>
> 
> 
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
> 



More information about the Matplotlib-users mailing list