我有一个想要显示的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()提前谢谢你。
发布于 2015-05-26 10:18:58
原来一个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()发布于 2018-01-02 14:26:16
使用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有多个形状,那么你可以像this answer中讨论的那样在sf.shapes()上循环。
https://stackoverflow.com/questions/30447790
复制相似问题