使用地理命令显示地理点

2024-04-16 04:20:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在地图上显示点使用一个形状文件作为地图和一个坐标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})

Tags: andtheimportmapdfreaddataplot
1条回答
网友
1楼 · 发布于 2024-04-16 04:20:56

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\u map添加到ax,然后将geo\u df添加到同一ax。最后一行回答您的问题“如何显示点?”。请记住,这两个层必须在同一个CRS中(假设代码中有epsg 4326),否则层不会重叠。你知道吗

有关绘图的更多信息,请参见geopandas文档-https://geopandas.readthedocs.io/en/latest/mapping.html和CRS中的https://geopandas.readthedocs.io/en/latest/projections.html。你知道吗

相关问题 更多 >