在 matplotlib 散点图中标记点 [英] Labeling points in matplotlib scatterplot

查看:67
本文介绍了在 matplotlib 散点图中标记点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题不是重复的,我不想绘制数字而不是点,我想在我的点旁边绘制数字.

我正在使用 matplotlib 绘制绘图.需要绘制三个点 [[3,9],[4,8],[5,4]]

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

将 matplotlib.pyplot 导入为 pltallPoints = [[3,9],[4,8],[5,4]]f, 图 = plt.subplots(1)对于范围内的 i (3):xPoint = allPoints[i][0]yPoint = allPoints[i][1]图.plot(xPoint, yPoint, 'bo')

产生这个情节:

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

基于

Edit: This question is not a duplicate, I don't want to plot numbers instead of points, I wanted to plot numbers beside my points.

I'm making a plot using matplotlib. There are three points to plot [[3,9],[4,8],[5,4]]

I can easily make a scatterplot with them

import matplotlib.pyplot as plt

allPoints = [[3,9],[4,8],[5,4]]

f, diagram = plt.subplots(1)

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

That produces this plot:

I want to label each point with numbers 1,2,3.

Based on this SO answer I tried to use annotate to label each point.

import matplotlib.pyplot as plt

allPoints = [[1,3,9],[2,4,8],[3,5,4]]

f, 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))

This produces a blank plot. I'm closely following the other answer but it isn't producing any plot. Where have I made a mistake?

解决方案

You can do that:

import matplotlib.pyplot as plt

points = [[3,9],[4,8],[5,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()

这篇关于在 matplotlib 散点图中标记点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆