Do you know how I can remove vertical lines in my plot?
Y-axis:
[0, 0, 2, 2, 5, 5, 1, 1, 0, 0, 4, 4, 3, 3, 0, 0]
X-axis:
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
You can use plt.hlines . Example:
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')
Using a LineCollection
:
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.