首页 > 解决方案 > 在 Geopandas 中绘图时管理投影

问题描述

我正在使用 geopandas 绘制意大利地图。

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize = (20,30))

region_map.plot(ax=ax, color='white', edgecolor='black')
plt.xlim([6,19])
plt.ylim([36,47.7])
plt.tight_layout()
plt.show()

这就是在正确定义region_map为“几何” GeoSeries 之后的结果。

意大利地图

但是,我无法修改图形纵横比,即使figsizeplt.subplots. 我错过了一些微不足道的事情,还是可能是地理熊猫问题?

谢谢

标签: pythonpandasfiguregeopandasmap-projections

解决方案


您的源数据集 ( region_map) 显然是在地理坐标系中“编码”的(单位:lats 和 lons)。在您的情况下可以安全地假设这是 WGS84 (EPSG: 4326 )。如果您希望您的绘图看起来更像它在例如谷歌地图中的样子,您将不得不将其坐标重新投影到许多投影坐标系之一(单位:米)。您可以使用全球可接受的 WEB MERCATOR (EPSG: 3857 )。

Geopandas 让这一切变得尽可能简单。您只需要了解我们如何处理计算机科学中的坐标投影并通过其 EPSG 代码学习最流行的 CRS 的基础知识。

import matplotlib.pyplot as plt

#If your source does not have a crs assigned to it, do it like this:
region_map.crs = {"init": "epsg:4326"}

#Now that Geopandas what is the "encoding" of your coordinates, you can perform any coordinate reprojection
region_map = region_map.to_crs(epsg=3857)

fig, ax = plt.subplots(figsize = (20,30))
region_map.plot(ax=ax, color='white', edgecolor='black')

#Keep in mind that these limits are not longer referring to the source data!
# plt.xlim([6,19])
# plt.ylim([36,47.7])
plt.tight_layout()
plt.show()

我强烈建议阅读有关管理预测的官方 GeoPandas 文档。


推荐阅读