Python Dash Ploly:格式化表格时自定义颜色图例 [英] Python Dash Plotly : Customise colour legend when formatting table

查看:27
本文介绍了Python Dash Ploly:格式化表格时自定义颜色图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要通过标记极端小值和大值(红色)和中值(绿色)来为列上色。以下是色标图例的示例。

但是,通过使用以下默认代码"RdYlGn",我只能使用默认颜色图例执行此操作

def discrete_background_color_bins(df, n_bins=7, columns='all'):

 bounds = [i * (1.0 / n_bins) for i in range(n_bins+1)]
 if columns == 'all':
     if 'id' in df:
         df_numeric_columns = df.select_dtypes('number').drop(['id'], axis=1)
     else:
         df_numeric_columns = df.select_dtypes('number')
 else:
     df_numeric_columns = df[columns]
 df_max = df_numeric_columns.max().max()
 df_min = df_numeric_columns.min().min()
 ranges = [
     ((df_max - df_min) * i) + df_min
     for i in bounds
 ]
 styles = []
 legend = []
 for i in range(1, len(bounds)):
     min_bound = ranges[i - 1]
     max_bound = ranges[i]
     backgroundColor = colorlover.scales[str(n_bins+4)]['div']['RdYlGn'][2:-2][i - 1]
     color = 'black'

     for column in df_numeric_columns:
         styles.append({
             'if': {
                 'filter_query': (
                     '{{{column}}} >= {min_bound}' +
                     (' && {{{column}}} < {max_bound}' if (i < len(bounds) - 1) else '')
                 ).format(column=column, min_bound=min_bound, max_bound=max_bound),
                 'column_id': column
             },
             'backgroundColor': backgroundColor,
             'color': color
         })
     legend.append(
         html.Div(style={'display': 'inline-block', 'width': '60px'}, children=[
             html.Div(
                 style={
                     'backgroundColor': backgroundColor,
                     'borderLeft': '1px rgb(50, 50, 50) solid',
                     'height': '10px'
                 }
             ),
             html.Small(round(min_bound, 2), style={'paddingLeft': '2px'})
         ])
     )

 return (styles, html.Div(legend, style={'padding': '5px 0 5px 0'}))

有人能告诉我如何突出显示红色的极值和绿色(或蓝色)的中值吗?

谢谢。

推荐答案

终于得到答案了!

....
styles = []
legend = []
backgroundColors = ['rgb(215,48,39)', 'rgb(252,141,89)', 'rgb(145,207,96)', 'rgb(26,152,80)', 'rgb(145,207,96)', 'rgb(252,141,89)',  'rgb(215,48,39)']
for i in range(1, len(bounds)):
    min_bound = ranges[i - 1]
    max_bound = ranges[i]
    backgroundColor = backgroundColors[i - 1]
    color = "white" if i > len(bounds) / 2.0 else "inherit"
    ....

这篇关于Python Dash Ploly:格式化表格时自定义颜色图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆