Search code examples
pythonplotly-pythonaxis-labels

Placement of Y labels plotly python horizontal barchart


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:

enter image description here

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')  

Solution

  • 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')  
    

    enter image description here

    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')  
    

    enter image description here