简体   繁体   中英

Combine two matplotlib markers into one in legend

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

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"])

在此处输入图像描述

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

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM