带有Choroplethmapbox的地图未显示在Dash中

问题描述 投票:1回答:2

我正在尝试在Dash中使用Choroplethmapbox构建地图,但无法在其中绘图。

我使用其他工具测试geojson,在该工具中,地图绘制正确,但没有使用Choroplethmapbox

关于我在做什么错的任何想法?

谢谢

GeoJson:

https://gist.github.com/Tlaloc-Es/5c82834e5e4a9019a91123cb11f598c0

Python Dash代码:

import json
with open('mexico.geojson') as f:
    counties = json.load(f)
fig = go.Figure(go.Choroplethmapbox(
    geojson=counties,
    locations=df['COV_'],
    z=df['Enero'],
    colorscale="Viridis", zmin=0, zmax=df['Enero'].max(),
    marker_opacity=0.5, marker_line_width=0))

fig.update_layout(mapbox_style="carto-positron",
                  mapbox_zoom=3, mapbox_center = {"lat": 37.0902, "lon": -95.7129})
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
python geojson plotly-dash choropleth
2个回答
2
投票

看起来您似乎在底部缺少了fig.show(),而在顶部却没有了[[import plotly.graph_objects go]]。下面的代码将打开一个地图,但Choropleth值需要数据。您的代码示例中有df,但没有包含csv。如果需要我们代码中的df,请导入pandas,然后从csv创建一个名为df的数据框。这是一个可以帮助您的链接。 Pandas dataframesimport json import plotly.graph_objects as go with open(r'{insert file path here}.geojson') as f: counties = json.load(f) fig = go.Figure(go.Choroplethmapbox( geojson=counties, colorscale="Viridis", zmin=0, zmax=100, marker_opacity=0.5, marker_line_width=0)) fig.update_layout(mapbox_style="carto-positron", mapbox_zoom=3, mapbox_center = {"lat": 37.0902, "lon": -95.7129}) fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) fig.show()

这是在您的json上使用熊猫的示例。有关下面代码的进一步说明,请参见here

from urllib.request import urlopen import json with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response: counties = json.load(response) import pandas as pd df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", dtype={"fips": str}) import plotly.express as px fig = px.choropleth(df, geojson=counties, locations='fips', color='unemp', color_continuous_scale="Viridis", range_color=(0, 12), scope="usa", labels={'unemp':'unemployment rate'} ) fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) fig.show()


1
投票
我需要在此代码中添加一个ID字段:
© www.soinside.com 2019 - 2024. All rights reserved.