繁体   English   中英

Geopandas 数据未正确绘制

[英]Geopandas data not plotting correctly

我根本没有太多使用 GeoPandas 的经验,所以我有点迷茫。 我正在尝试 plot 这个数据

jupyter笔记本dataframe图片

我在 GeoPandas 网站上关注了许多参考资料,通读了博客文章和这篇堆栈溢出文章。 他们都告诉我做同样的事情,但它现在似乎仍然有效。 在 geopandas 中绘制数据

当我尝试 plot 这个数据时,结果如下: enter image description here

All I am trying to do is plot points from this csv file that has latitude and longitude data onto a map (eventually a map that I have loaded from an.shp file).

无论如何,这是我到目前为止编写的代码:

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 个坐标,它们似乎都很好。 任何建议都会很棒! 谢谢!

应该是投影系统的问题。 一件好事是在创建Geopandas object 时立即定义crs 如果你试试,

df = gpd.GeoDataFrame(df, geometry='coordinates', crs = 4326)

也许您将能够看到您的观点。 我输入“4326”是因为您的 xy 坐标看起来像 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()

图形

当我使用前四行坐标运行您的代码时,我得到了您所期望的。 从您的 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()

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM