显示一个Shapefile

Ace*_*Ace 3 python matplotlib shapefile python-3.x

我有一个shapefile要显示。我尝试使用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()
Run Code Online (Sandbox Code Playgroud)

先感谢您。

MS_*_*MS_ 6

使用GeoPandas:

import geopandas as gpd
shape=gpd.read_file('shapefile')
shape.plot()
Run Code Online (Sandbox Code Playgroud)

使用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()
Run Code Online (Sandbox Code Playgroud)

如果shapefile具有多个形状,则可以sf.shapes()按照此答案中的讨论进行循环。


Ace*_*Ace 5

原来,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()
Run Code Online (Sandbox Code Playgroud)