matplotlib散点中的标记点

2024-03-29 09:55:15 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:这个问题不是重复的,我不想画数字而不是点,我想画数字旁边的点。

我正在用matplotlib做一个绘图。有三个点要绘制[[3,9],[4,8],[5,4]]

我可以很容易地用它们画一个散点图

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

产生这个情节的:

plot

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

基于this所以我尝试使用annotate来标记每个点。

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

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


Tags: inimportformatplotlibasrangeplt数字
2条回答

你可以这样做:

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

scatter_plot

我解决了我自己的问题。我需要打印点,然后对它们进行注释,注释没有内置的打印功能。

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.plot(xPoint, yPoint, 'bo')
    diagram.annotate(nodeRefNumber, (xPoint, yPoint), fontsize=12)

编辑后添加fontsize选项,就像Gregoux的回答一样

相关问题 更多 >