I need to divide the graph into 10 parts, while the lines should last up to the graph. What now enter image description here What I want enter image description here
I tried to find the closest date to the value, but it looks very crooked. I also tried to find solutions, but there were no works with dates anywhere
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime as dt
import csvdates = []
Y = []
date = 0
with open('data.txt', 'r') as datafile:
plotting = csv.reader(datafile, delimiter=',')
for ROWS in datafile.readlines():
dates.append(ROWS.split(' ')[0])
Y.append(int(ROWS.split(' ')[1]))
date = ROWS.split(' ')[0]
date = dt.datetime.strptime(date, "%d.%m.%Y")
date = date.year
date = str(date)
fig, ax = plt.subplots()
X = [dt.datetime.strptime(d,'%d.%m.%Y').date() for d in dates]
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.plot(X, Y, "-o")
plt.xlim(min(X), max(X))
# plt.ylim(min(Y), max(Y))
y_min = min(Y)
y_max = max(Y)
step = 0
res = y_max
old_step = (y_max - y_min) / 9
new_date = dt.datetime.strptime('31.05.2007', '%d.%m.%Y')
with open('data.txt', 'r') as datafile:
for i in range(10):
k = 0
res = res - step
max_date = min(Y, key=lambda x: abs(res - abs(x)))
for j in Y:
if Y[k] == max_date:
new_date = X[k]
k += 1
ax.hlines(res, min(X), new_date, color='black', linestyle='--')
if res == y_max:
step = old_step
plt.gcf().autofmt_xdate()
ax.grid()
plt.title('График спада паводка '+date+' г.')
plt.xlabel('T')
plt.ylabel('Q м\u00B3/c')
plt.show()