Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change size/alpha of markers in the legend box of matplotlib

What is the most convenient way to enlarge and set the alpha value of the markers (back to 1.0) in the legend box? I'm also happy with big coloured boxes.

import matplotlib.pyplot as plt
import numpy as np

n = 100000
s1 = np.random.normal(0, 0.05, n)
s2 = np.random.normal(0, 0.08, n)
ys = np.linspace(0, 1, n)

plt.plot(s1, ys, ',', label='data1', alpha=0.1)
plt.plot(s2, ys, ',', label='data2', alpha=0.1)
plt.legend(bbox_to_anchor=(1.005, 1), loc=2, borderaxespad=0.)

enter image description here

like image 688
tamasgal Avatar asked Feb 04 '16 11:02

tamasgal


People also ask

How do I change the alpha in Matplotlib?

You can update the alpha value of an existing Line2D , using the set_alpha method. The idea would be to plot the line once and then update the alpha in the loop.

How do I resize the legend box in Matplotlib?

Create line1 and line2 using two lists with different line widths. To place a legend on the figure and to adjust the size of legend box, use borderpad=2 in legend() method. To display the figure, use show() method.

What is alpha value in Matplotlib?

Matplotlib allows you to regulate the transparency of a graph plot using the alpha attribute. By default, alpha=1. If you would like to form the graph plot more transparent, then you'll make alpha but 1, such as 0.5 or 0.25.

How to adjust the size of a Matplotlib legend box?

To adjust the size of a matplotlib legend box, we can use borderpad arguments in the legend method. Create line1 and line2 using two lists with different line widths. To place a legend on the figure and to adjust the size of legend box, use borderpad=2 in legend () method.

How to adjust marker size in Matplotlib in Python?

In this article, we are going to see how to adjust marker size in Matplotlib in Python. We can adjust marker size in plots of matplotlib either by specifying the size of the marker in either plot method or scatter method while plotting the graph. plot () method used to plot the graph between 2 data points.

How do you plot a legend with markerscale in Python?

The legend() function takes a markerscale parameter which allows you to scale up the marker sizes in the legend. Our original marker sizes were 1.5 so to scale them up to 9 we would want to scale them by a factor of 6 (because 1.5*6=9). This allows us to simply plot our legend with.

How to adjust the size of marker in a plot?

plt.plot (data1, data2, marker=".") Example 2: To adjust the size of marker in a plot use markersize parameter to plot method to adjust the marker size. Here in this example, we adjusted the marker size to size 20 such that the maker size is increased from its standard size.


4 Answers

For the size you can include the keyword markerscale=## in the call to legend and that will make the markers bigger (or smaller).

import matplotlib.pyplot as plt 
import numpy as np
fig = plt.figure(1)
fig.clf()

x1,y1 = 4.*randn(10000), randn(10000)
x2,y2 = randn(10000), 4.*randn(10000)
ax = [fig.add_subplot(121+c) for c in range(2)]


ax[0].plot(x1, y1, 'bx',ms=.1,label='blue x')
ax[0].plot(x2, y2, 'r^',ms=.1,label='red ^')
ax[0].legend(loc='best')

ax[1].plot(x1, y1, 'bx',ms=.1,label='blue x')
ax[1].plot(x2, y2, 'r^',ms=.1,label='red ^')
ax[1].legend(loc='best', markerscale=40)

The output from the code

like image 191
CCHaggerty Avatar answered Oct 17 '22 17:10

CCHaggerty


We can use the handler_map option to .legend() to define a custom function to update the alpha or marker for all Line2D instances in the legend. This method has the advantage that it gets the legend markers correct first time, they do not need to be modified afterwards, and fixes issues where the original legend markers can sometimes still be seen.

This method makes use of HandlerLine2D from the matplotlib.legend_handler module. I'm not aware of a way to do this without adding the extra import.

A complete script would look like this:

import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerLine2D
import numpy as np

# Generate data
n = 100000
s1 = np.random.normal(0, 0.05, n)
s2 = np.random.normal(0, 0.08, n)
ys = np.linspace(0, 1, n)

# Create figure and plot the data
fig, ax = plt.subplots()
ax.plot(s1, ys, ',', label='data1', alpha=0.1)
ax.plot(s2, ys, ',', label='data2', alpha=0.1)

def change_alpha(handle, original):
    ''' Change the alpha and marker style of the legend handles '''
    handle.update_from(original)
    handle.set_alpha(1)
    handle.set_marker('.')

# Add the legend, and set the handler map to use the change_alpha function
ax.legend(bbox_to_anchor=(1.005, 1), loc=2, borderaxespad=0.,
          handler_map={plt.Line2D: HandlerLine2D(update_func=change_alpha)})

plt.show()

enter image description here


Note, below is my original answer. I have left it here for posterity as is may work for some use cases, but has the problem that when you change the alpha and markers, it actually creates new instances on the legend, and does not remove the old ones, so both can still be visible. I would recommend the method above in most cases.

If you name your legend, you can then iterate over the lines contained within it. For example:

leg=plt.legend(bbox_to_anchor=(1.005, 1), loc=2, borderaxespad=0.)

for l in leg.get_lines():
    l.set_alpha(1)
    l.set_marker('.')

note, you also have to set the marker again. I suggest setting it to . rather than , here, to make it a little more visible enter image description here

like image 38
tmdavison Avatar answered Oct 17 '22 18:10

tmdavison


leg = plt.legend()    
for lh in leg.legendHandles: 
    lh.set_alpha(1)

credit to https://izziswift.com/set-legend-symbol-opacity-with-matplotlib/

like image 9
Max Podkorytov Avatar answered Oct 17 '22 17:10

Max Podkorytov


for me the trick was to use the right property:

leg = axs.legend()

for l in leg.get_lines():
    l._legmarker.set_markersize(6)
like image 4
IljaBek Avatar answered Oct 17 '22 17:10

IljaBek