I have a 1-D numpy array, which I want to plot and I wanted the plot marker to be a number which shows the location of the element. For example, if my array is [2.5,4,3]
then I want the plot to have the number 0 at the point (0,2.5), 1 at (1,4) and 2 at (2,3) and so on.
How to do this?
This part is easy with Matplotlib. Just call the plot() function and provide your x and y values. Calling the show() function outputs the plot visually.
The use of pylab might be discouraged (I had to look up what pylab was).
import matplotlib.pyplot as plt
xs = [0, 1, 2]
ys = [2.5, 4, 3]
plt.plot(xs, ys, "-o")
for x, y in zip(xs, ys):
plt.text(x, y, str(x), color="red", fontsize=12)
you need call pylab.text()
in a for loop:
import pylab as pl
xs = [0, 1, 2]
ys = [2.5, 4, 3]
pl.plot(xs, ys, "-o")
for x, y in zip(xs, ys):
pl.text(x, y, str(x), color="red", fontsize=12)
pl.margins(0.1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With