Home > Enterprise >  Plotting timedelta values gives out of scope axis
Plotting timedelta values gives out of scope axis

Time:11-22

I have a dataframe that looks like this:

      commits     commitdates       Age (in days)  Year-Month  server_version  
0         97        2021-04-07       75 days    2021-04          v1       
1         20        2021-05-31       43 days    2021-05          v3       
2         54        2021-06-21       54 days    2021-06          v0.1      
3         100       2021-06-18       75 days    2021-06          v2.1.0       
4         12        2020-12-06       22 days    2020-12           Nan    

I want to plot the Age(in days) which is of type timedelta64[ns] , by the number of commits of type int64 by the server_versions on the x axis.

I tried to do this using plotly, but the age values are a bit strange and don't seem correct, I just want them to be displayed starting from 0 to all the rest, like normal int values, I am not sure how can I fix this. enter image description here

My code is this:

import plotly.express as px
fig = px.scatter(final_api, x="Version", y="Age (in days)", color="commits", width =1000, height = 800)
fig.show()

I am a bit new to plotly, any help will be appreciated.

CodePudding user response:

All you have to do is to convert your timedelta64 to days and add days as suffix for the yaxis, I answer your question based on this random data:

import pandas as pd
import numpy as np

s = pd.Series(pd.timedelta_range(start='1 days', end='75 days'))
df = pd.DataFrame()
df['commits'] = np.random.randint(100, size=len(s))
df['Version'] = 'v' df['commits'].astype(str)
df['Age (in days)'] = s

fig = px.scatter(df, x="Version", y=df['Age (in days)'].dt.days, color="commits", width =1000, height = 800)
fig.update_yaxes(ticksuffix =  " days")
fig.show()

Output

enter image description here

  • Related