Draw and Color Overlapping Circles with Shapely in Python
Question:
I am trying to draw and color overlapping circles using Shapely in Python. I’ve looked at several similar examples but keep running into problems with depreciated functions (like cascaded_union)
Here’s my code so far:
import matplotlib.pyplot as plt
from shapely.geometry import Point, Polygon, MultiPoint, MultiPolygon
def plot_coords(coords):
pts = list(coords)
x, y = zip(*pts)
plt.plot(x,y)
def plot_polys(polys):
for poly in polys:
plot_coords(poly.exterior.coords)
points = [Point(0, 0),
Point(2,0),
Point(1,2),
Point(-1,2),
Point(-2,0),
Point(-1,-2),
Point(1,-2)]
for point in points:
plot_polys([point.buffer(2.2)])
plt.show()
The output looks like so:
From here, I would like to fill in the circles with colors I choose. I want the overlapping areas to have mixed colors, but to do this I need to figure out how to define the overlap. I tried following along here with unary_union, but I am a bit lost how to do this on a series of buffered points.
Any help would be greatly appreciated! I’m also open to other methods of accomplishing this goal.
Answers:
maybe a step towards a solution, you can try the matplotlib.pyplot.fill_between methods for your circles and use the alpha argument to have different colors in overlying sections.
import matplotlib.pyplot as plt
from shapely.geometry import Point, Polygon, MultiPoint, MultiPolygon
def plot_coords(coords, col):
pts = list(coords)
x, y = zip(*pts)
plt.plot(x, y, c=col)
def plot_polys(polys, col):
for poly in polys:
plot_coords(poly.exterior.coords, col=col)
# the line I added
plt.fill_between(*poly.exterior.xy, alpha=.5, color=col)
# define list of colors
colors = ['r', 'r', 'r', 'k', 'k', 'k', 'g', 'g']
# convert list to generator for easy iteration in loop
color_gen = (col for col in colors)
points = [Point(0, 0),
Point(2, 0),
Point(1, 2),
Point(-1, 2),
Point(-2, 0),
Point(-1, -2),
Point(1, -2)]
for point in points:
plot_polys([point.buffer(2.2)], col=next(color_gen))
plt.show()
I am trying to draw and color overlapping circles using Shapely in Python. I’ve looked at several similar examples but keep running into problems with depreciated functions (like cascaded_union)
Here’s my code so far:
import matplotlib.pyplot as plt
from shapely.geometry import Point, Polygon, MultiPoint, MultiPolygon
def plot_coords(coords):
pts = list(coords)
x, y = zip(*pts)
plt.plot(x,y)
def plot_polys(polys):
for poly in polys:
plot_coords(poly.exterior.coords)
points = [Point(0, 0),
Point(2,0),
Point(1,2),
Point(-1,2),
Point(-2,0),
Point(-1,-2),
Point(1,-2)]
for point in points:
plot_polys([point.buffer(2.2)])
plt.show()
The output looks like so:
From here, I would like to fill in the circles with colors I choose. I want the overlapping areas to have mixed colors, but to do this I need to figure out how to define the overlap. I tried following along here with unary_union, but I am a bit lost how to do this on a series of buffered points.
Any help would be greatly appreciated! I’m also open to other methods of accomplishing this goal.
maybe a step towards a solution, you can try the matplotlib.pyplot.fill_between methods for your circles and use the alpha argument to have different colors in overlying sections.
import matplotlib.pyplot as plt
from shapely.geometry import Point, Polygon, MultiPoint, MultiPolygon
def plot_coords(coords, col):
pts = list(coords)
x, y = zip(*pts)
plt.plot(x, y, c=col)
def plot_polys(polys, col):
for poly in polys:
plot_coords(poly.exterior.coords, col=col)
# the line I added
plt.fill_between(*poly.exterior.xy, alpha=.5, color=col)
# define list of colors
colors = ['r', 'r', 'r', 'k', 'k', 'k', 'g', 'g']
# convert list to generator for easy iteration in loop
color_gen = (col for col in colors)
points = [Point(0, 0),
Point(2, 0),
Point(1, 2),
Point(-1, 2),
Point(-2, 0),
Point(-1, -2),
Point(1, -2)]
for point in points:
plot_polys([point.buffer(2.2)], col=next(color_gen))
plt.show()