In this video a detailed explain about how a shapefile can be plotted in python is discussed. The main focus of the video is to be able to plot the shapefile with data like area of each portion of the shapefile and plot it with different colors for better understanding. Along with that it is also discussed how a base map can be added to the background of the plotted shapefile along with the details like north arrow, scalebar, legend and also make the shapefile transparent.
code used:
import geopandas as gpd
import matplotlib.pyplot as plt
import matplotlib.ticker as tik
import contextily as ctx
from matplotlib_scalebar.scalebar import ScaleBar
from matplotlib.colors import from_levels_and_colors
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.patches as mpatches
import matplotlib.lines as mlines
def numft(x,pos):
s =f'{x/1000:,.0f}'
return s
shape_file = "P:/PhD/Pavan_phd/data/Catalunya_provinces/bm5mv21sh0tpp1_20200601_0.shp"
shape_filee = gpd.read_file(shape_file)
p = shape_filee.crs
fig,ax = plt.subplots(figsize =(8,8))
z_pal =[0,5910,6310,7740,12170]
pal_rgb = ['aqua','purple','brown', 'red']
cmap2,norm =from_levels_and_colors(z_pal,pal_rgb,extend= 'neither')
shape_filee.plot(ax=ax,facecolor = 'none', linewidth = 0.5, edgecolor = 'black', label='Catalunya')
shape_filee.boundary.plot(ax=ax, linewidth = 0.5, edgecolor = 'black')
shape_filee.plot(column ='AREAPROV',cmap=cmap2, ax=ax,linewidth = 0.5, edgecolor = 'black',norm=norm)
shape_filee.boundary.plot(ax=ax, linewidth = 0.5, edgecolor = 'black')
Create a Patch object with the desired properties
catalunya_line = mlines.Line2D([], [], color='black', label='Catalunya provinces')
catalunya_patch = mpatches.Patch(color='black', label='Catalunya provinces')
ctx.add_basemap(ax=ax, crs =shape_filee.crs,source = ctx.providers.CartoDB.Positron, zoom = 8, alpha = 1)
for axis in [ax.xaxis, ax.yaxis]:
formatter = tik.FuncFormatter(numft)
axis.set_major_formatter(formatter)
Add the north arrow
x, y, arrow_length = 0.95, 1, 0.09
ax.annotate('N', xy=(x, y), xytext=(x, y-arrow_length),
arrowprops=dict(facecolor='black', width=5, headwidth=15),
ha='center', va='center', fontsize=26,
xycoords=ax.transAxes)
scalebar = ScaleBar(0.001, units='km', location='lower center')
ax.add_artist(scalebar)
plt.legend(handles =[catalunya_line],loc ="lower right", fontsize=10)
plt.title("Catalunya", fontsize=18)
plt.xlabel("x-UTM[km]", fontsize=18)
plt.ylabel("y-UTM[km]", fontsize=18)
scalarmappable = plt.cm.ScalarMappable(cmap= cmap2)
scalarmappable.set_array(shape_filee['AREAPROV'])
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="4%", pad=0.06)
cbar = plt.colorbar(scalarmappable, cax=cax, aspect=10)
cbar.set_label(label='Area (km\u00B2)', size=18)
cbar.set_ticks([6666,8200,9831,11366])
cbar.set_ticklabels(['5902','6306','7731','12166'], rotation=90, verticalalignment='center', fontsize=16)
plt.tight_layout()
plt.show()
Auf dieser Seite können Sie das Online-Video How to plot Shapefile in Python | Part-03 mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer My Knowlege space 11 August 2023 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 288 Mal angesehen und es wurde von 6 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!