【问题标题】:Adding annotates to scatter plots - don't work向散点图添加注释 - 不起作用
【发布时间】:2022-01-02 03:27:58
【问题描述】:

我正在尝试为散点图添加注释,但它没有出现,我不明白为什么。

例子:

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()

结果:

如何在散点图中添加注释?

【问题讨论】:

  • 您没有在此块中明确创建 ax 对象。它从哪里来的? ax 实际上是在不同的数字上吗?

标签: python python-3.x matplotlib


【解决方案1】:

如果您将fig = figure(figsize=(16, 8), dpi=80) 替换为

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2021-03-07
    • 2012-05-09
    相关资源
    最近更新 更多