使用 plotly 绘制 geopandas 数据框
问题描述
我有一个 geopandas 数据框,它由区域名称 (District)、geometry 列和 amount 列组成.我的目标是使用下面提到的方法绘制等值线图
我还检查了我的列的格式/类型是否正确.
这是我用来绘制地图的代码
fig = px.choropleth(合并,geojson=merged.geometry,位置=合并的.index,颜色=金额")fig.update_geos(fitbounds="locations", visible=False)图.show()
它产生了下图
这显然不是正确的数字.由于某些原因,它不显示地图,而是显示一条线,当我放大时,我能够看到地图,但它有线穿过它.像这样
有没有人遇到过类似的问题?如果是这样,您是如何解决的?
我使用的 Plotly 版本是 4.7.0.我已尝试升级到最新版本,但仍然无法正常工作.
非常感谢任何帮助.请在我的
I have a geopandas dataframe, which consists of the region name(District), the geometry column, and the amount column. My goal is to plot a choropleth map using the method mentioned below https://plotly.com/python/choropleth-maps/#using-geopandas-data-frames
Here’s a snippet of my dataframe
I also checked that my columns were in the right format/type.
And here's the code I used to plot the map
fig = px.choropleth(merged,
geojson=merged.geometry,
locations=merged.index,
color="Amount")
fig.update_geos(fitbounds="locations", visible=False)
fig.show()
It produced the below figure
which is obviously not the right figure. For some reasons, it doesn't show the map, instead it shows a line and when I zoom in, I am able to see the map but it has lines running through it. Like this
Has anyone ran into a similar problem? If so how were you able to resolve it?
The Plotly version I am using is 4.7.0. I have tried upgrading to a most recent version but it still didn’t work.
Any help is greatly appreciated. Please find my code and the data on my github.
解决方案I'll give you the answer to @tgrandje's comment that solved the problem. Thanks to @Poopah and @tgrandje for the opportunity to raise the answer.
import pandas as pd
import plotly.express as px
import geopandas as gpd
import pyproj
# reading in the shapefile
fp = "./data/"
map_df = gpd.read_file(fp)
map_df.to_crs(pyproj.CRS.from_epsg(4326), inplace=True)
df = pd.read_csv("./data/loans_amount.csv")
# join the geodataframe with the cleaned up csv dataframe
merged = map_df.set_index('District').join(df.set_index('District'))
#merged = merged.reset_index()
merged.head()
fig = px.choropleth(merged, geojson=merged.geometry, locations=merged.index, color="Amount")
fig.update_geos(fitbounds="locations", visible=False)
fig.show()
相关文章