我正在使用GeoPandas绘图功能绘制带有图例的地图.当我绘制时,我的传说出现在图的右上角.这是它的样子:
我想将图例移到图表的下半部分.对于正常的matplotlib图,我通常会做这样的事情:
fig, ax = plt.subplots(1, figsize=(4.5,10))
lima_bank_num.plot(ax=ax, column='quant_cuts', cmap='Blues', alpha=1, legend=True)
ax.legend(loc='lower left')
Run Code Online (Sandbox Code Playgroud)
但是,不考虑此修改.
我有一个包含芝加哥地址的 DataFrame,我已将其地理编码为纬度和经度值,然后编码为 Point 对象(使 DataFrame 成为 GeoDataFrame)。一小部分已使用芝加哥以外的 LatLong 值进行了错误的地理编码。我有一个用于芝加哥边界(GeoDataFrame)的 shapefile,我想选择点位于芝加哥边界多边形之外的所有行。
选择多边形内的所有点很容易(通过 geopandas sjoin 函数),但我还没有找到一种选择不在多边形内的点的好方法。一个存在吗?
假设我有两个不相交的群体/多边形"群岛"(想想两个非相邻县的人口普查区).我的数据看起来像这样:
>>> p1=Polygon([(0,0),(10,0),(10,10),(0,10)])
>>> p2=Polygon([(10,10),(20,10),(20,20),(10,20)])
>>> p3=Polygon([(10,10),(10,20),(0,10)])
>>>
>>> p4=Polygon([(40,40),(50,40),(50,30),(40,30)])
>>> p5=Polygon([(40,40),(50,40),(50,50),(40,50)])
>>> p6=Polygon([(40,40),(40,50),(30,50)])
>>>
>>> df=gpd.GeoDataFrame(geometry=[p1,p2,p3,p4,p5,p6])
>>> df
geometry
0 POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))
1 POLYGON ((10 10, 20 10, 20 20, 10 20, 10 10))
2 POLYGON ((10 10, 10 20, 0 10, 10 10))
3 POLYGON ((40 40, 50 40, 50 30, 40 30, 40 40))
4 POLYGON ((40 40, 50 40, 50 50, 40 50, 40 40))
5 POLYGON …
Run Code Online (Sandbox Code Playgroud) 我最近开始在python中使用Geopandas来完成我的一些空间工作,并对此感到非常满意 - 我目前正在尝试阅读PostGIS功能,并且不太了解如何参数化数据库连接,这似乎并不清楚在文档中:
GeoDataFrame.from_postgis(sql, con, geom_col='geom', crs=None, index_col=None,
coerce_float=True, params=None)
Run Code Online (Sandbox Code Playgroud)
这可能是一个非常简单的问题,所有我想知道的是需要进入'con' - 我假设一个包含数据库连接信息的字符串?但是以什么格式?设置'sql'似乎很简单.任何帮助非常感谢 - 谢谢!
我想绘制 geopandas 数据帧的单行中包含的几何图形,但我遇到了问题。这里有一个例子
import geopandas as gpd
import numpy as np
from shapely.geometry import Polygon
p1 = Polygon([(0, 0), (1, 0), (1, 1)])
p2 = Polygon([(2, 0), (3, 0), (3, 1), (2, 1)])
p3 = Polygon([(1, 1), (2, 1), (2, 2), (1, 2)])
index = np.random.random(3)
df = gpd.GeoDataFrame()
df['index'] = index
df['geometry'] = [p1,p2,p3]
df = df.set_geometry('geometry')
Run Code Online (Sandbox Code Playgroud)
现在,如果我使用df.plot()
我得到的命令进行绘图
但如果我尝试只绘制一行, df.loc[:,0].plot()
我收到以下错误
TypeError: Empty 'DataFrame': no numeric data to plot
,
而如果我尝试
df.loc[:,'geometry'].plot()
我得到 AttributeError: 'Polygon' object has …
您好,我正在尝试将 X 和 Y 坐标列表转换为线。我想通过groupby
ID 和时间映射这些数据。只要我grouby
一列,我的代码就会成功执行,但两列是我遇到错误的地方。我参考了这个问题。
以下是一些示例数据:
ID X Y Hour
1 -87.78976 41.97658 16
1 -87.66991 41.92355 16
1 -87.59887 41.708447 17
2 -87.73956 41.876827 16
2 -87.68161 41.79886 16
2 -87.5999 41.7083 16
3 -87.59918 41.708485 17
3 -87.59857 41.708393 17
3 -87.64391 41.675133 17
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
df = pd.read_csv("snow_gps.csv", sep=';')
#zip the coordinates into a point object and convert to a GeoData Frame
geometry = [Point(xy) for xy in zip(df.X, df.Y)]
geo_df …
Run Code Online (Sandbox Code Playgroud) 我正在尝试geopandas
使用 Anaconda Prompt安装该软件包,但是在我使用之后conda install geopandas
发生了意外:
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: -
Found conflicts! Looking for incompatible packages
Run Code Online (Sandbox Code Playgroud)
在此之后,它继续搜索冲突,但数小时未完成。最后还是不能用geopandas
。
我也试过geopandas
在不同的虚拟环境中安装,它可以工作,但我不知道如何在 Jupyter Notebooks 中使用该环境。
我想知道,没有单独的环境怎么安装geopandas
?
或者,在单独的环境中安装 Jupyter …
我是初学者,我尝试按照教程进行操作。所以我安装了一个 anaconda,下一步我应该安装 geopandas,但我收到以下消息:
conda install geopandas
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: \
...
Run Code Online (Sandbox Code Playgroud)
我一次又一次地尝试安装 anaconda 但没有任何效果...
我在使用 geopanda 的 read_file() 读取 geojson 时收到以下警告:
...geodataframe.py:422: RuntimeWarning: Sequential read of iterator was interrupted. Resetting iterator. This can negatively impact the performance.
for feature in features_lst:
Run Code Online (Sandbox Code Playgroud)
这是我使用的代码示例:
crime_gdf = gpd.read_file('datasets/crimes.geojson', bbox=bbox)
Run Code Online (Sandbox Code Playgroud)
crimes.geojson
是一个包含大量点的文件,每个点都有一个“犯罪类型”
bbox
定义边界
代码按预期运行,但我不明白该警告。
编辑
我将 geojson 转换为feather,并收到相同的警告。
我需要在地理图上绘制一些数据。具体来说,我想强调数据来自的国家和州。我的数据集是
Year Country State/City
0 2009 BGR Sofia
1 2018 BHS New Providence
2 2002 BLZ NaN
3 2000 CAN California
4 2002 CAN Ontario
... ... ... ...
250 2001 USA Ohio
251 1998 USA New York
252 1995 USA Virginia
253 2011 USA NaN
254 2019 USA New York
Run Code Online (Sandbox Code Playgroud)
为了创建地理图,我一直在使用geopandas
如下:
import geopandas as gpd
shapefile = 'path/ne_110m_admin_0_countries/ne_110m_admin_0_countries.shp'
gdf = gpd.read_file(shapefile)[['ADMIN', 'ADM0_A3', 'geometry']]
gdf.columns = ['country', 'country_code', 'geometry']
Run Code Online (Sandbox Code Playgroud)
然后我合并了两个数据集:
merged = gdf.merge(df, left_on = 'country_code', right_on = …
Run Code Online (Sandbox Code Playgroud)