creating a matplotlib scatter legend size related

前端 未结 6 2074
野性不改
野性不改 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条回答
  •  青春惊慌失措
    2020-12-04 21:11

    Building on mjp's and jpobst's answers, if you have more than two discrete sizes you can make a loop and include the labels in the call to plt.scatter():

    msizes = [3, 4, 5, 6, 7]
    markers = []
    for size in msizes:
       markers.append(plt.scatter([],[], s=size, label=size))
    
    plt.legend(handles=markers)
    

    Note that you can format the label using standard string formatting, such as label = ('M%d' %size) for the labels in mjp's answer.

提交回复
热议问题