Combine two matplotlib markers into one in legend

Viewed 1146

To have a custom marker, I made two scatter plots with same data points but different markers. Thus by plotting one marker on top of the other I get the look of a new custom marker. Now I want to use it in legend. Is there a way I can use two markers one on top of the other in legend and show them as a single marker.

Edit: The question is not regarding how to share the same label for two different markers, but how to plot one marker on top of other in the legend

1 Answers

Using a tuple of markers answers the question;


    from numpy.random import randn
    m=np.random.uniform(size=10)
    x=np.arange(0,10,1)
    y=x**2
    fig, ax = plt.subplots(1,1)
    
    blue_dot = ax.scatter(x[:5],y[:5], s=m*100, color='b')

    red_dot = ax.scatter(x[5:],y[5:], s=200*m, color='r')
    black_cross = ax.scatter(x[5:],y[5:], s=400*m, marker='+', color='k')
    
    lgnd = ax.legend([blue_dot, (red_dot, black_cross)], ["Blue Circle", "Red Circle and Black Cross"])

enter image description here

Now I want to change the size of the markers in the legend so that all the markers of equal size. For that, I have tried adding this to above code.

lgnd.legendHandles[0]._sizes = [200]
lgnd.legendHandles[1]._sizes = [200] # this is affecting the size of red_dot only

enter image description here

How do I change the size of black_cross as well in the legend?

Related