39

I want to plot the data points that are in a 1-D array just along the horizontal axis [edit: at a given y-value], like in this plot:

http://static.inky.ws/image/644/image.jpg

How can I do this with pylab?

K.-Michael Aye
  • 5,465
  • 6
  • 44
  • 56
Nihar Sarangi
  • 4,845
  • 8
  • 27
  • 32

3 Answers3

40

Staven already edited his post to include how to plot the values along y-value 1, but he was using Python lists.

A variant that should be faster (although I did not measure it) only uses numpy arrays:

import numpy as np
import matplotlib.pyplot as pp
val = 0. # this is the value where you want the data to appear on the y-axis.
ar = np.arange(10) # just as an example array
pp.plot(ar, np.zeros_like(ar) + val, 'x')
pp.show()

As a nice-to-use function that offers all usual matplotlib refinements via kwargs this would be:

def plot_at_y(arr, val, **kwargs):
    pp.plot(arr, np.zeros_like(arr) + val, 'x', **kwargs)
    pp.show()
K.-Michael Aye
  • 5,465
  • 6
  • 44
  • 56
25

This will plot the array "ar":

import matplotlib.pyplot as pp
ar = [1, 2, 3, 8, 4, 5]
pp.plot(ar)
pp.show()

If you are using ipython, you can start it with the "-pylab" option and it will import numpy and matplotlib automatically on startup, so you just need to write:

ar = [1, 2, 3, 8, 4, 5]
plot(ar)

To do a scatter plot with the y coordinate set to 1:

plot(ar, len(ar) * [1], "x")
Staven
  • 3,113
  • 16
  • 20
  • Hi, Thanks for the answer. This is the way I tried the first time, but may be I was not able to explain my question properly. The plot it draws is a continuous lines made by connecting points in ar. I have an 1-D array of 800 data values distributed in (-1,6). I just want to plot the points on a constant Y (http://static.inky.ws/image/644/image.jpg) but with the method you explained am getting something like this (http://static.inky.ws/image/645/image.jpg). – Nihar Sarangi Sep 09 '11 at 09:12
  • the first plot I got is with matlab, I want to do a similar plot with matplotlib. – Nihar Sarangi Sep 09 '11 at 09:17
0
X = np.arange(10)

plt.scatter( X, [0] * X.shape[0])

Click on the link to check the plot

gboffi
  • 22,939
  • 8
  • 54
  • 85
  • If one wants to plot at a different _y_ level, `Yo = ... ; plt.scatter( X, [Yo] * X.shape[0])` --- maybe you want to [edit] your answer to incorporate my suggestion. – gboffi May 26 '23 at 13:38