Search code examples
pythonshapefilegeopandas

Use two shapefiles in geopandas


I have to shapefiles. One is POLYGON and represent administrative districts in Sweden. The other one is POINTS representing the countrys lakes. I have data related to the administrative districts and wants to add the lakes to make the map more beautiful.

This is my script to create a map without lakes.

variable = 'förändring1'
vmin, vmax = -2, 4

fig, ax = plt.subplots(1, figsize=(10, 16))

matplotlib.rcParams["figure.dpi"] = 250
ax.axis('off')
ax.set_title('TITLE, REGIONS\n Q1 2020 - Q2 2021', fontdict={'fontsize': '24', 'fontweight' : '3', 'fontname':'Open Sans'})

ax.annotate('X', xy=(0.7, .05), xycoords='figure fraction', fontsize=11, color='#555555')

legend = plt.legend(title="title",
                    loc=4, fontsize='small', fancybox=True)

geo_df1.plot(edgecolor='black', column=variable, cmap=my_cmp, linewidth=0.3, ax=ax, categorical=True, legend=True,
            legend_kwds=dict(loc='lower right', 
                             title='Förändring\n%-enheter',
                             prop = {'size' : 15}))

My shapefile for the districts looks like this:

LA-kod  LA-namn geometry
0   LA1903  Eskilstuna  POLYGON ((568494.857 6523152.063, 565723.456 6...
1   LA1925  Borås   POLYGON ((396919.129 6351819.960, 395817.199 6...
2   LA1948  Sundsvall   MULTIPOLYGON (((623003.025 6929654.815, 628496...
3   LA1960  Lycksele    POLYGON ((704138.531 7189721.517, 695708.319 7...
4   LA1965  Pajala  POLYGON ((888013.662 7448254.868, 868700.048 7...
... ... ... ...
64  LA1902  Nyköping-Oxelösund  MULTIPOLYGON (((617964.321 6507990.893, 620313...
65  LA1944  Gävle   POLYGON ((583931.306 6691340.733, 576700.277 6...
66  LA1949  Kramfors    MULTIPOLYGON (((640892.553 6993927.028, 639395...
67  LA1934  Örebro  POLYGON ((519879.892 6521506.409, 516154.724 6...
68  LA1921  Halmstad    POLYGON ((373979.424 6248279.617, 374683.890 6...
69 rows × 3 columns

My shapefile for lakes looks like this:

SJOID   geometry
0   631121-147437   POINT (523392.000 6309304.000)
1   707350-149283   POINT (534314.000 7069077.000)
2   633919-152479   POINT (573459.000 6337866.000)
3   763280-176148   POINT (793878.000 7634014.000)
4   656371-143806   POINT (484086.000 6561244.000)
... ... ...
106595  753912-164311   POINT (676771.000 7538815.000)
106596  753912-165416   POINT (687817.000 7538959.000)
106597  753912-174751   POINT (781138.000 7540177.000)
106598  743421-188986   POINT (924825.000 7437141.000)
106599  753913-178160   POINT (815220.000 7540632.000)
106600 rows × 2 columns


Solution

  • Can you try

    ax1=geo_df1.plot()
    geo_df2.plot(ax=ax1)
    

    where, geo_df1 and geo_df2 represent the geodataframes from two shapefiles along with the necessary parameters.