如何创建需要多个输入的饼图子图

问题描述 投票:0回答:1

我想构建两个饼图子图,以国家、部门和年份作为输入。我想看看男性担任的职位比例以及女性担任的职位比例。

(例如男性“CEO”=23%,主席=20%,会员=57%)。

以同样的方式为女性做馅饼

这是我到目前为止所做的代码

@app.callback(Output('pie', 'figure'),
          
          [Input('year-slider', 'value'),

           Input('country-selection', 'value'),

           Input('settore-selection', 'value')])



def update_graph(year_slider, country_selection, settore_selection):

    

    m3=df.groupby(['time','geo', 'NACE', 'POSITION'])['sex'].count().reset_index(name='count')

    

    if country_selection != 'Select all':

        m3 = m3[m3['geo']==country_selection]



    m3 = m3[m3['time'] == year_slider]


    if settore_selection is not None and settore_selection in m3['NACE'].unique():

        m3 = m3[m3['NACE']==settore_selection]

        

    
    fig = make_subplots(rows=1, cols=2, specs=[[{'type':'pie'}, {'type':'pie'}]])
    
    
    fig.add_trace(go.Pie( values=['count'],labels=m3['POSITION'],
                         domain=dict(x=[0, 0.5]), name='Men'),
              1, 1)
   
    fig.add_trace(go.Pie( values=['count'],labels=m3['POSITION'],
                         
                         domain=dict(x=[0.5, 1.0]), name='Women'),
              1, 2)
        
    
    # Use `hole` to create a donut-like pie chart
    
    fig.update_traces(hole=.4, hoverinfo="label+percent+name")
    
    fig.update_layout(
        title_text="How the positions are distributed in men and women?",
        # Add annotations in the center of the donut pies.
        annotations=[dict(text='Men', x=0.18, y=0.5, font_size=20, showarrow=False),
                     dict(text='Women', x=0.82, y=0.5, font_size=20, showarrow=False)])    
        
    return fig    

破折号网络没有给我任何错误,标题出现,还有注释。但没有图表。

我的 .csv 文件的一部分是这样的:

,time,_geo,geo,value,UNIT,_UNIT,EGROUP,_EGROUP,POSITION,_POSITION,sex,_sex,NACE,_NACE

1,2015,AT,Austria,2.0,Number of persons (headcount),NR,Largest listed companies,COMP,Employee representatives,EMP_REP,Women,W,"C, E & F (Mining & quarrying; electricity, gas and water; construction)",CEF

2,2015,AT,Austria,8.0,Number of persons (headcount),NR,Largest listed companies,COMP,Employee representatives,EMP_REP,Men,M,"C, E & F (Mining & quarrying; electricity, gas and water; construction)",CEF

3,2015,AT,Austria,0.0,Number of persons (headcount),NR,Largest listed companies,COMP,Executives,EXEC,Women,W,"C, E & F (Mining & quarrying; electricity, gas and water; construction)",CEF

4,2015,AT,Austria,2.0,Number of persons (headcount),NR,Largest listed companies,COMP,CEO (Chief Executive Officer),CEO,Men,M,"C, E & F (Mining & quarrying; electricity, gas and water; construction)",CEF

我在网上查看过,但只有一些示例可以单独删除值而不是列。所以想必问题就在那里

python pandas plotly plotly-dash pie-chart
1个回答
0
投票

我相信你不小心传递了参数

values=['count']
而不是
values=m3['count'] to 
go.Pie`:

fig.add_trace(go.Pie( values=m3['count'],labels=m3['POSITION'],
                     domain=dict(x=[0, 0.5]), name='Men'),
          1, 1)

fig.add_trace(go.Pie( values=m3['count'],labels=m3['POSITION'],
                     
                     domain=dict(x=[0.5, 1.0]), name='Women'),
          1, 2)
© www.soinside.com 2019 - 2024. All rights reserved.