Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using numbers as matplotlib plot markers

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?

like image 372
lovespeed Avatar asked Oct 28 '13 11:10

lovespeed


People also ask

How do I plot numbers in matplotlib?

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.


2 Answers

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)
like image 79
Marshies Avatar answered Nov 12 '22 03:11

Marshies


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)

enter image description here

like image 14
HYRY Avatar answered Nov 12 '22 02:11

HYRY