How to plot a list of Shapely points
Asked Answered
E

2

7

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)]
Eparch answered 19/12, 2019 at 23:7 Comment(0)
V
11

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

Varicelloid answered 20/12, 2019 at 15:25 Comment(2)
What method is called that results in the graphics being displayed? For example, if I wanted to loop over a list of Multipoint's, how can I display each?Handknit
May I suggest xs, ys = zip(*[(point.x, point.y) for point in points]) which avoids double looping. Or xs, ys = zip(*[point.xy for point in points]). Or with numpy xs, ys = np.c_[[np.r_[point.xy] for point in points]].TMelisandra
G
1

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()
Gert answered 1/2, 2023 at 14:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.