I have a pandas dataframe with a column containing timestamps (start
) and another column containing timedeltas (duration
) to indicate duration.
I'm trying to plot a bar chart showing these durations with their left edge at the timestamps. I haven't found anyway online of doing it. Is there any way to achieve this?
So far, this is what I have, which doesn't work:
height = np.ones(df.shape[0])width = [x for x in df['duration']]plt.bar(left=df['start'], height=height, width=width)
Edit: I have updated the width as follows but that also doesn't solve this problem:
width = [x.total_seconds()/(60*1200) for x in df['duration']]
I'm interested in knowing whether datetime.timedelta
objects can be used in width
, since datetime
objects can be used as x-axis. And if not, what alternatives are there?
Edit #2:
This may not be the exact answer to my question, but it solved the purpose I had in mind. For whoever interested, this is the approach I took finally (I used start
and duration
to make end
for this purpose):
for i in range(df.shape[0]):plt.axvspan(df.ix[i, 'start'], df.ix[i, 'end'], facecolor='g', alpha=0.3)plt.axvline(x=df.ix[i, 'start'], ymin=0.0, ymax=1.0, color='r', linewidth=1)plt.axvline(x=df.ix[i, 'end'], ymin=0.0, ymax=1.0, color='r', linewidth=1)