我正试图用破折号中的Choroplethmapbox
构建一个地图,但我无法用它进行绘图
我使用另一个工具测试geojson,在该工具中,地图绘制正确,但不使用Choroplethmapbox
你知道我做错了什么吗
谢谢
GeoJson:
https://gist.github.com/Tlaloc-Es/5c82834e5e4a9019a91123cb11f598c0
Python破折号代码:
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})
我需要添加一个带有此代码的id:
我使用了这个页面的代码:https://community.plot.ly/t/plot-a-shapefile-shp-in-a-choropleth-chart/27850
看起来您缺少底部的fig.show(),而顶部的导入plotly.graph\u对象为go。下面的代码将打开一个映射,但choropleth值需要数据。您的代码示例中有df,但没有包含csv。如果您想使用代码中的df,请导入pandas,并从csv创建一个名为df的数据帧。这里有一个链接可以帮助您实现这一点Pandas dataframes
下面是一个在json中使用pandas的示例。下面代码的进一步解释可以在here
我必须解决一个类似的问题,这是我应用于您的问题的解决方案(尽管使用了
px.choropleth_mapbox
而不是我发现对choropleths来说很混乱的graph_对象)app.layout = ...
),而你可能不用它就可以服务,只需按照建议使用fig.show()
,显式优于隐式。因此,创建布局部分更好李>不管怎样,希望我的解决方案可以作为你工作的开始。我添加了单选按钮,以便您可以选择要用于颜色的数据列
将此代码保存到文件“myapp.py”中,并在终端中以
python myapp.py
的形式运行它,启动web浏览器,导航到它所使用的url(它在终端中写出,通常为172.0.0.1:8050),这样您可以:我从默认的anaconda频道运行这些版本
顺便说一句,我实际上是用这些数据问了一个关于dash choropleth映射框选择行为(Selection behavior in plotly-dash Choropleth mapbox plot)的问题。谢谢
相关问题 更多 >
编程相关推荐