creating a matplotlib scatter legend size related

前端 未结 6 2064
野性不改
野性不改 2020-12-04 20:46

I am looking for a way to include a (matplotlib) legend that describe the size of points in a scatter plot, as this could be related to another variable, like in this basic

6条回答
  •  -上瘾入骨i
    2020-12-04 20:50

    This'll also work, and I think it's a bit simpler:

    msizes = np.array([3, 4, 5, 6, 7, 8])
    
    l1, = plt.plot([],[], 'or', markersize=msizes[0])
    l2, = plt.plot([],[], 'or', markersize=msizes[1])
    l3, = plt.plot([],[], 'or', markersize=msizes[2])
    l4, = plt.plot([],[], 'or', markersize=msizes[3])
    
    labels = ['M3', 'M4', 'M5', 'M6']
    
    leg = plt.legend([l1, l2, l3, l4], labels, ncol=1, frameon=True, fontsize=12,
    handlelength=2, loc = 8, borderpad = 1.8,
    handletextpad=1, title='My Title', scatterpoints = 1)
    

    Taken from: Point size legends in matplotlib and basemap plots

提交回复
热议问题