【问题标题】:Placement of Y labels plotly python horizontal barchartY标签的放置plotly python水平条形图
【发布时间】:2021-11-04 08:32:04
【问题描述】:

我正在寻找一种方法来获取分组条上方的绘图水平条形图的 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')  

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

【问题讨论】:

  • 在查看了documentiations 关于如何配置 yaxis 之后,我发现没有预定义的方法。
  • 您是指要插入黄色箭头所在位置的刻度标签或跟踪名称吗?即插入 "mean" 和 "mean_proj" 或 "BAR 1", "BAR 2", ...
  • 我正在寻找一种方法将“BAR 1”放在前两个栏上方,将“BAR 2”放在后两个栏上方。

标签: python axis-labels plotly-python placement


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    对于正在寻找另一种解决此问题的方法(不使用隐藏痕迹)的人,您也可以使用 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') 
    

    代码将生成下图:

    【讨论】:

      猜你喜欢
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 2013-06-02
      • 2021-10-08
      相关资源
      最近更新 更多