Python 显示形状文件

Python 显示形状文件,python,python-3.x,matplotlib,shapefile,Python,Python 3.x,Matplotlib,Shapefile,我有一个我想展示的。我尝试使用matplotlib来显示它,但我发现: 然而,当我尝试使用在线网站显示时,我得到了这个结果; 如何获得第二张图像 这是我的密码: import shapefile import matplotlib.pyplot as plt print("Initializing Shapefile") sf = shapefile.Reader("ap_abl") apShapes = sf.shapes() points = apShapes[3].points pri

我有一个我想展示的。我尝试使用matplotlib来显示它,但我发现: 然而,当我尝试使用在线网站显示时,我得到了这个结果;

如何获得第二张图像

这是我的密码:

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()

提前谢谢。

一个shapefile内部有多个形状,我需要绘制所有这些形状。从这一点来看,这是有效的:

import shapefile
import matplotlib.pyplot as plt

sf = shapefile.Reader("ap_abl")

print("Initializing Display")
fig = plt.figure()
ax = fig.add_subplot(111)
plt.xlim([76, 85])
plt.ylim([12, 21])
print("Display Initialized")

for shape in sf.shapes():
    print("Finding Points")
    points = shape.points
    print("Found Points")    

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

print("Displaying Polygons")
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()
,如中所述。

shapefile是几何图形的数据库。你必须对每一个都进行渲染。对不起;我已经删除了我的答案。我没有时间同时实现和调试您的解决方案。我的上述评论仍然有效;您需要迭代Shapefile中的所有形状。祝你好运您知道如何访问几何体吗?Shapefile数据库中的每个形状都是一个几何体。您至少已经在访问其中一个!你是不是只是从其他地方复制/粘贴了这段代码,却不知道它在做什么或Shapefile是什么?宾果!阅读文档很有帮助!