0

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()
losttime
  • 1
  • 1
  • Refer to this answer : https://stackoverflow.com/a/43551544/17457846 – HMH1013 Apr 06 '23 at 15:44
  • @ХМН1013 the problem is that I have dates at the bottom – losttime Apr 06 '23 at 17:00
  • @losttime The main problems seems to be you have strings at the bottom instead of dates. As your post is missing reproducible code and test data, your question currently isn't suitable for StackOverflow. Please [edit](https://stackoverflow.com/posts/75951291/edit) your post. – JohanC Apr 06 '23 at 17:41
  • Note the **Time Series Axis** section of this [answer](https://stackoverflow.com/q/33382619/7758804). `date = dt.datetime.strptime(date, "%d.%m.%Y")` converts to strings. you must plot the data with datetime dtypes, add the horizonal lines, and then convert format of the xticks. – Trenton McKinney Apr 06 '23 at 22:36

0 Answers0