用饼图绘制快速订单标签

Plotly express order label with a pie chart

让我们使用这个示例数据框:

df = pd.DataFrame({"Day":['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
'Proportion':[0.24495486, 0.17300189, 0.23019185, 0.15408692, 0.17827757,0.01100911, 0.0084778]})

         Day  Proportion
0     Monday    0.244955
1    Tuesday    0.173002
2  Wednesday    0.230192
3   Thursday    0.154087
4     Friday    0.178278
5   Saturday    0.011009
6     Sunday    0.008478

我想使用 plotly express 通过饼图可视化此数据框。我构建了以下函数:

import plotly.express as px

def plot_pie_graph(df_graph,col_names,col_values, title_x = "axis_x",title_y="axis_y",title = "Graphe",
                   color_discrete_map = {}, save_graph_name = "") :
    
    if title_x == "axis_x" :
        title_x = col_names
    if title_y == "axis_y":
        title_y = col_values
    
    fig = px.pie(df_graph, values = col_values, names = col_names,color_discrete_map=color_discrete_map,
                 color=col_names)

    fig.update_layout({
            'plot_bgcolor': 'rgba(0,0,0,0)',
            'paper_bgcolor': 'rgba(0,0,0,0)',
        },
        hoverlabel=dict(
            #bgcolor="white", 
            font_size=12, 
            #font_family="Rockwell"
        ),
        xaxis={
            'title': title_x,
            },
        yaxis={'title':title_y},
        title={
            'text': title,
            #'y':0.95,
            'x':0.5,
            'xanchor': 'center',
            'yanchor': 'top'},
        hovermode = "x"
    )
    
    if save_graph_name != "" :
        fig.write_html(save_graph_name + ".html")
    
    fig.show()

当我使用它时,我得到了以下饼图:

plot_pie_graph(df,"Day",'Proportion',title = "Proportion by day")

天数没有按照数据框中的顺序排列。我看到有 a parameter called "category_order" with bar charts 来处理这个问题,但饼图不可用。请问我如何在逻辑上对标签中的日期进行排序?

只需添加

fig.update_traces(sort=False) 

在创建图形之后和save/show创建图形之前。