beginh beginh - 1 year ago 148
Python Question

Plot vertical lines from datapoints to zero axis in python

I want to plot vertical lines from each point of my dataset to the zero axis. At the moment plot looks like this:

values = [0.0, 0.2, 0.0, 0.4, 1.4, 0.5]
times = [1, 4, 10, 12, 14, 20]
plt.plot(values,times,'o')
plt.show()


How to plot the vertical lines? I found in the documentation axvline, which however does vertical lines through the whole graph and I do not want it:

xcoords = [0.22058956, 0.33088437, 2.20589566]
for xc in xcoords:
plt.axvline(x=xc)

Answer Source

You can plot an arbitrary line from c1 = [x1, y1] to c2 = [x2, y2] using plt.plot(c1, c2). So to plot these lines you can just do

xcoords = [0.22058956, 0.33088437, 2.20589566]
for xc in xcoords:
    plt.plot([xc, 0], [xc, Y-VALUE])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download