clechristophe
clechristophe

Reputation: 183

Matplotlib Scatter plot with numpy row index as marker

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 !

Upvotes: 6

Views: 12205

Answers (1)

p-robot
p-robot

Reputation: 4904

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

Upvotes: 15

Related Questions