[英]How to create a dropdown menu which changes the column used for coloring a choropleth map? (Plotly Express)
我目前正致力于可视化奥地利的 covid-19 病例。 我希望图表能够通过下拉菜单切换您想要查看的值。 (例如总病例数、总死亡人数等)这是我目前拥有的:
fig = px.choropleth(df,geojson = data
,locations = 'GKZ'
,scope = 'europe'
,color = "7-Tage-Inzidenz"
,featureidkey='properties.iso'
,animation_frame = 'Datum'
,hover_data = ['Bezirk']
)
fig.update_geos(showcountries=False, showcoastlines=False,showland = False,fitbounds = "locations")
fig["layout"]
fig["layout"].pop("updatemenus")
fig.write_html('österreichkarte.html',auto_play = False)
我尝试了 Plotly 提供的教程并添加了这个:
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(
args=[{"color":"7-Tage-Inzidenz"}],
label = "7-Tage-Inzidenz",
method = "restyle"
),
dict(
args=[{"color":"AnzahlFaelle"}],
label = "AnzahlFaelle",
method = "restyle"
)
]))
])
现在我得到一个带有“7-Tage-Inzidenz”和“AnzahlFaelle”的下拉菜单,但是当我 select 其中之一时没有任何变化。 任何帮助是极大的赞赏!
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
df = px.data.election()
geojson = px.data.election_geojson()
fig = go.Figure(go.Choroplethmapbox(geojson=geojson,
locations=df['district'],
z=df['Coderre'],
coloraxis='coloraxis',
featureidkey="properties.district",
)
)
# fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.update_layout(coloraxis_colorscale='Viridis',
mapbox=dict(style='carto-positron',
zoom=9,
center = {"lat": 45.55 , "lon":-73.75 },
))
button1 = dict(method='update',
label='Coderre',
args=[{'z':[df['Coderre']]},
{'coloraxis.colorscale':'Viridis'}])
button2 = dict(method='update',
label='Bergeron',
args=[{'z':[df['Bergeron']]},
{'coloraxis.colorscale':'RdBu'}])
fig.update_layout(updatemenus=[dict(active=0,
buttons=[button1, button2])]
)
fig.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.