7

I have a numpy array and i'm trying to plot it with a scatter plot using matplotlib.

from matplotlib import pyplot as plt
from matplotlib import pylab as pl

pl.plot(matrix[:,0],matrix[:,1], 'ro')

This gives me something like : plot

Now i want to replace the red dots by a number correspondig to the index of the row in the numpy array. How could i do it ?

Thank you !

1 Answer 1

16

You can do this using plt.text:

from matplotlib import pyplot as plt

import numpy as np
N = 100
matrix = np.random.rand(N,2)

plt.plot(matrix[:,0],matrix[:,1], 'ro', alpha = 0.5)
for i in range(matrix.shape[0]):
    plt.text(matrix[i,0], matrix[i,1], str(i))

plt.show()

enter image description here

If you want to replace the red dots, then set alpha = 0.0:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Oh I didn't think about that ! Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.