Matplotlib:从x轴到点绘制线条

时间:2011-12-09 06:29:25

标签: python matplotlib

我有一堆我想用matplotlib绘制的点。对于每个点(a,b),我想在[0,b]中为Y绘制线X = a。知道怎么做吗?

2 个答案:

答案 0 :(得分:10)

您只需使用两个端点绘制每一行。 [0,b]中的Y的垂直线X = a具有端点(x,y)=(a,0)和(a,b)。 所以:

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

给出如下内容: drawing vertical lines

答案 1 :(得分:4)

使用stem

最简单的解决方案使用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