46

I want to use Shapely for my computational geometry project. I need to be able to visualize and display polygons, lines, and other geometric objects for this. I've tried to use Matplotlib for this but I am having trouble with it.

from shapely.geometry import Polygon
import matplotlib.pyplot as plt

polygon1 = Polygon([(0,5),
                    (1,1),
                    (3,0),
                    ])

plt.plot(polygon1)
plt.show()

I would like to be able to display this polygon in a plot. How would I change my code to do this?

Georgy
  • 6,348
  • 7
  • 46
  • 58
ebeb9
  • 379
  • 1
  • 3
  • 12
  • 3
    1. Nicely written first question! 2. If you can get individual lists of the `x` and `y` coordinates for your polygon you can plot like: `plt.plot(polygon_x,polygon_y)`. You'll also have to append the first elements to the end to get the final line drawn. I unfortunately know nothing about shapely but hopefully this helps! – Hoog Apr 04 '19 at 18:20
  • 3
    It would be `plt.plot(*polygon1.exterior.xy)`. More generally you can plot shapely objects through `descates`. Googling for "shapely matplotlib" should already give you relevant results. – ImportanceOfBeingErnest Apr 04 '19 at 19:40
  • http://geopandas.org/ may be worth investigating for this functionality. – daryl Apr 05 '19 at 05:32

4 Answers4

71

Use:

import matplotlib.pyplot as plt

x,y = polygon1.exterior.xy
plt.plot(x,y)

Or, more succinctly:

plt.plot(*polygon1.exterior.xy)
Nitay
  • 3,223
  • 5
  • 31
  • 41
ap21
  • 1,490
  • 1
  • 10
  • 22
13

A little late but I find the most convenient way to do this is with Geopandas as suggested above but without writing to a file first.

from shapely.geometry import Polygon
import matplotlib.pyplot as plt
import geopandas as gpd

polygon1 = Polygon([(0,5),
                    (1,1),
                    (3,0),
                    ])

 p = gpd.GeoSeries(polygon1)
 p.plot()
 plt.show()

Polygon Plotted using Geopandas

Checkout the docs for Geopandas.GeoSeries

Micah Johnson
  • 301
  • 2
  • 8
7

If your data is in a .shp file, I would recommend geopandas:

import geopandas as gpd
import matplotlib.pyplot as plt

shapefile = gpd.read_file("path/to/shapes.shp")
shapefile.plot()
plt.show()

Mitchell Tracy
  • 925
  • 1
  • 10
  • 15
  • 1
    Any way to plot this without writing to file first? – CGFoX Apr 29 '20 at 09:22
  • 1
    You may be able to get away with wrapping the string in a [StringIO](https://docs.python.org/3/library/io.html) as shown in [this question](https://stackoverflow.com/questions/141449/how-do-i-wrap-a-string-in-a-file-in-python), and passing the result to `gpd.read_file` though I have not tested this. – Mitchell Tracy Apr 30 '20 at 13:23
2

It might be an overkill, but as an alternative to other good comments I would add an option of installing QGIS - a free software for working with geometries. All you need to do is to save your geometries as a shape file (.shp), geoJSON or any other format and open it with QGIS. If you're planning a big project it maybe more convenient at the end than using matplotlib.

  • Thank you. I am building a project that visualizes the Ham Sandwich theorem for my computational geometry course. – ebeb9 Apr 13 '19 at 16:49