Matplotlib scatter plot with different text at each data point

后端 未结 9 2188
挽巷
挽巷 2020-11-22 12:50

I am trying to make a scatter plot and annotate data points with different numbers from a list. So, for example, I want to plot y vs x and annotate

9条回答
  •  自闭症患者
    2020-11-22 13:38

    I would love to add that you can even use arrows /text boxes to annotate the labels. Here is what I mean:

    import random
    import matplotlib.pyplot as plt
    
    
    y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
    z = [0.15, 0.3, 0.45, 0.6, 0.75]
    n = [58, 651, 393, 203, 123]
    
    fig, ax = plt.subplots()
    ax.scatter(z, y)
    
    ax.annotate(n[0], (z[0], y[0]), xytext=(z[0]+0.05, y[0]+0.3), 
        arrowprops=dict(facecolor='red', shrink=0.05))
    
    ax.annotate(n[1], (z[1], y[1]), xytext=(z[1]-0.05, y[1]-0.3), 
        arrowprops = dict(  arrowstyle="->",
                            connectionstyle="angle3,angleA=0,angleB=-90"))
    
    ax.annotate(n[2], (z[2], y[2]), xytext=(z[2]-0.05, y[2]-0.3), 
        arrowprops = dict(arrowstyle="wedge,tail_width=0.5", alpha=0.1))
    
    ax.annotate(n[3], (z[3], y[3]), xytext=(z[3]+0.05, y[3]-0.2), 
        arrowprops = dict(arrowstyle="fancy"))
    
    ax.annotate(n[4], (z[4], y[4]), xytext=(z[4]-0.1, y[4]-0.2),
        bbox=dict(boxstyle="round", alpha=0.1), 
        arrowprops = dict(arrowstyle="simple"))
    
    plt.show()
    

    Which will generate the following graph:

提交回复
热议问题