代码之家  ›  专栏  ›  技术社区  ›  user3668129

向散点图添加注释-不起作用

  •  0
  • user3668129  · 技术社区  · 3 年前

    例子:

    x = [ 6.343056,  -2.5713196 , 6.0487113 ,  10.894953  , 13.541282    , 3.64124    ,-1.7216474 , -4.916564 , -1.2961074 , 6.835421 ]
    y = [11.965563,   -3.6742558,  12.373212,    9.714097 ,  -0.56607884 , 10.242817  ,0.7512899  , 2.1867976 ,  3.8110814 , 12.3270445]
    t = [9, 6, 6, 4, 1, 9, 3, 9, 0, 9]
    
    
    figure(figsize=(16, 8), dpi=80)
    plt.scatter(x, y, c=t, cmap='viridis', alpha=0.3);
    for i, tag in enumerate(t):
        
        ax.annotate("{}".format(tag), (x[i], y[i]),
                     textcoords="offset points", 
                     xytext=(0,10),
                     ha='center')
    plt.colorbar();
    plt.show()
    

    结果:

    enter image description here

    1 回复  |  直到 3 年前
        1
  •  1
  •   cotrane    3 年前

    fig = figure(figsize=(16, 8), dpi=80) 对于

    fig, ax = plt.subplots(figsize=(16, 8), dpi=80)
    

    它会起作用的。您尚未在上面的示例中定义要注释的轴。