Polygon border unions in matplotlib

I am trying to build a union of several polygons in matplotlibwith a certain alpha level. My current code below has a darker color at the intersections. Would it be the same to make intersections of the same color with other places?

import matplotlib.pyplot as plt

fig, axs = plt.subplots()
axs.fill([0, 0, 1, 1], [0, 1, 1, 0], alpha=.25, fc='r', ec='none')
axs.fill([0.5, 0.5, 1.5, 1.5], [0.5, 1.5, 1.5, 0.5], alpha=.25, fc='r', ec='none')

Generated chart

+4
source share
1 answer

As @unutbu and @Martin Valgur comment, I think slim is the way to go. This question may be somewhat redundant with an earlier one, but here is a clean piece of code that should do what you need.

, (), . , "" , - .

import shapely.geometry as sg
import shapely.ops as so
import matplotlib.pyplot as plt

#constructing the first rect as a polygon
r1 = sg.Polygon([(0,0),(0,1),(1,1),(1,0),(0,0)])

#a shortcut for constructing a rectangular polygon
r2 = sg.box(0.5,0.5,1.5,1.5)

#cascaded union can work on a list of shapes
new_shape = so.cascaded_union([r1,r2])

#exterior coordinates split into two arrays, xs and ys
# which is how matplotlib will need for plotting
xs, ys = new_shape.exterior.xy

#plot it
fig, axs = plt.subplots()
axs.fill(xs, ys, alpha=0.5, fc='r', ec='none')
plt.show() #if not interactive

Marking the union of two rectangles

+5

Source: https://habr.com/ru/post/1621816/


All Articles