View a polygon read from a shapefile using matplotlib

I am trying to view the main polygon read from Shapefile using matplotlib and pyshp. But all my efforts give only empty axes without a polygon. Here are some of my attempts using a dataset showing the borders of Belgium :

import shapefile as sf r = sf.Reader("BEL_adm/BEL_adm0") p=r.shapes() b=p[0] points = b.points import matplotlib.pyplot as plt from matplotlib.path import Path imporst matplotlib.patches as patches verts = points verts = [] for x,y in points: verts.append(tuple([x,y])) codes = ['']*len(verts) codes[0] = Path.MOVETO codes[-1] = Path.CLOSEPOLY for i in range(1,len(verts)): codes[i]=Path.LINETO path = Path(verts, codes) fig = plt.figure() ax = fig.add_subplot(111) patch = patches.PathPatch(path, facecolor='orange', lw=2) ax.add_patch(patch) ax.set_xlim(-2,2) ax.set_ylim(-2,2) plt.show() 

Another patch attempt also gives an empty frame:

 fig = plt.figure(figsize=(11.7,8.3)) ax = plt.subplot(111) x,y=zip(*b.points) import matplotlib.patches as patches import matplotlib.pyplot as plt bol=patches.Polygon(b.points,True, transform=ax.transAxes) ax.add_patch(bol) ax.set_ylim(0,60) ax.set_xlim(0,200) plt.show() 

I would be glad to see what is missing.
Thanks Oz

0
source share
1 answer

instead of calling set_xlim(), set_ylim() to set the axis range, you can use ax.autoscale() .

For your version of Polygon, you do not need to set the transform argument to ax.transAxes, just call:

 bol=patches.Polygon(b.points,True) 
+1
source

Source: https://habr.com/ru/post/1437096/


All Articles