Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: Draw lines from x axis to points

I have a bunch of points that I am trying to plot using matplotlib. For each point (a,b) I want to draw the line X = a for Y in [0,b]. Any idea how to do this?

like image 923
Graddy Avatar asked Dec 09 '11 06:12

Graddy


2 Answers

You just draw each line using the two endpoints. A vertical line X=a for Y in [0,b] has endpoints (x,y) = (a,0) and (a,b). So:

# make up some sample (a,b): format might be different to yours but you get the point.
import matplotlib.pyplot as plt
points = [ (1.,2.3), (2.,4.), (3.5,6.) ] # (a1,b1), (a2,b2), ...

plt.hold(True)
plt.xlim(0,4)  # set up the plot limits

for pt in points:
    # plot (x,y) pairs.
    # vertical line: 2 x,y pairs: (a,0) and (a,b)
    plt.plot( [pt[0],pt[0]], [0,pt[1]] )

plt.show()

Gives something like the following: drawing vertical lines

like image 196
mathematical.coffee Avatar answered Sep 21 '22 02:09

mathematical.coffee


Use a stem plot

The least cumbersome solution employs matplotlib.pyplot.stem

import matplotlib.pyplot as plt
x = [1. , 2., 3.5]
y = [2.3, 4., 6.]
plt.xlim(0,4)
plt.stem(x,y)
plt.show()

enter image description here

like image 35
Serge Stroobandt Avatar answered Sep 21 '22 02:09

Serge Stroobandt