Marking points in matplotlib chartplot

Edit: This question is not a duplicate, I don't want to display numbers instead of dots, I would like to plot numbers next to my dots.

I am making a plot using matplotlib. Adjust three points [[3.9], [4.8], [5.4]]

I can easily make a scatter diagram 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')

      

This creates this graph:

plot

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

Based on this SO answer, I tried to use annotation to mark 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 creates an empty site. I am following the other answer closely, but it does not create any plot. Where did I go wrong?

+3


source to share


2 answers


You can do it:

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

+1


source


I solved my question. I needed to plot points and then annotate them, there is no inline plot in annotation.

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)

      



Edited to add fontsize parameter the same as in Gregoux's answer

+2


source







All Articles