1

I have a nx2 dimension array representing n points with their two coordinates x, y. Using pyplot, I would like to display the number n of my points instead of just the points with no way to know which is what.

I have found a way to legend my points but really what I would like is only the number.

How can I achieve this ?

2
  • Could be a duplicate, however I was interpreting the title and last sentence in the question such that user has already found a solution such as the one from the linked question, but does not like it. Improving the quesiton may help understanding if this is the case or not. Commented Jun 19, 2017 at 12:41
  • It is the case, I would like numbers showing up instead of points. Commented Jun 19, 2017 at 13:33

1 Answer 1

6

You may use plt.text to place the number as text in the plot. To have the number appear at the exact position of the coordinates, you may center align the text using ha="center", va="center".

import numpy as np; np.random.seed(2)
import matplotlib.pyplot as plt

xy = np.random.rand(10,2)

plt.figure()
for i, ((x,y),) in enumerate(zip(xy)):
    plt.text(x,y,i, ha="center", va="center")

plt.show()

enter image description here

In order to have the plot autoscale to the range where the values are, you may add an invisible scatter plot

x,y =zip(*xy)
plt.scatter(x,y, alpha=0) 

or, if numbers are really small, better an invisible plot

x,y =zip(*xy)
plt.plot(x,y, alpha=0.0) 
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you ! Your code works for me and this what I want except that I use a normal distribution so I have xy = np.random.normal(0, 1, (10, 2)) instead and see nothing displayed :/
The values are out of the plotting range. See updated answer.
The scaling seems to work except for very small values (10^-12). Do you know how to solve this problem ?
Which problem ?
I see. In that case better make an invisible plot instead of a scatter. I updated the answer.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.