我对GeoPandas根本没有什么经验,所以有点迷茫。我正试图绘制这些数据
我按照GeoPandas网站上的许多参考资料,阅读了博客文章,以及这个堆栈溢出的帖子。所有的人都告诉我做同样的事情,但现在看来还是可以的。在Geopandas中绘制数据
当我试图绘制这些数据时,它的结果是这样的。在这里输入图像描述
我所要做的就是将这个csv文件中的经纬度数据绘制到地图上(最终是我从.shp文件中加载的地图)。
总之,这是我目前写的代码。
import csv
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
import descartes
from shapely.geometry import Point, Polygon
#Load in the CSV Bike Station Location Data
df = pd.read_csv('HRSQ12020.csv')
#combine the latitude and longitude to make coordinates
df['coordinates'] = df[['Longitude', 'Latitude']].values.tolist()
# Change the coordinates to a geoPoint
df['coordinates'] = df['coordinates'].apply(Point)
df
#convert df to a geodf
df = gpd.GeoDataFrame(df, geometry='coordinates')
df
#plot the geodf
df.plot(figsize=(20,10));
有什么问题吗?我检查了所有的100个坐标,它们似乎都没有问题。任何建议都将是巨大的! 谢谢!我没有太多经验。
很可能是投影系统的问题。一个好的做法是立即定义 crs
在创建 Geopandas
对象。如果你尝试。
df = gpd.GeoDataFrame(df, geometry='coordinates', crs = 4326)
也许你会明白你的观点。我把 "4326" 因为你的X-Y坐标看起来像GPS坐标,而GPS坐标是WSG84标准(CRS码:4326)。如果不是好的,就换成相关的crs代码。
上面这些回答很有帮助。这也变成了另一种解决方案,因为lingo建议设置crs。我当时得到的是一个错误,但是当我忽略了这个错误的时候,这个就解决了。这是我的代码,最后成功了。
import csv
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
import descartes
from shapely.geometry import Point, Polygon
#Load in the CSV Bike Station Location Data
df = pd.read_csv('HRSQ12020.csv')
#combine the latitude and longitude to make coordinates
df['coordinates'] = df[['Longitude', 'Latitude']].values.tolist()
# Change the coordinates to a geoPoint
df['coordinates'] = df['coordinates'].apply(Point)
df.head()
#fixing wrong negative value for Latitude
df.loc[df["Latitude"] == df["Latitude"].min()]
df.at[80, 'Latitude'] = 40.467715
#count the numner of racks at each station
rackTot = 0
for index, row in df.iterrows():
rackTot += row['NumRacks']
crs = {'init' :'epsg:4326'}
geometry = [Point(xy) for xy in zip(df.Longitude, df.Latitude)]
geobikes = gpd.GeoDataFrame(df, crs=crs, geometry=geometry)
geobikes.head()
#plot the geodf
#not working for some reason, fix later
geobikes.plot()
当我用前四行坐标运行你的代码时,我得到了你所期望的结果。从你的绘图范围来看,你可能有一些负的纬度值。你可以用df['Latitude'].min()来检查吗?
import csv
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
from shapely.geometry import Point, Polygon
df = pd.DataFrame({'Latitude' :[40.441326, 40.440877, 40.439030, 40.437200],
'Longitude' :[-80.004679, -80.003080, -80.001860, -80.000375]})
df['coordinates'] = df[['Longitude', 'Latitude']].values.tolist()
# Change the coordinates to a geoPoint
df['coordinates'] = df['coordinates'].apply(Point)
df
#convert df to a geodf
df = gpd.GeoDataFrame(df, geometry='coordinates')
df
#plot the geodf
df.plot(figsize=(20,10));
你也可以使用plt.subplots(),然后为你的数据设置xlim和ylim。
df = pd.DataFrame({'Latitude' :[40.441326, 41.440877, 42.439030, 43.437200],
'Longitude' :[-78.004679, -79.003080, -80.001860, -81.000375]})
df['coordinates'] = df[['Longitude', 'Latitude']].values.tolist()
# Change the coordinates to a geoPoint
df['coordinates'] = df['coordinates'].apply(Point)
df
#convert df to a geodf
df = gpd.GeoDataFrame(df, geometry='coordinates')
print(type(df))
#plot the geodf
fig, ax = plt.subplots(figsize=(14,6))
df.plot(ax = ax)
xlim = ([df.total_bounds[0] - 1, df.total_bounds[2] + 1])
ylim = ([df.total_bounds[1] - 1, df.total_bounds[3] + 1])
# you can also pass in the xlim or ylim vars defined above
ax.set_xlim([-82, -77])
ax.set_ylim([40, 42])
plt.show()