Make bar chart in Plotly with percentage data

Hi,
How can I make this bar chart in plotly?

image

With this data:

Time Product A Product B Produc C
Total 90 % 88 % 92 %
2022-1 92 % 92 % 91 %
2022-2 93 % 81 % 93 %
2023-1 99 % 83 % 89 %
2023-2 82 % 99 % 82 %
2024-1 89 % 86 % 93 %
2024-2 94 % 83 % 96 %
2025-1 83 % 92 % 100 %
2025-2 85 % 90 % 95 %

You can use textsuffix to make it work. Please refer this link: Layout.yaxis in Python

You can use this below code to achieve your goal:

import pandas as pd
import plotly.graph_objs as go
df = pd.read_excel('Book1_1.xlsx')

fig = go.Figure()
fig.add_trace(go.Bar(name='Product A', x=df['Time'], y=df['Product A']*100))
fig.add_trace(go.Bar(name='Product B', x=df['Time'], y=df['Product B']*100))
fig.add_trace(go.Bar(name='Product C', x=df['Time'], y=df['Produc C']*100))

# Change the bar mode
fig.update_layout(barmode='group', template='plotly_white', legend=dict(orientation='h', x=0.3))
fig.update_xaxes(type='category')
fig.update_yaxes(range=[80,105], ticksuffix="%")
fig.show()

Thank you so much!!

How can I change the colors to this:
#003E69, #ED7D31, #A5A5A5.

How can i place the title in the middel?

And is it possible to set a trend line at 90%?

You can set marker_color for each traces and update title when update layout. For the trend, I think you can add a trace with go.Scatter

i

mport pandas as pd
import plotly.graph_objs as go
df = pd.read_excel('Book1_1.xlsx')

fig = go.Figure()
fig.add_trace(go.Bar(name='Product A', x=df['Time'], y=df['Product A']*100, marker_color='#003E69'))
fig.add_trace(go.Bar(name='Product B', x=df['Time'], y=df['Product B']*100, marker_color='#ED7D31'))
fig.add_trace(go.Bar(name='Product C', x=df['Time'], y=df['Produc C']*100, marker_color='#A5A5A5'))

# Change the bar mode
fig.update_layout(barmode='group', 
                  template='plotly_white', 
                  legend=dict(orientation='h', x=0.3), 
                  title={
                      'text': "Products",
                      'y':0.9,
                      'x':0.5,
                      'xanchor': 'center',
                      'yanchor': 'top'})
fig.update_xaxes(type='category')
fig.update_yaxes(range=[80,105], ticksuffix="%")
fig.show()

1 Like

Thank you so much again!! Is it possible to also get a trend line like this at 90%?:

You could use add_hline. Please refer this link : Horizontal and vertical lines and rectangles in Python

import pandas as pd
import plotly.graph_objs as go
df = pd.read_excel('Book1_1.xlsx')

fig = go.Figure()
fig.add_trace(go.Bar(name='Product A', x=df['Time'], y=df['Product A']*100, marker_color='#003E69'))
fig.add_trace(go.Bar(name='Product B', x=df['Time'], y=df['Product B']*100, marker_color='#ED7D31'))
fig.add_trace(go.Bar(name='Product C', x=df['Time'], y=df['Produc C']*100, marker_color='#A5A5A5'))
fig.add_hline(y=90, line_dash="dot", line_color='red')
# Change the bar mode
fig.update_layout(barmode='group', 
                  template='plotly_white', 
                  legend=dict(orientation='h', x=0.3), 
                  title={
                      'text': "Products",
                      'y':0.9,
                      'x':0.5,
                      'xanchor': 'center',
                      'yanchor': 'top'})
fig.update_xaxes(type='category')
fig.update_yaxes(range=[80,105], ticksuffix="%")
fig.show()

Thank you so much! Is it possible to show number on top of bars with product A?

Number
n=30
n=31
n=32
n=33
n=34
n=35
n=36
n=37

And is it possible to get a little space between the barchart of a group?

If it was me, I will do something as below:

  • Add one more columns that based on df.index and change it to string.
  • Use text option when adding trace
  • Use bargroupgap to make more space between bars
import pandas as pd
import plotly.graph_objs as go
df = pd.read_excel('Book1_1.xlsx')
df['n'] = (30 + df.index).astype(str)

fig = go.Figure()
fig.add_trace(go.Bar(name='Product A', x=df['Time'], y=df['Product A']*100, marker_color='#003E69', text= 'n = ' + df.n))
fig.add_trace(go.Bar(name='Product B', x=df['Time'], y=df['Product B']*100, marker_color='#ED7D31'))
fig.add_trace(go.Bar(name='Product C', x=df['Time'], y=df['Produc C']*100, marker_color='#A5A5A5'))
fig.add_hline(y=90, line_dash="dot", line_color='red')
# Change the bar mode
fig.update_layout(barmode='group', 
                  template='plotly_white', 
                  legend=dict(orientation='h', x=0.3), 
                  title={
                      'text': "Products",
                      'y':0.9,
                      'x':0.5,
                      'xanchor': 'center',
                      'yanchor': 'top'}, bargroupgap=0.15)
fig.update_xaxes(type='category')
fig.update_yaxes(range=[75,105], ticksuffix="%")
fig.update_traces(textposition='inside')
fig.show()

Thank you! How can I make the text of ā€œnā€ bigger?