我有一个 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()
提前谢谢你。
最佳答案
使用 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/