gpt4 book ai didi

python - 如何创建一个下拉菜单来更改用于为等值线图着色的列? (剧情快车)

转载 作者:行者123 更新时间:2023-12-04 08:13:54 25 4
gpt4 key购买 nike

我目前正在对奥地利的 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”的下拉菜单,但是当我选择其中一个时没有任何变化。任何帮助是极大的赞赏!

最佳答案

我从 this example 创建了代码基于 official reference 中的示例图.

  • px 没有从下拉选择数据的例子,所以我把它改成了。
  • 您需要在 choroplethmapbox 中设置颜色轴。
  • 我将数据列和色标设置为按钮侧的输入值。
  • 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()
    enter image description here
    enter image description here

    关于python - 如何创建一个下拉菜单来更改用于为等值线图着色的列? (剧情快车),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65807473/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com