0

My goal is to plot lines that represent certain days of the year. Note: I do not want to use a package like date time -- I am just trying to do this with the data points as integer values. So in the plot below, the event at y=3 lasts between day 123 to 189 and event at y=2 lasts between days 214 and 365.

Where I run into problems is with event at y=1, which should go from day 205 to 22 (that's what's in the data dataframe). However, the plot does not know that I am plotting days of the year (obviously) and so it stretches from day 0 to 205, which is wrong. Instead, it should start at 205, stretch to the right and then end at the value of 22. I've hand drawn in blue what it should look like.

example figure

Help?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data = pd.DataFrame(np.array([[3,123,189],[2,214,365],[1,205,22]]), columns=['name','start','end'])
plt.hlines(data['name'],data['start'],data['end'],linewidth=1)
plt.xlabel('Date')
plt.ylabel('Event')
2
  • The simplest thing is to make two events 1: [1, 205, 365] and [1, 1, 22]. If you are going to automate this, you need only check if start > end. Commented Oct 1, 2021 at 13:31
  • I have 1000s of rows of data, so if you could give some pseudo code that would be helpful! Commented Oct 1, 2021 at 14:27

1 Answer 1

1

How about this

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data = pd.DataFrame(np.array([[3,123,189],[2,214,365],[1,205,22]]), columns=['name','start','end'])

endstart = (data.index[data['start'] > data['end']].tolist())
startend = ([i for i in range(len(data)) if i not in endstart])

fig, ax = plt.subplots()

# plot startend
ax.hlines(data['name'][startend],data['start'][startend],data['end'][startend],linewidth=1)
# plot endstart
ax.hlines(data['name'][endstart],data['start'][endstart],365,linewidth=1)
ax.hlines(data['name'][endstart],0,data['end'][endstart],linewidth=1)

ax.set_xlabel('Date')
ax.set_ylabel('Event')

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I just had to make the following change and then the code worked: startend = ([i for i in (data.index) if i not in endstart])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.