使用地理熊猫显示地理位置

时间:2019-12-04 09:43:51

标签: python geopandas

我想使用形状文件作为地图和带有坐标的csv在地图上显示点。该代码有效,但我不明白如何显示图形。 我的问题是:如何显示积分?什么是“ WnvPresent”?我如何只显示地图和点,而不是显示为负值和正值? 我从其下载shp文件的网站:https://ec.europa.eu/eurostat/web/gisco/geodata/reference-data/administrative-units-statistical-units/countries 这个想法来自的网站:https://towardsdatascience.com/geopandas-101-plot-any-data-with-a-latitude-and-longitude-on-a-map-98e01944b972

import pandas as pd
import matplotlib.pyplot as plt
import descartes
import geopandas as gpd
from shapely.geometry import Point, Polygon
 %matplotlib inline 
#read map data in form of .shp
street_map = gpd.read_file(r"C:\Users\stetc\Desktop\images/portofolio\ref-countries-2016-01m.shp")
#create the map
fig,ax = plt.subplots(figsize=(15,15))
street_map.plot(ax = ax)
#read given data
df = pd.read.file(r"C:\Users\stetc\Documents\full_dataset.csv")
#the next step is to get the data in the right format. The way we do this is by turning our regular Pandas DataFrame into a geo-DataFrame, which will require us to specify as parameters the original DataFrame, our coordinate reference system (CRS), and the geometry of our new DataFrame. In order to format our geometry appropriately, we will need to convert the longitude and latitude into Points (we imported Point from shapely above), so first let’s read in the training data-set and specify the EPSG:4326 CRS like so
crs = {"init":"epsg:4326"}
#create points using longitude and lat from the data set
geometry = [Point(xy) for xy in zip (df["Longitude"], df["Latitude"])]
#Create a GeoDataFrame 
geo_df =gpd.GeoDataFrame (df, #specify out data
                          crs=crs, # specify the coordinates reference system
                          geometry = geometry #specify the geometry list created
                         )
fig,ax = plt.subplots(figsize = (15,15))
street_map.plot (ax = ax, alpha = 0.4 , color="grey" )
geo_df[geo_df["WnvPresent"]==0].plot(ax=ax,markersize=20, color = "blue", marker="o",label="Neg")
geo_df[geo_df["WnvPresent"]==1].plot(ax=ax,markersize=20, color = "red", marker="o",label="Pos")
plt.legend(prop={"size":15})

1 个答案:

答案 0 :(得分:0)

WnvPresent只是示例中用于绘制两种不同颜色的列(我会做不同的方式,但这是另一个讨论),如果您的目标是仅绘制点,则可以忽略。

尝试下面的代码。我还添加了zorder以确保点位于street_map的顶部。

fig, ax = plt.subplots(figsize=(15,15))
street_map.plot(ax=ax, alpha=0.4, color="grey", zorder=1)
geo_df.plot(ax=ax, markersize=20, color="blue", marker="o", zorder=2)

在第一步中,创建图形,然后将street_map添加到斧头,然后将geo_df添加到同一斧头。最后一行回答您的问题“如何显示点?”。请记住,这两层必须位于同一CRS中(假设代码中的epsg 4326),否则各层将不会重叠。

geopandas文档-https://geopandas.readthedocs.io/en/latest/mapping.html和此处的https://geopandas.readthedocs.io/en/latest/projections.html中的CRS有关绘图的更多信息。