简体   繁体   中英

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)]

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

在此处输入图片说明


If you are using Jupyter Notebook or Jupyter Lab, you could wrap the list of points in aMultiPoint 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:
在此处输入图片说明

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM