【发布时间】:2011-12-09 06:29:24
【问题描述】:
我有一堆点,我正在尝试使用 matplotlib 绘制。对于每个点 (a,b),我想在 [0,b] 中为 Y 绘制线 X = a。知道怎么做吗?
【问题讨论】:
标签: python matplotlib
我有一堆点,我正在尝试使用 matplotlib 绘制。对于每个点 (a,b),我想在 [0,b] 中为 Y 绘制线 X = a。知道怎么做吗?
【问题讨论】:
标签: python matplotlib
您只需使用两个端点绘制每条线。 [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()
提供如下内容:
【讨论】:
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()
【讨论】: