簡體   English   中英

如何創建一個下拉菜單來更改用於為等值線 map 着色的列? (情節快報)

[英]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 其中之一時沒有任何變化。 任何幫助是極大的贊賞!

我根據官方參考中的示例圖從這個示例中創建了代碼。

  1. px 沒有從下拉列表中選擇數據的示例,因此我將其更改為 go。
  2. 您需要在 choroplethmapbox 中設置顏色軸。
  3. 我將數據列和色標設置為按鈕側的輸入值。
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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM