Python Dash Plotly:格式化时自定义颜色图例 table
Python Dash Plotly : Customise colour legend when formatting table
我想通过标记极小值和极大值(红色颜色)和中点值(绿色颜色)来为列着色).以下是色标图例示例。
但是,通过使用以下默认代码‘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"
....
我想通过标记极小值和极大值(红色颜色)和中点值(绿色颜色)来为列着色).以下是色标图例示例。
但是,通过使用以下默认代码‘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"
....