Hello-experts
Hello-experts

Reputation: 123

How to plot a list of Shapely points

I created a list of Shapely Point objects based on the point data set. How can I plot this list of points below?

points = [Point(-4.85624511894443, 37.1837967179202), 
          Point(-4.855703975302475, 37.18401757756585),
          Point(-4.85516283166052, 37.1842384372115),
          Point(-4.85343407576431, 37.182006629169),
          Point(-4.85347524651836, 37.1804461589773),
          Point(-4.855792124429867, 37.18108913443582),
          Point(-4.85624511894443, 37.1837967179202)]

Upvotes: 7

Views: 15124

Answers (2)

Ohad
Ohad

Reputation: 69

the easiest way is to use Geopandas and build a GeoDataFrame.

from the tutorial:

import geopandas as gpd
from shapely.geometry import Point
d = {'col1': ['P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7'], 'geometry': [Point(-4.85624511894443, 37.1837967179202), 
      Point(-4.855703975302475, 37.18401757756585),
      Point(-4.85516283166052, 37.1842384372115),
      Point(-4.85343407576431, 37.182006629169),
      Point(-4.85347524651836, 37.1804461589773),
      Point(-4.855792124429867, 37.18108913443582),
      Point(-4.85624511894443, 37.1837967179202)]}
gdf = gpd.GeoDataFrame(d, crs="EPSG:4326")(choose your own crs)

and then just plot it:

gdf.plot()

Upvotes: 1

Georgy
Georgy

Reputation: 13747

You can get two lists of x and y coordinates by accessing x and y attributes of Point and then use, for example, plt.scatter or plt.plot functions of Matplotlib as follows:

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

points = [Point(-4.85624511894443, 37.1837967179202), 
          Point(-4.855703975302475, 37.18401757756585),
          Point(-4.85516283166052, 37.1842384372115),
          Point(-4.85343407576431, 37.182006629169),
          Point(-4.85347524651836, 37.1804461589773),
          Point(-4.855792124429867, 37.18108913443582),
          Point(-4.85624511894443, 37.1837967179202)]
xs = [point.x for point in points]
ys = [point.y for point in points]
plt.scatter(xs, ys)
# or plt.plot(xs, ys) if you want to connect points by lines

enter image description here


If you are using Jupyter Notebook or Jupyter Lab, you could wrap the list of points in a MultiPoint object to get an SVG image. This can be useful for debugging purposes when you want to plot something quickly without importing Matpotlib.

>>> MultiPoint(points)

gives:
enter image description here

Upvotes: 11

Related Questions