1
votes

So I have the following challenge: I have a list of data from 2018 to July 2020 as DateTime I have turnover attached to each date positions What I want to achieve is a line plot that starts from the same origo point and shows the development over the past years. Currently, x axis shows 2018,2019,2020 separately.

I cumulated the turnover for each year like this: for 2018:

df_2018=df.loc['2018-01-01':'2018-12-31']
df_2018["Turnovercum"]=df_2018["Turnover"].cumsum()
Line_2018=df_2018["Turnovercum"]
Line_2018

I did the same exercise for 2019 and 2020

then I plotted it like this:

fig = go.FigureWidget()
fig.add_trace(go.Scatter(x=Line_2020.index, y=Line_2020,
                    mode='lines',
                    name='Turnover 2020'))
fig.add_trace(go.Scatter(x=Line_2019.index, y=Line_2019,
                    mode='lines',
                    name='Turnover 2019'))
fig.add_trace(go.Scatter(x=Line_2018.index, y=Line_2018,
                    mode='lines',
                    name='Turnover 2018'))

Do you have any suggestions on how to plot it in a way that all the 3 lines are starting from the same origo?This is how it looks like at this moment

Any help or links where to look this up is much appreciated!

1

1 Answers

0
votes

You would need to translate your x-axis:

fig = go.FigureWidget()
fig.add_trace(go.Scatter(x=Line_2020.index-Line_2020.index.min(), y=Line_2020,
                    mode='lines',
                    name='Turnover 2020'))
fig.add_trace(go.Scatter(x=Line_2019.index-Line_2019.index.min(), y=Line_2019,
                    mode='lines',
                    name='Turnover 2019'))
fig.add_trace(go.Scatter(x=Line_2018.index-Line_2018.index.min(), y=Line_2018,
                    mode='lines',
                    name='Turnover 2018'))

I suppose your index is already of a datetime type, if not you can simply add:

df_2018.index=df_2018.index.astype('datetime64[ns]')