我想根据不同列中的值更改栏的不透明度。这是一个简单的例子。如果 GDPPercap <20000 I want to change the opacity to 0.5 for instance. I also have a discrete color map that assigns colors based on the decade, for instance 1980-1990 is green , 1990-2000 is red. Within this color map I am looking to change the opacity of the bars.
import plotly.express as px
data_canada = px.data.gapminder().query("country == 'Canada'")
fig = px.bar(data_canada, x='year', y='pop')
fig.show()
添加包含新标准化国内生产总值值的列。 (从 0 到 1)指定以该列作为颜色目标的连续颜色图。以 RGBA 形式指定该颜色规范的透明度。阈值 0.4 设置得合适,因此将其更改为您的阈值。
import plotly.express as px
data_canada = px.data.gapminder().query("country == 'Canada'")
data_canada_ = data_canada.copy()
s = data_canada_['gdpPercap']
data_canada_['gdpPercap_min_max'] = (s - s.min()) / (s.max() - s.min())
fig = px.bar(data_canada_,
x='year', y='pop',
color='gdpPercap_min_max',
color_continuous_scale=[(0, "rgba(65,105,225,1.0)"), (0.4, "rgba(65,105,225,0.5)"), (1.0, "rgba(65,105,225,0.5)")])
fig.show()