Y 标签的放置 python 水平条形图

Placement of Y labels plotly python horizontal barchart

我正在寻找一种方法来获取分组条形图上方的水平条形图的 y 标签。我正在寻找的是:

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

希望你们中的一个知道如何做到这一点。提前致谢!

一种模拟这种情况的方法是复制轨迹并在这些轨迹上使用 文本 参数

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

其中 y 轴刻度是标签

同样的技术。

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

对于任何正在寻找另一种方法来解决此问题(不使用隐藏痕迹)的人,您也可以使用 fig.add_annotations。使用 add_annotations,您可以在图表中的特定位置添加文本。

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

代码将产生下图: