【问题标题】:How remove data points from text annotate?如何从文本注释中删除数据点?
【发布时间】:2018-04-24 15:03:58
【问题描述】:

以下脚本生成带有注释数据点的散点图。我想从图中删除圆形标记并只显示标签。

fig, ax = Plot.subplots()
ax.scatter(Y0_mean, Y1_mean)
for i, txt in enumerate(features.playerCountry.unique()):
    country_name = countries_code[countries_code.CountryCode == txt] 
                   ['ctr'].values[0].lower()
    ax.annotate(country_name, (Y0_mean[i], Y1_mean[i]), xytext=(Y0_mean[i], 
               Y1_mean[i]), size=5)

ax.legend(fontsize=8)
fig.savefig(figPath + 'LocationAwareMeanFeatures_ctr'+str(lr), dpi=300)

【问题讨论】:

  • 不要打电话给ax.scatter?或者,如果您确实想在调用 ax.scatter 后删除一个点,请参阅下面的副本
  • @DavidG 当我删除 ax.scatter() 时不会显示任何内容。我还尝试了 ax.remove() 显示空白情节。

标签: python matplotlib annotate


【解决方案1】:

有 2 个选项。 1) 不要打电话给ax.scatter。这确实意味着您必须自己设置坐标轴限制以查看这些点。

y=[2.56422, 3.77284,3.52623,3.51468,3.02199]
x=[0.15, 0.3, 0.45, 0.6, 0.75]
n=[58,651,393,203,123]

fig, ax = plt.subplots()
# ax.scatter(x, y)

for i, txt in enumerate(n):
    ax.annotate(txt, (x[i],y[i]))

ax.set_ylim(2.5,4)

plt.show()

或选项 2) 调用 ax.scatter 但删除通过以下操作添加的 LineCollections:

y=[2.56422, 3.77284,3.52623,3.51468,3.02199]
x=[0.15, 0.3, 0.45, 0.6, 0.75]
n=[58,651,393,203,123]

fig, ax = plt.subplots()
points = ax.scatter(x, y)

for i, txt in enumerate(n):
    ax.annotate(txt, (x[i],y[i]))

points.remove()

plt.show()

两种方法给出相同的结果(前提是您在选项 1 中设置与在选项 2 中相同的轴限制):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    相关资源
    最近更新 更多