python - 在python中绘制从数据点到零轴的垂直线

标签 python matplotlib plot

我想绘制从我的数据集的每个点到零轴的垂直线。目前情节看起来像这样:

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)

最佳答案

使用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

关于python - 在python中绘制从数据点到零轴的垂直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38172903/

相关文章:

matlab - 同时将绘图属性应用于所有 MATLAB 子图

javascript - 在 PythonAnywhere 上的 html 文件中运行脚本

Python For 循环和 BeautifulSoup 结果转换为 CSV

python - Python 中的 if 和 else 条件问题

python绘制一维波动方程(初学者)

python - 如何绘制 3D 曲面?

Python 从长度变化的数组中刷新 Pyplot 线

Matlab:绘制点并使它们可点击以显示有关它的信息

python - 如何对齐 QGraphicsTextItem 的网格?

python - 不准确的袖扣 plotly