Home > Enterprise >  Plotting events on a line graph
Plotting events on a line graph

Time:04-11

I am trying to visualise rain events using a data contained in a dataframe. the idea seems very simple, but the execution seems to be impossible! here is a part of the dataframe:

         start_time             end_time      duration      br_open_total
0   2022-01-01 10:00:00 2022-01-01 19:00:00     9.0       0.2540000563879943
1   2022-01-02 00:00:00 2022-01-02 10:00:00    10.0       1.0160002255520624
2   2022-01-02 17:00:00 2022-01-03 02:00:00     9.0       0.7620001691640113
3   2022-01-03 02:00:00 2022-01-04 12:00:00    34.0      10.668002368296513
4   2022-01-07 21:00:00 2022-01-08 06:00:00     9.0       0.2540000563879943
5   2022-01-16 05:00:00 2022-01-16 20:00:00    15.0       0.5080001127760454
6   2022-01-19 04:00:00 2022-01-19 17:00:00    13.0       0.7620001691640255
7   2022-01-21 14:00:00 2022-01-22 00:00:00    10.0       1.5240003383280751
8   2022-01-27 02:00:00 2022-01-27 16:00:00    14.0       3.0480006766561503
9   2022-02-01 12:00:00 2022-02-01 21:00:00     9.0       0.2540000563880126
10  2022-02-03 05:00:00 2022-02-03 15:00:00    10.0       0.5080001127760251

What I want to do is have a plot with time on the x axis, and the value of the 'br_open_total' on the y axis.

I can draw what I want it to look like, see below:

graph example

I apologise for the simplicity of the drawing, but I think it explains what I want to do. How do I do this, and then repeat for other dataframes on the same plot. I have tried staircase, matplotlib.pyplot.stair and others with no success.

It seems such a simple concept!

Edit 1:

Tried Joswin K J's answer with the actual data, and got this: attempt 1

The event at 02-12 11:00 should be 112 hours duration, but the bar is the same width as all the others.

Edit2: Tried Mozway's answer and got this: enter image description here

Still doesn't show width of each event, and doesn't discretise the events either

CodePudding user response:

Try this:

import matplotlib.pyplot as plt

for ind,row in df.iterrows():
    plt.plot(pd.Series([row['start_time'],row['end_time']]),pd.Series([row['br_open_total'],row['br_open_total']]),color='b')
    plt.plot(pd.Series([row['start_time'],row['start_time']]),pd.Series([0,row['br_open_total']]),color='b')
    plt.plot(pd.Series([row['end_time'],row['end_time']]),pd.Series([0,row['br_open_total']]),color='b')
plt.xticks(rotation=90)

Result:

enter image description here

CodePudding user response:

You can use a step plot

reshaped data:

    duration  br_open_total    variable                date
0        9.0       0.254000  start_time 2022-01-01 10:00:00
11       9.0       0.000000    end_time 2022-01-01 19:00:00
1       10.0       1.016000  start_time 2022-01-02 00:00:00
12      10.0       0.000000    end_time 2022-01-02 10:00:00
2        9.0       0.762000  start_time 2022-01-02 17:00:00
3       34.0      10.668002  start_time 2022-01-03 02:00:00
14      34.0       0.000000    end_time 2022-01-04 12:00:00
4        9.0       0.254000  start_time 2022-01-07 21:00:00
15       9.0       0.000000    end_time 2022-01-08 06:00:00
5       15.0       0.508000  start_time 2022-01-16 05:00:00
16      15.0       0.000000    end_time 2022-01-16 20:00:00
6       13.0       0.762000  start_time 2022-01-19 04:00:00
17      13.0       0.000000    end_time 2022-01-19 17:00:00
7       10.0       1.524000  start_time 2022-01-21 14:00:00
18      10.0       0.000000    end_time 2022-01-22 00:00:00
8       14.0       3.048001  start_time 2022-01-27 02:00:00
19      14.0       0.000000    end_time 2022-01-27 16:00:00
9        9.0       0.254000  start_time 2022-02-01 12:00:00
20       9.0       0.000000    end_time 2022-02-01 21:00:00
10      10.0       0.508000  start_time 2022-02-03 05:00:00
21      10.0       0.000000    end_time 2022-02-03 15:00:00
  • Related