2017-07-09 379 views
3

編輯:這個問題不是重複的,我不想繪製數字而不是點,我想繪製我的點旁邊的數字。matplotlib scatterplot中的標記點

我正在使用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 SO我試着用註解來標記每個點。

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

這會產生一個空白圖。我正在密切關注其他答案,但它沒有產生任何陰謀。我在哪裏犯了一個錯誤?

+0

既然你已經知道如何繪製點,你已經知道如何標記點,唯一未解決的問題是爲什麼只有*註釋的情節保持空白。這在第一個重複問題中得到了解答。對於標記點的一般情況,我添加了另一個副本。 – ImportanceOfBeingErnest

+0

@ImportanceOfBeingErnest我不知道如何繪製標記的點。我認爲.annotate()功能會繪製和標記點。對我來說這是有道理的,因爲我指定了座標和標籤,但我錯了。 – Hugh

回答

1

你可以這樣做:

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

+0

這樣做效果更好,使用.annotate()的文字太小,因此能夠增加尺寸 – Hugh

2

我解決了我自己的問題。我需要繪製點然後對它們進行註釋,註釋沒有繪製內置圖。

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) 

編輯添加的字體大小的選項,就像在Gregoux的回答