[英]Plotting a geopandas dataframe using plotly
I have a geopandas dataframe, which consists of the region name( District ), the geometry column, and the amount column.我有一个 geopandas dataframe,它由区域名称( District )、几何列和数量列组成。 My goal is to plot a choropleth map using the method mentioned below https://plotly.com/python/choropleth-maps/#using-geopandas-data-frames
我的目标是 plot 使用下面提到的方法https://plotly.com/python/choropleth-data-frames/#using
Here's a snippet of my dataframe这是我的 dataframe 的片段
I also checked that my columns were in the right format/type.我还检查了我的列的格式/类型是否正确。
And here's the code I used to plot the map这是我用来 plot 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.
由于某些原因,它没有显示 map,而是显示一条线,当我放大时,我能够看到 map 但它有线穿过它。 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.我使用的 Plotly 版本是 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 .
请在我的github上找到我的代码和数据。
I'll give you the answer to @tgrandje's comment that solved the problem.我会给你@tgrandje 解决问题的评论的答案。 Thanks to @Poopah and @tgrandje for the opportunity to raise the answer.
感谢@Poopah 和@tgrandje 有机会提出答案。
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()
Another possible source of the problem (when using Plotly graph_objects) is mentioned in this answer over at gis.stackexchange.com:另一个可能的问题来源(使用 Plotly graph_objects 时)在 gis.stackexchange.com 的这个答案中提到:
locations
argument has to point to a column that matches GeoJSON's 'id's. locations
参数必须指向与 GeoJSON 的 'id' 匹配的列。geojson
argument expects a dictionary. geojson
参数需要一个字典。To solve your problem, you should: (i) point locations to the dataframe's index, and (ii) turn your GeoJSON string to a dictionary.
要解决您的问题,您应该:(i)将位置指向数据框的索引,以及(ii)将 GeoJSON 字符串转换为字典。
It's not exactly the answer to your question, but I thought my problem was the same as yours and this helped me.这不完全是您问题的答案,但我认为我的问题与您的问题相同,这对我有所帮助。 So I am including the answer here.
所以我在这里包括答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.