Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 !

like image 270
clechristophe Avatar asked Mar 02 '17 11:03

clechristophe


1 Answers

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

like image 158
p-robot Avatar answered Sep 30 '22 15:09

p-robot