2

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

darthV
  • 355
  • 1
  • 3
  • 16
  • 4
    Does this answer your question? [How to make two markers share the same label in the legend using matplotlib?](https://stackoverflow.com/questions/31478077/how-to-make-two-markers-share-the-same-label-in-the-legend-using-matplotlib) – Sahan Dissanayaka Jul 25 '20 at 13:17
  • https://matplotlib.org/3.1.1/tutorials/intermediate/legend_guide.html This site has the specific answer. – darthV Jul 26 '20 at 08:21

1 Answers1

3

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?

darthV
  • 355
  • 1
  • 3
  • 16