如何为多个饼图保持相同的颜色顺序?

时间:2019-12-20 10:53:44

标签: python plotly

我要遍历多列并创建可绘制的饼图,以检查这些变量在两种类型的客户之间的分布:power usernot power user。但是,颜色顺序每次都会更改。

在一个情节中,Yes将为橙色,No将为橙色,在下一个情节中,Yes将为蓝色,No将为橙色。

如果您查看两个图表之间的图例,则可以在此处看到示例:

enter image description here

enter image description here

这是我正在使用的代码。经过一番谷歌搜索后,我尝试将sort=False添加到go.pie,但没有任何改变:

    def plot_pie(column) :

        trace1 = go.Pie(values  = churn[column].value_counts().values.tolist(),
                        labels  = churn[column].value_counts().keys().tolist(),
                        hoverinfo = "label+percent+name",
                        domain  = dict(x = [0,.48]),
                        name    = f"{target}",
                        marker  = dict(line = dict(width = 2,
                                                   color = "rgb(243,243,243)")
                                      ),
                        hole    = .6,
                        sort=False
                       )
        trace2 = go.Pie(values  = not_churn[column].value_counts().values.tolist(),
                        labels  = not_churn[column].value_counts().keys().tolist(),
                        hoverinfo = "label+percent+name",
                        marker  = dict(line = dict(width = 2,
                                                   color = "rgb(243,243,243)")
                                      ),
                        domain  = dict(x = [.52,1]),
                        hole    = .6,
                        name    = f"Not {target}" ,
                        sort=False
                       )


        layout = go.Layout(dict(title = column + f" distribution in {target} ",
                                plot_bgcolor  = "rgb(243,243,243)",
                                paper_bgcolor = "rgb(243,243,243)",
                                annotations = [dict(text = f"{target}",
                                                    font = dict(size = 13),
                                                    showarrow = False,
                                                    x = .15, y = .5),
                                               dict(text = f"not {target}",
                                                    font = dict(size = 13),
                                                    showarrow = False,
                                                    x = .88,y = .5
                                                   )
                                              ]
                               )
                          )
        data = [trace1,trace2]
        fig  = go.Figure(data = data,layout = layout)
        py.iplot(fig)

有什么建议吗?

0 个答案:

没有答案