微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – matplotlib散点图中的标记点

参见英文答案 > Display numbers instead of points using pyplot                                     1个
>             matplotlib scatter plot with different text at each data point                                    4个
编辑:这个问题不重复,我不想绘制数字而不是点数,我想在我的点旁边绘制数字.

我正在使用matplotlib创建一个情节.绘制[[3,9],[4,8],[5,4]]有三点

我可以轻松地用它们制作散点图

import matplotlib.pyplot as plt

allPoints = [[3,4]]

f,diagram = plt.subplots(1)

for i in range(3):
    xPoint =  allPoints[i][0]
    yPoint =  allPoints[i][1]
    diagram.plot(xPoint,yPoint,'bo')

这产生了这个情节:

plot

我想用数字1,2,3标记每个点.

基于this SO回答我试图使用注释来标记每个点.

import matplotlib.pyplot as plt

allPoints = [[1,3,[2,4,[3,5,diagram = plt.subplots(1)

for i in range(3):
    pointRefNumber = allPoints[i][0]
    xPoint =  allPoints[i][1]
    yPoint =  allPoints[i][2]
    diagram.annotate(pointRefNumber,(xPoint,yPoint))

这会产生一个空白图.我正在密切关注另一个答案,但它没有产生任何情节.我哪里弄错了?

解决方法

你可以这样做:

import matplotlib.pyplot as plt

points = [[3,4]]

for i in range(len(points)):
    x = points[i][0]
    y = points[i][1]
    plt.plot(x,y,'bo')
    plt.text(x * (1 + 0.01),y * (1 + 0.01),i,fontsize=12)

plt.xlim((0,10))
plt.ylim((0,10))
plt.show()

scatter_plot

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐