简体   繁体   English

使用 plotly 绘制地理熊猫 dataframe

[英]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 的这个答案中提到:

  1. The locations argument has to point to a column that matches GeoJSON's 'id's. locations参数必须指向与 GeoJSON 的 'id' 匹配的列。
  2. The 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.

相关问题 使用 plotly 绘制 geopandas dataframe 几何图形 - Plotting a geopandas dataframe geometry with plotly 使用 geopandas 和 matplotlib 绘制地图 - Plotting a map using geopandas and matplotlib 使用Pandas从熊猫数据框中绘制分组的条形图 - Plotting a grouped bar chart using Plotly from a pandas dataframe 按小时重新采样 Pandas DataFrame 并使用 Plotly 绘制堆积条形图 - Resampling Pandas DataFrame by hour and plotting a stacked bar chart using Plotly Plotly Express:使用 plotly express 将 dataframe 的各个列绘制为多个图(可滚动) - Plotly Express: Plotting individual columns of a dataframe as multiple plots (scrollable) using plotly express 使用 geopandas 和 folium 在 python 中绘制多边形 - plotting polygons in python using geopandas and folium 使用 geopandas 和 geoplot 在 NYC map 上绘制位置 - Plotting locations on a NYC map using geopandas and geoplot Plotly:绘制 dataframe 的列,结果为空白 plot - Plotly: Plotting columns of a dataframe resulting in blank plot 在 Plotly 中拆分数据框并使用不同的线条样式进行绘图 - Splitting a dataframe and plotting with different line styles in Plotly 将数据帧转换为可读的,以使用 plotly express 绘制条形图 - Transform a dataFrame into readable for plotting barchart with plotly express
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM