4

I have a GeoPandas DataFrame (loaded from a shapefile) containing a number of polygons which looks like this:

                                            geometry  raster_val
0  POLYGON ((8.56799 47.80800, 8.56799 47.77200, ...         1.0
1  POLYGON ((6.73199 47.12400, 6.73199 47.08800, ...         1.0
2  POLYGON ((10.36799 47.01600, 10.36799 46.98000...         1.0
3  POLYGON ((8.60399 46.51200, 8.60399 46.47600, ...         1.0

Is there any elegant way to print/plot those polygons into a .jpg file?

ksnn
  • 141
  • 1
  • 2
  • 11
  • 2
    I have never used GeoPandas but looking at the mapping documentation it seems that you just need to call your_dataframe.plot(). You can pass optional parameters for further customization and then simply call plt.savefig() to save the figure as a .jpg file. Let me know if the plot() method works so I can expand this comment as an answer. – Marcelo Villa Nov 28 '19 at 19:57
  • @MarceloVilla That way works and would be my suggested answer. I have nothing to add so I leave it for you to answer. – sbphd Dec 02 '19 at 07:42

1 Answers1

5

Taking a look at the geopandas mapping documentation, it seems that you can plot a GeoDataFrame by calling the plot() method on it. Here is one example from the documentation:

import geopandas
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
world.plot()

enter image description here

To export the plot as a .jpg file, you can simply use the matplotlib.pyplot.savefig() function. The code would be similar to:

import matplotlib.pyplot as plt
plt.savefig('world.jpg')

You can, of course, modify the plot by passing several arguments to the plot() method.

Marcelo Villa
  • 5,928
  • 2
  • 19
  • 38
  • That's great! Thanks! But what if I already have a figure which I'm displaying (like plt.imshow(ndvi_layer, cmap='RdYlGn')) and I want to draw those polygons on top of that? – ksnn Dec 04 '19 at 14:13
  • @ksnn As it is a rather different question than the original one you posted, I'd suggest creating a new question and linking it here so I can answer it. – Marcelo Villa Dec 04 '19 at 14:55
  • Sure! I just posted it here https://gis.stackexchange.com/questions/344076/plotting-goedataframe-on-raster – ksnn Dec 05 '19 at 15:57