简体   繁体   English

Plotly Express:在动画中使颜色条静态化

[英]Plotly Express: Make colorbar static in animation

There is a similar question here but the solution is not working for me.这里有一个类似的问题但解决方案对我不起作用。 Maybe it's because I am generating the map via px rather than go .也许是因为我通过px而不是go生成地图。

I am trying to make the colorbar static, but I am not able to achieve that properly.我试图使颜色条静态,但我无法正确实现。 I added z , zmax and zmin to the traces but it results in weird behaviour.我加zzmaxzmin的痕迹,但它会导致怪异的行为。 In the provided example here, it makes the entire map static.在此处提供的示例中,它使整个地图成为静态。 (in my original dataframe, it shows false values on some countries) What am I doing wrong here, any guidance would be appreciated. (在我的原始数据框中,它在某些国家/地区显示错误值)我在这里做错了什么,将不胜感激。

import plotly.express as px

df = px.data.gapminder()

fig = px.choropleth(df, locations='iso_alpha', color='gdpPercap', animation_frame=df.year.astype(str),
                        projection ='equirectangular')
    
# fig.update_traces(z=df['lifeExp'],zmin=0, zmax=100)
# for idx,frame in enumerate(fig.frames):
#     frame.data[0].update(z=df['lifeExp'], zmin=0, zmax=100)
fig.show()

在此处输入图片说明

Oh well, adding range_color to the main function call and specifying the range (min,max) does the trick here.哦, range_color ,在主函数调用中添加range_color并指定范围 (min,max) 在这里可以解决问题。 I used the min and max of the gdpPercap feature itself, but they other values can be used too.我使用了gdpPercap特性本身的最小值和最大值,但也可以使用其他值。

import plotly.express as px

df = px.data.gapminder()

fig = px.choropleth(df, locations='iso_alpha', color='gdpPercap', animation_frame=df.year.astype(str),
                        projection ='equirectangular', range_color=(min(df.gdpPercap), max(df.gdpPercap)))
fig.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM