【问题标题】:Change max/min value colorbar in treemap [duplicate]更改树形图中的最大值/最小值颜色条 [重复]
【发布时间】:2022-01-02 10:18:26
【问题描述】:

这是来自 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()

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

【问题讨论】:

标签: python plotly treemap


【解决方案1】:

根据 plotly express 树图 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()

【讨论】:

  • 非常感谢您的回答!我会试试的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 2017-07-28
  • 2017-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多