Specify color of histogram based on column values. Consistent across charts

Viewed 578

I am trying to plot histograms using plotly express. I have a dataframe which has values like the following:

    env     type    status  success count
0   PROD    ABC     403     False   21
1   IMPL    HTTP    200     True    64037
2   IMPL    HTTP    304     False   9
3   PROD    ABC     503     False   1
4   IMPL    ABC     200     True    3308
5   PROD    HTTP    204     True    54
6   IMPL    ABC     500     False   1000
7   PROD    ABC     500     False   100
....          .....       ....

I want to plot multiple chart(histogram or pie) based on various groupings(env, type) showing the total count of error codes in each grouping. I am trying something like:

fig = px.pie(df.groupby("success").get_group(False), title="Overall Failure Count", names="status", values = "count", color='status')
fig.show()

for env, df_env in df.groupby("env"):
    fig = px.pie(df_env.groupby("success").get_group(False), title="Failure Count for %s"%env, 
          names="status", values = "count", color='status')
    fig.show()

With this the colors for the statuses are generated on the fly and are different for same statuses across charts. I want the colors generated for all the charts such that they are consistent across different charts.

eg: color for status code 200 is red in all charts

I tried using a dict() that would map the status to color:

def colors(n):
    color_list = list()
    for i in range(n):
        random_number = random.randint(0,16777215)
        hex_number = str(hex(random_number))
        hex_number ='#'+ hex_number[2:]
        color_list.append(hex_number)
    return color_list
statuses = list(df.status.unique())
colors_list = colors(len(statuses))
colorMap = dict(zip(statuses, colors_list))

fig = px.pie(df.groupby("success").get_group(False), title="Overall Failure Count", names="status", values = "count", color=df['status'].apply(lambda x : colorMap[x]))

But I get the following error:

All arguments should have the same length. The length of argument `color` is 14, whereas the length of previous arguments ['status', 'count'] is 24

I have also tried various other things like creating a column, df['color] with the hex value of the color corresponding to the status. But that did not work either.

Can someone tell me what I am missing and how I could get this working?

1 Answers

To get the same color mapped to the same value you would use the same color_discrete_map option for all plots, see Using an explicit mapping for discrete colors and Directly Mapping Colors to Data Values.

In your case, something like this:

fig = px.pie(df.groupby("success").get_group(False), 
             title="Overall Failure Count", 
             names="status", 
             values="count", 
             color='status',
             color_discrete_map={200:'red',
                                 403:'blue',
                                 304:'green',
                                 503:'yellow',
                                 204:'grey',
                                 500:'cyan'
                                }
            )
Related