Placement of Y labels plotly python horizontal barchart
Question:
I’m looking for a way to get the y labels of a plotly horizontal barchart above the grouped bars. What i’m looking for is:
import pandas as pd
import plotly.graph_objects as go
test_df = pd.DataFrame({'component':['BAR 1','BAR 2','BAR 3'],'mean':[7.4,7.8,6.5],'mean_proj':[6.8,7.9,8.4]})
def barchart2(df,x1,x2,y):
fig = go.Figure()
fig.add_trace(go.Bar(name=x1,x=df[x1],y=df[y],orientation='h',hoverinfo=None,marker_color='#221435')),
fig.add_trace(go.Bar(name=x2,x=df[x2],y=df[y],orientation='h',hoverinfo=None,marker_color='rgb(55,177,140)')),
fig.update_xaxes(showgrid=False, zeroline=False)
fig.update_yaxes(showgrid=False, zeroline=False)
fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)',bargap=0.20,margin=dict(t=30,b=5,l=25,r=0))
fig.update_layout(barmode='group')
return fig.write_html('test.html',auto_open=True)
barchart2(test_df,'mean','mean_proj','component')
Answers:
An approach to simulating this is to duplicate traces and use text parameters on these traces
import pandas as pd
import plotly.graph_objects as go
test_df = pd.DataFrame({'component':['BAR 1','BAR 2','BAR 3'],'mean':[7.4,7.8,6.5],'mean_proj':[6.8,7.9,8.4]})
def barchart2(df,x1,x2,y):
fig = go.Figure()
fig.add_trace(go.Bar(name=x1,x=df[x1],y=df[y],marker_color='#221435')),
fig.add_trace(go.Bar(name=x1,x=df[x1],y=df[y],marker_color='rgba(0,0,0,0)', text=x1, showlegend=False)),
fig.add_trace(go.Bar(name=x2,x=df[x2],y=df[y],marker_color='rgb(55,177,140)')),
fig.add_trace(go.Bar(name=x2,x=df[x2],y=df[y],marker_color='rgba(0,0,0,0)', text=x2, showlegend=False)),
fig.update_traces(orientation="h", hoverinfo=None, insidetextanchor="start")
fig.update_xaxes(showgrid=False, zeroline=False)
fig.update_yaxes(showgrid=False, zeroline=False)
fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)',bargap=0.20,margin=dict(t=30,b=5,l=25,r=0))
fig.update_layout(barmode='group')
# return fig.write_html('test.html',auto_open=True)
return fig
barchart2(test_df,'mean','mean_proj','component')
where y-axis ticks are labels
Same technique.
import pandas as pd
import plotly.graph_objects as go
test_df = pd.DataFrame({'component':['BAR 1','BAR 2','BAR 3'],'mean':[7.4,7.8,6.5],'mean_proj':[6.8,7.9,8.4]})
def barchart2(df,x1,x2,y):
fig = go.Figure()
fig.add_trace(go.Bar(name=x1,x=df[x1],y=df[y],marker_color='#221435')),
fig.add_trace(go.Bar(name=x1,x=df[x1],y=df[y],marker_color='rgba(0,0,0,0)', text=df[y], showlegend=False)),
fig.add_trace(go.Bar(name=x2,x=df[x2],y=df[y],marker_color='rgb(55,177,140)')),
fig.add_trace(go.Bar(name=x2,x=df[x2],y=df[y],marker_color='rgba(0,0,0,0)', text=df[y], showlegend=False)),
fig.update_traces(orientation="h", hoverinfo=None, insidetextanchor="start")
fig.update_xaxes(showgrid=False, zeroline=False)
fig.update_yaxes(showgrid=False, zeroline=False)
fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)',bargap=0.20,margin=dict(t=30,b=5,l=25,r=0))
fig.update_layout(barmode='group')
# return fig.write_html('test.html',auto_open=True)
return fig.update_yaxes(visible=False)
barchart2(test_df,'mean','mean_proj','component')
For anyone who’s looking for another way to fix this (without the use of hidden traces) you could also use fig.add_annotations. With add_annotations you can add text on certain places in the graph.
import pandas as pd
import plotly.graph_objects as go
test_df = pd.DataFrame({'component':['BAR 1','BAR 2','BAR 3','BAR 4','BAR 5','BAR 6','BAR 7'],'mean':[7.4,7.8,6.5,7.7,7.4,7.8,6.5],'mean_proj':[6.8,7.9,8.4,6.3,7.9,8.4,6.3]})
def barchart2(df,x1,x2,y,font):
fig = go.Figure()
fig.add_trace(go.Bar(name=x1,x=df[x1],y=df[y],orientation='h',hoverinfo=None,marker_color='#221435')),
fig.add_trace(go.Bar(name=x2,x=df[x2],y=df[y],orientation='h',hoverinfo=None,marker_color='rgb(55,177,140)')),
fig.update_xaxes(showgrid=False, zeroline=False,showticklabels=False)
fig.update_yaxes(showgrid=False, zeroline=False,showticklabels=False)
fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)',bargap=0.50,
margin=dict(t=30,b=5,l=0,r=0),barmode='group',
legend=dict(yanchor='bottom',y=-0.1,xanchor='left',x=0,font=dict(family=font,size=25)))
startplace = 6.4
for i in df[y].unique():
fig.add_annotation(x=0.23,y=startplace,
text=i,
showarrow=False,
font=dict(family=font,color='#EB0045',size=25)),
startplace = startplace - 1
return fig.write_html('fig1.html',auto_open=True)
barchart2(test_df,'mean','mean_proj','component','Arial')
The code will result in the following graph:
I’m looking for a way to get the y labels of a plotly horizontal barchart above the grouped bars. What i’m looking for is:
import pandas as pd
import plotly.graph_objects as go
test_df = pd.DataFrame({'component':['BAR 1','BAR 2','BAR 3'],'mean':[7.4,7.8,6.5],'mean_proj':[6.8,7.9,8.4]})
def barchart2(df,x1,x2,y):
fig = go.Figure()
fig.add_trace(go.Bar(name=x1,x=df[x1],y=df[y],orientation='h',hoverinfo=None,marker_color='#221435')),
fig.add_trace(go.Bar(name=x2,x=df[x2],y=df[y],orientation='h',hoverinfo=None,marker_color='rgb(55,177,140)')),
fig.update_xaxes(showgrid=False, zeroline=False)
fig.update_yaxes(showgrid=False, zeroline=False)
fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)',bargap=0.20,margin=dict(t=30,b=5,l=25,r=0))
fig.update_layout(barmode='group')
return fig.write_html('test.html',auto_open=True)
barchart2(test_df,'mean','mean_proj','component')
An approach to simulating this is to duplicate traces and use text parameters on these traces
import pandas as pd
import plotly.graph_objects as go
test_df = pd.DataFrame({'component':['BAR 1','BAR 2','BAR 3'],'mean':[7.4,7.8,6.5],'mean_proj':[6.8,7.9,8.4]})
def barchart2(df,x1,x2,y):
fig = go.Figure()
fig.add_trace(go.Bar(name=x1,x=df[x1],y=df[y],marker_color='#221435')),
fig.add_trace(go.Bar(name=x1,x=df[x1],y=df[y],marker_color='rgba(0,0,0,0)', text=x1, showlegend=False)),
fig.add_trace(go.Bar(name=x2,x=df[x2],y=df[y],marker_color='rgb(55,177,140)')),
fig.add_trace(go.Bar(name=x2,x=df[x2],y=df[y],marker_color='rgba(0,0,0,0)', text=x2, showlegend=False)),
fig.update_traces(orientation="h", hoverinfo=None, insidetextanchor="start")
fig.update_xaxes(showgrid=False, zeroline=False)
fig.update_yaxes(showgrid=False, zeroline=False)
fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)',bargap=0.20,margin=dict(t=30,b=5,l=25,r=0))
fig.update_layout(barmode='group')
# return fig.write_html('test.html',auto_open=True)
return fig
barchart2(test_df,'mean','mean_proj','component')
where y-axis ticks are labels
Same technique.
import pandas as pd
import plotly.graph_objects as go
test_df = pd.DataFrame({'component':['BAR 1','BAR 2','BAR 3'],'mean':[7.4,7.8,6.5],'mean_proj':[6.8,7.9,8.4]})
def barchart2(df,x1,x2,y):
fig = go.Figure()
fig.add_trace(go.Bar(name=x1,x=df[x1],y=df[y],marker_color='#221435')),
fig.add_trace(go.Bar(name=x1,x=df[x1],y=df[y],marker_color='rgba(0,0,0,0)', text=df[y], showlegend=False)),
fig.add_trace(go.Bar(name=x2,x=df[x2],y=df[y],marker_color='rgb(55,177,140)')),
fig.add_trace(go.Bar(name=x2,x=df[x2],y=df[y],marker_color='rgba(0,0,0,0)', text=df[y], showlegend=False)),
fig.update_traces(orientation="h", hoverinfo=None, insidetextanchor="start")
fig.update_xaxes(showgrid=False, zeroline=False)
fig.update_yaxes(showgrid=False, zeroline=False)
fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)',bargap=0.20,margin=dict(t=30,b=5,l=25,r=0))
fig.update_layout(barmode='group')
# return fig.write_html('test.html',auto_open=True)
return fig.update_yaxes(visible=False)
barchart2(test_df,'mean','mean_proj','component')
For anyone who’s looking for another way to fix this (without the use of hidden traces) you could also use fig.add_annotations. With add_annotations you can add text on certain places in the graph.
import pandas as pd
import plotly.graph_objects as go
test_df = pd.DataFrame({'component':['BAR 1','BAR 2','BAR 3','BAR 4','BAR 5','BAR 6','BAR 7'],'mean':[7.4,7.8,6.5,7.7,7.4,7.8,6.5],'mean_proj':[6.8,7.9,8.4,6.3,7.9,8.4,6.3]})
def barchart2(df,x1,x2,y,font):
fig = go.Figure()
fig.add_trace(go.Bar(name=x1,x=df[x1],y=df[y],orientation='h',hoverinfo=None,marker_color='#221435')),
fig.add_trace(go.Bar(name=x2,x=df[x2],y=df[y],orientation='h',hoverinfo=None,marker_color='rgb(55,177,140)')),
fig.update_xaxes(showgrid=False, zeroline=False,showticklabels=False)
fig.update_yaxes(showgrid=False, zeroline=False,showticklabels=False)
fig.update_layout(paper_bgcolor='rgba(0,0,0,0)',plot_bgcolor='rgba(0,0,0,0)',bargap=0.50,
margin=dict(t=30,b=5,l=0,r=0),barmode='group',
legend=dict(yanchor='bottom',y=-0.1,xanchor='left',x=0,font=dict(family=font,size=25)))
startplace = 6.4
for i in df[y].unique():
fig.add_annotation(x=0.23,y=startplace,
text=i,
showarrow=False,
font=dict(family=font,color='#EB0045',size=25)),
startplace = startplace - 1
return fig.write_html('fig1.html',auto_open=True)
barchart2(test_df,'mean','mean_proj','component','Arial')
The code will result in the following graph: