python - 显示形状文件

标签 python python-3.x matplotlib shapefile

我有一个 shapefile我想展示的。我尝试使用 matplotlib 来显示它,但我得到了这个: What I Get 但是,当我尝试使用在线网站进行展示时,我得到了这个; What I Want

如何获取第二张图片?

这是我的代码:

import shapefile
import matplotlib.pyplot as plt

print("Initializing Shapefile")
sf = shapefile.Reader("ap_abl")
apShapes = sf.shapes()
points = apShapes[3].points
print("Shapefile Initialized")

print("Initializing Display")
fig = plt.figure()
ax = fig.add_subplot(111)
plt.xlim([78, 79])
plt.ylim([19, 20])
print("Display Initialized")

print("Creating Polygon")
ap = plt.Polygon(points, fill=False, edgecolor="k")
ax.add_patch(ap)
print("Polygon Created")

print("Displaying polygon")
plt.show()

提前谢谢你。

最佳答案

使用 GeoPandas:

import geopandas as gpd
shape=gpd.read_file('shapefile')
shape.plot()

使用 pyshp 和笛卡尔:

from descartes import PolygonPatch
import shapefile
sf=shapefile.Reader('shapefile')
poly=sf.shape(1).__geo_interface__
fig = plt.figure() 
ax = fig.gca() 
ax.add_patch(PolygonPatch(poly, fc='#ffffff', ec='#000000', alpha=0.5, zorder=2 ))
ax.axis('scaled')
plt.show()

如果 shapefile 有多个形状,那么您可以遍历 sf.shapes(),如 this answer 中所述。 .

关于python - 显示形状文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30447790/

相关文章:

python - Matplotlib 箱线图宽度(对数刻度)

python-3.x - google.cloud storage python api在指定位置创建桶

python - 使用具有重复键名的 Pandas 从 csv 文件创建 JSON 对象

python - 如何下载Python中创建的PDF文件?

python - OSX : Setting Enthought python path in . bash_profile 导致奇怪的终端行为

python - cx_Freeze - 从桌面快捷方式运行 .exe 时出错

python-3.x - Python3 从同级目录导入模块/包

python - 如何在 matplotlib/pylab 图表中水平打印 Y 轴标签?

python - 如何在条形图和框架之间留出间隙

python - 长测试用例、子字符串游戏超时