更改树图中的 max/min 值颜色条

Change max/min value colorbar in treemap

这是来自 plotly 库的示例树状图图表

import plotly.express as px
import numpy as np
df = px.data.gapminder().query("year == 2007")

print(np.average(df['lifeExp'], weights=df['pop']))
fig = px.treemap(df, path=[px.Constant("world"), 'continent', 'country'], values='pop',
                  color='lifeExp', hover_data=['iso_alpha'],
                  color_continuous_scale='RdBu',
                  color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop'])
                )
fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))
fig.show()

这些值应为最小值 = 0 和最大值 = 200,如下所示。你可以帮帮我吗?

根据 plotly express treemap documentation,您可以将列表 [min,max] 传递给 range_color 参数:

import plotly.express as px
import numpy as np
df = px.data.gapminder().query("year == 2007")

print(np.average(df['lifeExp'], weights=df['pop']))
fig = px.treemap(df, path=[px.Constant("world"), 'continent', 'country'], values='pop',
                  color='lifeExp', hover_data=['iso_alpha'],
                  color_continuous_scale='RdBu',
                  color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop']),
                  range_color=[0,200]
                )
fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))
fig.show()