Using plotly express px.bar causes a shift in output

Question:

I am trying to print a bar chart using plotly express (px.bar). It is a yearly set of data from 2015 to 2022 but gets plotted from 2016 to 2023. Not sure why…

Code to plot yearly returns.

# plotting the yearly % returns
fig = px.bar(yearly, x='Date', y=['Model','Benchmark'], title="Yearly Profit", labels={'value':
     'Percent', 'variable':''},barmode = 'group')
new = {'Model':'Model %', 'Benchmark': bMark + ' %'}   # Change the column names for the legend
fig.for_each_trace(lambda t: t.update(name = new[t.name]))
fig.show()

Here is the input “yearly.”

Date Model Benchmark
0 2015-12-31 00:00:00 72.7922 1.23411
1 2016-12-30 00:00:00 27.1839 11.9969
2 2017-12-29 00:00:00 39.9856 21.7039
3 2018-12-31 00:00:00 15.6401 -4.56891
4 2019-12-31 00:00:00 42.3679 31.2216
5 2020-12-31 00:00:00 211.67 18.3307
6 2021-12-31 00:00:00 56.0862 28.7285
7 2022-10-31 00:00:00 -1.97482 -17.7437

Here is the output chart.

enter image description here

Asked By: supplydemand

||

Answers:

Maybe not exactly the answer you’re looking for, but a workaround is shown below :

Explicitly get a "Year" column:

yearly['Date'] = pd.to_datetime(yearly['Date'])
yearly['Year'] = yearly['Date'].dt.year

Use that in your figure:

fig = px.bar(yearly, x='Year', y=['Model','Benchmark'], title="Yearly Profit", labels={'value':
     'Percent', 'variable':''},barmode = 'group')
new = {'Model':'Model %', 'Benchmark': 'bMark' + ' %'}   # Change the column names for the legend
fig.for_each_trace(lambda t: t.update(name = new[t.name]))
fig.show()

This shows the years as per expectation.

Answered By: Suraj Shourie

Assuming your Date column is of the datetime data type (if not, first do this: df['Date'] = pd.to_datetime(df['Date'])), here’s what you can do:

Extract the year attribute to a new column:

df['year'] = df['Date'].dt.year.astype(int)

Finally, you can specify the range of the year you want in plotly:

fig = px.bar(yearly, x='year', y=['Model','Benchmark'], title="Yearly Profit", labels={'value':
     'Percent', 'variable':''},barmode = 'group', range_x = [2015, 2022])

By directly extracting the year from the Date column, you are able to circumvent the issue you are encountering.

Answered By: Diamoniner12345