0

I want to produce a plot of the canton of Zürich by by importing a shapefile of Switzerland (https://www.swisstopo.admin.ch/de/geodata/landscape/boundaries3d.html) and zoom into the area of interest. In the last step I want to save the produced image as .eps file. The problem is that the save image file is empty.

First the packages geopandas and matplotlib are imported

import geopandas
import matplotlib.pyplot as plt

Then the data of the shapefile is loaded

gdf = geopandas.read_file("swissBOUNDARIES3D_1_3_TLM_KANTONSGEBIET.shp")

Then the map of Switzerland is plotted. The canton of Zürich is colored different than all other cantons. After this, a zoom onto the canton of Zürich is done.

fig = plt.figure(figsize=(30, 18))
ax2 = gdf.plot(figsize=(30,18), edgecolor='black')
gdf[gdf.NAME == "Zürich"].plot(edgecolor='black', color='lightskyblue', ax=ax2)
gdf[gdf.NAME != "Zürich"].plot(edgecolor='black', color='aliceblue', ax=ax2)
plt.xlim(650000,750000)
plt.ylim(220000,290000)

The produced image should then be saved.


fig.savefig("filename2.eps", format='eps')
plt.show()

But the generated image is empty. It is also empty when the format is changed to png.

MCK1886
  • 13
  • 2

1 Answers1

0

The generated image is empty, because your plot is not on the correct figure. gdf.plot() creates a new figure. In order to save that figure you could remove the call to plt.figure() and get the figure from the axes with:

ax2.figure.savefig("filename2.eps", format='eps')

or create a subplot and pass the created axes as argument to the first gdf.plot():

fig, ax2 = plt.subplots(figsize=(30,18))
gdf.plot(edgecolor='black', ax=ax2)
gdf[gdf.NAME == "Zürich"].plot(edgecolor='black', color='lightskyblue', ax=ax2)
gdf[gdf.NAME != "Zürich"].plot(edgecolor='black', color='aliceblue', ax=ax2)
plt.xlim(650000,750000)
plt.ylim(220000,290000)
fig.savefig("filename2.eps", format='eps')
CodeBard
  • 256
  • 2
  • 4