Bar plot with timedelta as bar width

2024/10/14 20:19:18

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)
Answer

If type of your df.duration[0] is pandas.tslib.Timedelta and your timestamps are days apart you could use:

width = [x.days for x in df.duration]

and this will produce the chart.

Otherwise use total_seconds method as outlined in this answer

UPDATE:

If the data is hourly with timedeltas in minutes then one way to have the chart you want is like this:

import datetime as dt
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltdates = pd.date_range(start=dt.date(2014,10,22), periods=10, freq='H')
df = pd.DataFrame({'start': dates, 'duration': np.random.randint(1, 10, len(dates))}, columns=['start', 'duration'])
df['duration'] = df.duration.map(lambda x: pd.datetools.timedelta(0, 0, 0, 0, x))
df.ix[1, 1] = pd.datetools.timedelta(0, 0, 0, 0, 30) # To clearly see the effect at 01:00:00
width=[x.minutes/24.0/60.0 for x in df.duration] # mpl will treat x.minutes as days hense /24/60.
plt.bar(left=df.start, width=width, height=[1]*df.start.shape[0])
ax = plt.gca()
_ = plt.setp(ax.get_xticklabels(), rotation=45)

This produces a chart like this:

enter image description here

https://en.xdnf.cn/q/69371.html

Related Q&A

Take screenshot of second monitor with python on OSX

I am trying to make an ambient light system with Python. I have gotten pyscreenshot to save a screenshot correctly, but I cant figure out how to get it to screenshot my second monitor (if this is even …

Invoking the __call__ method of a superclass

http://code.google.com/p/python-hidden-markov/source/browse/trunk/Markov.pyContains a class HMM, which inherits from BayesianModel, which is a new-style class. Each has a __call__ method. HMMs __call__…

Efficent way to split a large text file in python [duplicate]

This question already has answers here:Sorting text file by using Python(3 answers)Closed 10 years ago.this is a previous question where to improve the time performance of a function in python i need t…

Creating a unique id in a python dataclass

I need a unique (unsigned int) id for my python data class. This is very similar to this so post, but without explicit ctors. import attr from attrs import field from itertools import count @attr.s(aut…

How to get all the models (one for each set of parameters) using GridSearchCV?

From my understanding: best_estimator_ provides the estimator with highest score; best_score_ provides the score of the selected estimator; cv_results_ may be exploited to get the scores of all estimat…

How do I perform deep equality comparisons of two lists of tuples?

I want to compare two lists of tuples:larry = [(1,a), (2, b)] moe = [(2, b), (1, a)]such that the order of the items in the list may differ. Are there library functions to do this ?>> deep_equal…

Metadata-generation-failed when trying to install pygame [duplicate]

This question already has answers here:Python pygame not installing(3 answers)Closed last year.Trying to install pygame on python 3.11 using the following command "pip install pygame" and I a…

Why such a big pickle of a sklearn decision tree (30K times bigger)?

Why pickling a sklearn decision tree can generate a pickle thousands times bigger (in terms of memory) than the original estimator? I ran into this issue at work where a random forest estimator (with …

Buffer size for reading UDP packets in Python

I am trying to find out / adjust the size of network buffers:import socketsock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)sock.getsockopt(socket.SOL_SOCKET,socket.SO_RCVBUF) 212992What on earth i…

Why does datetime give different timezone formats for the same timezone?

>>> now = datetime.datetime.now(pytz.timezone(Asia/Tokyo)) >>> dt = datetime(now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond, pytz.timezone(Asia/Tokyo)…