Wizytor
Wizytor

Reputation: 105

How to plot time interval data in python?

I have a data frame df1 containing below information:

        Eng      Install date  Car
0       eng1       3/6/2010    car1
1       eng1     10/25/2010    car1
2       eng1       1/1/2014    car2
3       eng2       1/1/2011    car3
4       eng3       1/1/2014    car1
5       eng4       1/1/2015    car1
6       eng4       1/5/2016    car3
7       eng4       5/8/2017    car2
8       eng5       2/3/2010    car1
9       eng5       1/1/2011    car4

I would like to plot bar intervals showing one car history (engine rotation within selected car with time ranges)

This is the example history for car1:

start       end         Engine No.
2/3/2010    3/6/2010    eng5
3/6/2010    1/1/2014    eng1
1/1/2014    1/1/2015    eng3
1/1/2015    Today       eng4

Upvotes: 4

Views: 4621

Answers (3)

Mykola Zotko
Mykola Zotko

Reputation: 17794

You can use the timeline plot in plotly:

import plotly.express as px

px.timeline(df, x_start='start', x_end='end', color='Engine')

enter image description here

Upvotes: 0

cosmic_inquiry
cosmic_inquiry

Reputation: 2674

This is a little different approach. For the labels use ax.annotate. I made the labels the dates, not knowing if you wanted those as the labels or the engine number. I'm sure you've got it from here:

import pandas as pd
df = pd.DataFrame({'Eng':['eng1','eng1','eng1','eng2','eng3','eng4','eng4','eng4','eng5','eng5'],
'Install date':['3/6/2010','10/25/2010','1/1/2014','1/1/2011','1/1/2014','1/1/2015','1/5/2016','5/8/2017','2/3/2010','1/1/2011'],
'Car':['car1','car1','car2','car3','car1','car1','car3','car2','car1','car4']})

# df
# Out[47]:
#     Eng Install date   Car
# 0  eng1     3/6/2010  car1
# 1  eng1   10/25/2010  car1
# 2  eng1     1/1/2014  car2
# 3  eng2     1/1/2011  car3
# 4  eng3     1/1/2014  car1
# 5  eng4     1/1/2015  car1
# 6  eng4     1/5/2016  car3
# 7  eng4     5/8/2017  car2
# 8  eng5     2/3/2010  car1
# 9  eng5     1/1/2011  car4
df['Install date'] = pd.to_datetime(df['Install date'])
for car in df.Car.unique():
    most_recent_eng = df.loc[df.Car == car].groupby('Install date').max().tail(1).Eng.iloc[0]
    new_df = df.loc[df.Car == car].append(pd.DataFrame({'Car':[car],'Eng':[most_recent_eng],'Install date':[pd.to_datetime('now')]}), sort=False)
    piv = new_df.pivot_table(values='Car',columns='Eng',index='Install date', aggfunc='count')
    piv = piv.reindex(columns=piv.ffill().sum().sort_values(ascending=False).index).ffill()
    ax = piv.plot(marker='o', linestyle='-',linewidth=2, title=car, ms=4)
    j = 0.005
    i = 1
    for date, eng in zip(new_df[:-1]['Install date'].tolist(),new_df[:-1]['Eng'].tolist()):
        ax.annotate(date.strftime("%Y-%m-%d"), (date, 1.00), xycoords='data',xytext=(date, 1.00+i*j), textcoords='data',
            arrowprops=dict(arrowstyle="->"),
            )
        j+=0.005
        i = -1*i

Makes:

enter image description here

Upvotes: 3

cosmic_inquiry
cosmic_inquiry

Reputation: 2674

It's not quite what you asked for but hopefully this will suffice:

df = pd.DataFrame({'Eng':['eng1','eng1','eng1','eng2','eng3','eng4','eng4','eng4','eng5','eng5'],
'Install date':['3/6/2010','10/25/2010','1/1/2014','1/1/2011','1/1/2014','1/1/2015','1/5/2016','5/8/2017','2/3/2010','1/1/2011'],
'Car':['car1','car1','car2','car3','car1','car1','car3','car2','car1','car4']})

df
Out[47]:
    Eng Install date   Car
0  eng1     3/6/2010  car1
1  eng1   10/25/2010  car1
2  eng1     1/1/2014  car2
3  eng2     1/1/2011  car3
4  eng3     1/1/2014  car1
5  eng4     1/1/2015  car1
6  eng4     1/5/2016  car3
7  eng4     5/8/2017  car2
8  eng5     2/3/2010  car1
9  eng5     1/1/2011  car4
df['Install date'] = pd.to_datetime(df['Install date'])
for car in df.Car.unique():
    most_recent_eng = df.loc[df.Car == car].groupby('Install date').max().tail(1).Eng.iloc[0]
    new_df = df.loc[df.Car == car].append(pd.DataFrame({'Car':[car],'Eng':[most_recent_eng],'Install date':[pd.to_datetime('now')]}), sort=False)
    new_df.pivot_table(values='Car',columns='Eng',index='Install date', aggfunc='count').resample('1d').ffill().plot(kind='line',linewidth=10, title=car)

Result:

Result:

Upvotes: 1

Related Questions