Python 使用matplotlib删除时间历史图中的垂直线
你知道如何删除绘图中的垂直线吗 Y轴:Python 使用matplotlib删除时间历史图中的垂直线,python,matplotlib,Python,Matplotlib,你知道如何删除绘图中的垂直线吗 Y轴: [0, 0, 2, 2, 5, 5, 1, 1, 0, 0, 4, 4, 3, 3, 0, 0] X轴: 2016-04-18 00:00:00 2017-10-24 00:00:00 2017-10-24 00:00:00 2017-11-27 00:00:00 2017-11-27 00:00:00 2017-11-30 00:00:00 2017-11-30 00:00:00 2018-03-08 00:00:00 2018-03-08 00:00
[0, 0, 2, 2, 5, 5, 1, 1, 0, 0, 4, 4, 3, 3, 0, 0]
X轴:
2016-04-18 00:00:00
2017-10-24 00:00:00
2017-10-24 00:00:00
2017-11-27 00:00:00
2017-11-27 00:00:00
2017-11-30 00:00:00
2017-11-30 00:00:00
2018-03-08 00:00:00
2018-03-08 00:00:00
2018-06-13 00:00:00
2018-06-13 00:00:00
2018-07-09 00:00:00
2018-07-09 00:00:00
2018-10-29 00:00:00
2018-10-29 00:00:00
2019-11-18 15:00:00
您可以使用。例如:
y=[0, 0, 2, 2, 5, 5, 1, 1, 0, 0, 4, 4, 3, 3, 0, 0]
x=list(range(len(y)))
for (x1, x2),(y1, y2) in zip(zip(x, x[1:]),zip(y, y[1:])):
if y1==y2:
ax.hlines(y=y1, xmin=x1, xmax=x2, linewidth=4, color='r')
使用
行集合
:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import datestr2num
from matplotlib.collections import LineCollection
y = [0, 0, 2, 2, 5, 5, 1, 1, 0, 0, 4, 4, 3, 3, 0, 0]
x ="""2016-04-18 00:00:00
2017-10-24 00:00:00
2017-10-24 00:00:00
2017-11-27 00:00:00
2017-11-27 00:00:00
2017-11-30 00:00:00
2017-11-30 00:00:00
2018-03-08 00:00:00
2018-03-08 00:00:00
2018-06-13 00:00:00
2018-06-13 00:00:00
2018-07-09 00:00:00
2018-07-09 00:00:00
2018-10-29 00:00:00
2018-10-29 00:00:00
2019-11-18 15:00:00"""
x = [datestr2num(l) for l in x.splitlines()]
segs = np.column_stack((x,y)).reshape(len(x)//2, 2, 2)
lc = LineCollection(segs)
fig, ax = plt.subplots()
ax.add_collection(lc)
ax.autoscale()
ax.xaxis_date()
plt.show()
问题是您希望删除垂直线,而不是水平线?所以散点图不是一个选项,是吗?不幸的是不是,我真的只需要水平线来显示一个适当的时间历史,因为它是预期的。