Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot 1-d data at given y-value with pylab

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?

like image 740
Nihar Sarangi Avatar asked Sep 08 '11 17:09

Nihar Sarangi


People also ask

How do I plot a line in Pylab?

To plot a line plot in Matplotlib, you use the generic plot() function from the PyPlot instance. There's no specific lineplot() function - the generic one automatically plots using lines or markers. This results in much the same line plot as before, as the values of x are inferred.

How do you plot a one direction plot in python?

The quickest way to generate a simple, 1-D plot is using the pyplot. plot() function. the figure and axis objects and plots the data onto them. the figure and axes objects automatically, and is the simplest way to create a plot.


2 Answers

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() 
like image 99
K.-Michael Aye Avatar answered Sep 30 '22 10:09

K.-Michael Aye


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") 
like image 33
Staven Avatar answered Sep 30 '22 09:09

Staven