Droptdown with mullti selectable years

Hi guys I’m trying to make ich bar plot for sales.

I have a dropdown where i can select multiple years .

and the y axis of the bar is Sales Volume and the x axis should be Week or Month

If i select year 2019 and 2020
i want for example for year 2019 week 1 and year 2020 week 1 a separated war next to each other that i can compare both depens on how many years I choosed
My Dataframe is defined as follow:
branch, year, month, week, assignments, sales volume My Columnheaders
1 , 2020, 4 , 15 , 500 , 500.000,00
Picture:

and im working not with plotly express cause i need two different y axis for a better scaling:
fig = make_subplots(specs=[[{“secondary_y”: True}]])

or is it possible to display the sales volume of 2019 for each week and then all weeks of 2020:

for example display 2019 week1-53 and then 2020 week 1-52 like a timeline if you know what I mean.

At the moment I have all week 1 bars next to each other and so on.

Is there any way to solve my problem?

Thank you in advance.

Greeting Mae

Hi Mae.

I am not sure I completely understand what you want, but maybe this is it?

import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

data = px.data.stocks()[['date', 'GOOG']]
data['year'] = pd.to_datetime(data['date']).dt.year.astype(str)
data['month'] = pd.to_datetime(data['date']).dt.month
data['ym'] = pd.to_datetime(data['date']).apply(lambda x: str(x.year)+'_'+f'{x.month:02}')

data.drop_duplicates(['year', 'month'], keep='last', inplace=True)

selected_years = ['2018', '2019']

fig3 = make_subplots(specs=[[{"secondary_y": True}]])

fig3.add_trace(
    go.Bar(
        x=data[data.year == selected_years[0]]['month'], 
        y=data[data.year == selected_years[0]]['GOOG'], 
        name=selected_years[0],
        offsetgroup=1
        ),
    secondary_y=False,
)

fig3.add_trace(
    go.Bar(
        x=data[data.year == selected_years[1]]['month'], 
        y=data[data.year == selected_years[1]]['GOOG'], 
        name=selected_years[1],
        offsetgroup=2
        ),
    secondary_y=True,
)

fig3.show()

If that’s the case, this post might be helpful too: Group bar chart with secondary y axis in plotly by python - #5 by jawabutt

The other option would be:

# OPTION 2
fig2 = px.bar(data_frame=data, x='ym', y='GOOG', color='year')
fig2.show()

I hope this helps!

2 Likes

I want this:

but I cant work with plotly express for some reasons and I have no clue how to get this result.

And thank you very much celia

So I need to make a ym column to get this result… If I’m right isnt it?

Yes! In your case it would be yw (for year_week). And then you can change what appears in the ticklabels by updating the layout.

Hello celia,

I’m trying to create month/year and week/year column like 12.2019 and 52.2019 when im trying to combine my year and month or week column to create a new column called monthYear and weekYear thats working fine. but I want to format into datetime columns like week.year = 53.2019 or mont.year = 11.2019 but I cant find a solution…

Im trying to combine my month column = 9 and my year column = 2019 to a datetime with:

dff[‘monthYear’] = pd.to_datetime(dff.Monat.astype(str) + ‘-’ + dff.Jahr.astype(str), format=‘%m-%y’)

but im getting this error:

File “C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\tools\datetimes.py”, line 423, in _convert_listlike_datetimes
result, timezones = array_strptime(

File “pandas_libs\tslibs\strptime.pyx”, line 150, in pandas._libs.tslibs.strptime.array_strptime

ValueError: unconverted data remains: 20

My current problem is that I have no date column with 01.04.2019 only year, month and week column cause im not interested in days. But to create a bar chart like this I need to combine year and month and if i want to display all weeks of the selected years I need to combine week and year but converting it into an date doesnt work…