Match size of markers between plot and legend

Hi all,

I have different (X,Y) data series drawn as scatter plots on one figure. Within each series, the marker size is set to different values according to the rank of the data point within its series (the 1st point in the series is shown with a smaller marker than the 2nd, which itself is shown with a smaller marker than the 3rd and so on…).

I want to show in the legend which marker size correspond to which rank. I managed to set the marker size in the legend handles to the same values as those on the graph. I also set the markerscale keyword of the legend() function to 1, aiming to have the markers be the same size in both legend and graph.

However, the markers displayed in the legend handles are significantly bigger than those in the graph. Here is a code snippet replicating my issue.

import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D

rd.seed(12345)

fig,ax = plt.subplots()

# Number of time steps in each series
nb_steps = 5
t = np.linspace(1,nb_steps,nb_steps)

# Series 1
x1 = rd.random(nb_steps)
y1 = rd.random(nb_steps)

# Series 2
x2 = 1+2*rd.random(nb_steps)
y2 = 1+rd.random(nb_steps)

# Plotting with increasing marker size within series
marker_sizes = (t+3)**2
ax.scatter(x1,y1, marker = '+', s = marker_sizes)
ax.scatter(x2,y2, marker = '+', s = marker_sizes)

# Writing marker size legend
handles = []

for size, step in zip(marker_sizes, t):
    handles.append(Line2D([0], [0], marker='+', lw = 0, color='k', markersize=size, label=step))

ax.legend(handles = handles, markerscale=1)
plt.show()

And here is the output figure.
out

I’ll be grateful to anyone willing to look into this, and hopefully resolve this issue. I’d also be happy to provide additional info if needed.
Thanks in advance!