【问题标题】:Changing choropleth symbology with drop down @ plotly使用下拉 @ plotly 更改等值线符号系统
【发布时间】:2021-10-30 13:52:34
【问题描述】:

您好,我对 plotly 很陌生,并尝试使用按钮选择器创建具有不同符号系统的 choropleth covid 地图。我的问题是我无法实现不同的符号系统。

提前致谢!

这是我目前所拥有的:

df= pd.read_csv (covid.csv)
data=dict(type=‘choropleth’,
locations = df[‘Name’],
locationmode = ‘country names’,
z = df[‘Total’] # I believe here is my problem if I do z=[‘total’, ‘1M_pop’] I lost data
marker_line_color = ‘black’,
marker_line_width = 0.5,
)

layout=dict(title_text = ‘Cases today by country’,
title_x = 0.5,
geo=dict(
showframe = True,
showcoastlines = True,
projection_type = ‘mercator’
)
)
map=go.Figure( data=[data], layout =layout)

#buttons test
fig[“layout”]
fig[“layout”].pop(“updatemenus”)
fig.update_geos(fitbounds=“locations”, visible=True)

button1 = dict(method = “restyle”,
args = [{‘z’: [ df[“Total”] ] }, [“colorscale”, “Greens”]],
label = “Total Cases”)
button2 = dict(method = “restyle”,
args = [{‘z’: [ df[“1M_pop”] ]},[“colorscale”, “reds”]],
label = “Cases per 1M”)

fig.update_layout(width=1000,
coloraxis_colorbar_thickness=23,
updatemenus=[dict(y=1.1,
x=0.275,
xanchor=‘right’,
yanchor=‘bottom’,
active=0,
buttons=[button1, button2])
])
plot(fig, filename=‘covid.html’)``` 





[plotly][1]


  [1]: https://i.stack.imgur.com/BV3gh.png

【问题讨论】:

  • 嗨 Omri,欢迎来到 Stack Overflow!那里的链接格式不正确。请检查您的帖子的预览并修复链接。下次请小心,并始终记住在发布之前仔细检查并查看您的问题。祝你好运:D
  • 您好,欢迎您。如果您可以查看how-to-ask,然后尝试生成mcve,那就太好了。

标签: python pandas button plotly choropleth


【解决方案1】:
  • 已插入 OWID 数据以使其可重现。此外,您的引号和双引号无法使用,因此必须更改为标准引号
  • 我可以在 z 中进行更改以在 updatemenus 中工作。 colorscale 无法正常工作
  • 切换技术,生成两条相似的轨迹,然后使用 updatemenus 来控制可见性

import requests, io
import pandas as pd
import plotly.graph_objects as go

# df= pd.read_csv (covid.csv)
df = pd.read_csv(
    io.StringIO(
        requests.get(
            "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/latest/owid-covid-latest.csv"
        ).text
    )
)
df = df.rename(
    columns={
        "location": "Name",
        "total_cases": "Total",
        "total_cases_per_million": "1M_pop",
    }
)

data = dict(
    type="choropleth",
    locations=df["Name"],
    colorscale="reds",
    locationmode="country names",
    z=df[
        "Total"
    ],  # I believe here is my problem if I do z=['total', '1M_pop'] I lost data
    marker_line_color="black",
    marker_line_width=0.5,
)

layout = dict(
    title_text="Cases today by country",
    title_x=0.5,
    geo=dict(showframe=True, showcoastlines=True, projection_type="mercator"),
)
fig = go.Figure(
    data=[
        data,
        {**data, **{"z": df["1M_pop"], "colorscale": "blues", "visible": False}},
    ],
    layout=layout,
)

# buttons test
fig["layout"]
fig["layout"].pop("updatemenus")
fig.update_geos(fitbounds="locations", visible=True)

button1 = dict(
    method="update",
    args=[{"visible": [True, False]}],
    label="Total Cases",
)
button2 = dict(
    method="update",
    args=[{"visible": [False, True]}],
    label="Cases per 1M",
)

fig.update_layout(
    width=1000,
    coloraxis_colorbar_thickness=23,
    updatemenus=[
        dict(
            y=1.1,
            x=0.275,
            xanchor="right",
            yanchor="bottom",
            active=0,
            buttons=[button1, button2],
        )
    ],
)

fig

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-01
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 2021-10-31
    • 2012-11-18
    • 1970-01-01
    相关资源
    最近更新 更多