How to use GeoPandas to draw thematic Map
Editor to share with you how to use GeoPandas to draw thematic maps, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Practice using geopandas to draw thematic maps for reference only.
Import pandas as pdimport geopandas as gpimport contextily as ctximport matplotlib.pyplot as pltfrom shapely.geometry import Pointimport matplotlib.patches as mpatchesfrom matplotlib_scalebar.scalebar import ScaleBarchi=gp.read_file ("population_data/chi_shp/ province .shp") .to_crs (epsg=3415) nine_lines = gp.read_file ('population_data/chi_shp/chi_nine_dotted_line.shp') Encoding='utf-8') tai=gp.read_file ("population_data/chi_shp/ province .shp") tai=tai [tai ['province'] .isin (['Taiwan * *])] .to _ crs (epsg=3415) fig=plt.figure (figsize= (8)) # set canvas size ax= plt.gca () ax.set_title (chi.columns.values [1], fontsize=14,loc= "left") # main graph drawing chi.plot (ax=ax,color='#E24A33',column= "data column name to be drawn" Edgecolor='grey',linewidth=0.5,legend=True,legend_kwds= {'labels':],' loc': 'lower left',' title': 'Title',' shadow': False, 'fontsize':8,' frameon':False, 'prop': {' family': 'Times New Roman',' weight': 'normal',' size': 12}) # add nine * * nine_lines.geometry.to_crs (epsg=3415) .plot (ax=ax Edgecolor='black', linewidth=2, alpha=0.5) # add station * * tai.to_crs (epsg=3415) .plot (ax=ax,hatch= "/", label= "missing value" Facecolor='lightgrey') # set the display range of the drawing ax.set (ylim= (- 0.3mm 10cm)) # Custom legend text # get all the legend labels# legend_labels = ax.get_legend (). Get_texts () # bounds= ['QQ', legend_label in zip (bounds) Legend_labels): # legend_label.set_text (bound) # leg = ax.get_legend () # leg.set_title ('Percentage Error (%)', prop= {'family':' Times New Roman', 'weight':' normal', # 'size': 12}) # drawing ax_child = fig.add_axes ([0.75,0.20,0.15,0.15]) # left, bottom, width, heightchi.plot (ax=ax_child,color='#E24A33' Edgecolor='grey',linewidth=0.5) chi.plot (ax=ax_child,color='#348ABD',edgecolor='grey',linewidth=0.5) ax_child = nine_lines.geometry.to_crs (epsg=3415) .plot (ax=ax_child, edgecolor='black', linewidth=2) Alpha=0.5) tai.to_crs (epsg=3415) .plot (ax=ax_child,hatch= "/", label= "missing value", facecolor='lightgrey') ax_child.set (xlim= (- 0.5 million 10 minutes 6, 1.5 million 10 seconds 6) Ylim= (- 1.5 years 10 provinces 6) ax_child.set_xticks ([]) ax_child.set_yticks ([]) # add provincial and municipal names annotations # texts=chi.apply (lambda x: ax.annotate (province [province]], xy=x.geometry.centroid.coords [0], va='center'#, fontsize=10,arrowprops=dict (arrow) Color='k', lw=0.5), axis=1) # adjust_text (texts) # extra legend drawing p1=gp.GeoDataFrame ({'geometry': [Point (- 1.60 / 10 / 6)]}) p1.plot (ax=ax,markersize=100,facecolor='lightgrey',hatch= "/") ax.text (- 1.54 / 10 / 6) -0.15 / 10 / 6, "NoData", {'family':' Times New Roman', 'weight':' normal', 'size': 12}) # Legend operation LegendElement = [mpatches.Patch (facecolor='#E24A33', linestyle='--', linewidth=1.2) Label='1'), mpatches.Patch (facecolor='#348ABD', linestyle='--', linewidth=1.2, label='2')] ax.legend (handles = LegendElement, loc='lower left' Prop= {'family':' Times New Roman', 'weight':' normal', 'size': 12}, frameon=False,shadow=False) # add scale scalebar = ScaleBar (dx=1*10**-3,units='km',length_fraction=0.1, font_properties= {' family': 'Times New Roman',' weight': 'normal',' size': 12}, location=8,sep=1 Frameon=False) ax.add_artist (scalebar) # add north compass x, y, arrow_length = 0.42,0.09, 0.07ax.annotate ('nasty, xy= (x, y), xytext= (x, y-arrow_length), arrowprops=dict (facecolor='black', width=4, headwidth=7), ha='center', va='center', fontsize=10, xycoords=ax.transAxes) # add base image # ctx.add_basemap (ax,crs= "epsg:3415") Source=ctx.providers.OpenStreetMap.Mapnik (apiKey= "*") # ctx.add_basemap (ax_child,crs= "epsg:3415", source=ctx.providers.OpenStreetMap.CH (apiKey= "*")) # set scale # ax.set_axis_off () # ax_child.set_axis_off () ax.set_xticks ([]) ax.set_yticks ([]) # Save Map # plt.savefig ("filename", dpi=600,bbox_inches='tight')
The output diagram is roughly as follows:
The above is all the contents of the article "how to draw thematic maps with GeoPandas". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!