2016-07-03 122 views
1

我想繪製從我的數據集的每個點到零軸的垂直線。目前情節如下:在python中繪製從數據點到零軸的垂直線

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() 

如何繪製垂直線?我的文檔axvline,貫穿全圖這不過確實垂直線發現我不希望它:

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

回答

2

可以使用plt.plot(c1, c2)繪製從c1 = [x1, y1]任意行c2 = [x2, y2]。因此,爲了繪製這些線你可以做

xcoords = [0.22058956, 0.33088437, 2.20589566] 
for xc in xcoords: 
    plt.plot([xc, 0], [xc, Y-VALUE]) 
2

axvline有paramater ymax是調整yrange,直到其V線應該持續的百分之一。在當y軸是緊情況下,該解決方案可以是這樣的:

for i in range(len(times)): 
    plt.axvline(values[i], ymax=float(times[i])/max(times), color='b') 
0

使用stem情節

的至少麻煩的解決方案採用matplotlib.pyplot.stem

import matplotlib.pyplot as plt 
values = [0.0, 0.2, 0.0, 0.4, 1.4, 0.5] 
times = [1, 4, 10, 12, 14, 20] 
plt.stem(times,values) 
plt.show() 

enter image description here