Coloring the Intersection of Circles/Patches in Matplotlib

To color the intersection of circles/patches in Matplotlib, we use geometric operations to separate overlapping areas. This technique involves creating circular patches and using set operations to identify and color distinct regions.

Required Libraries

We need three key libraries for this task ?

import shapely.geometry as sg
import matplotlib.pyplot as plt
import descartes

Creating Overlapping Circles

First, we create two overlapping circular patches using Shapely's geometry operations ?

import shapely.geometry as sg
import matplotlib.pyplot as plt
import descartes

# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create two overlapping circles
circle_a = sg.Point(-.5, 0).buffer(1.)
circle_b = sg.Point(0.5, 0).buffer(1.)

print("Circle A center:", (-0.5, 0))
print("Circle B center:", (0.5, 0))
print("Radius:", 1.0)
Circle A center: (-0.5, 0)
Circle B center: (0.5, 0)
Radius: 1.0

Identifying Distinct Regions

We use set operations to separate the circles into three distinct regions ?

import shapely.geometry as sg
import matplotlib.pyplot as plt
import descartes

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create two overlapping circles
circle_a = sg.Point(-.5, 0).buffer(1.)
circle_b = sg.Point(0.5, 0).buffer(1.)

# Get distinct regions using set operations
left_only = circle_a.difference(circle_b)      # Only in A
right_only = circle_b.difference(circle_a)     # Only in B
intersection = circle_a.intersection(circle_b)  # In both A and B

# Create the plot
ax = plt.gca()

# Add patches with different colors
ax.add_patch(descartes.PolygonPatch(left_only, fc='blue', ec='black', alpha=0.7))
ax.add_patch(descartes.PolygonPatch(right_only, fc='red', ec='black', alpha=0.7))
ax.add_patch(descartes.PolygonPatch(intersection, fc='green', ec='black', alpha=0.7))

# Set plot properties
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
ax.set_aspect('equal')
ax.axis('off')

plt.show()

How It Works

The geometric operations work as follows:

  • difference() − Returns the part of one shape not overlapping with another
  • intersection() − Returns only the overlapping area between two shapes
  • buffer() − Creates a circular area around a point with specified radius

Customizing Colors and Transparency

You can customize the appearance with different colors and transparency levels ?

import shapely.geometry as sg
import matplotlib.pyplot as plt
import descartes

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create circles with different positions
circle_a = sg.Point(-0.3, 0).buffer(0.8)
circle_b = sg.Point(0.3, 0).buffer(0.8)

# Get regions
left_only = circle_a.difference(circle_b)
right_only = circle_b.difference(circle_a)
intersection = circle_a.intersection(circle_b)

ax = plt.gca()

# Custom colors with transparency
ax.add_patch(descartes.PolygonPatch(left_only, fc='purple', ec='darkblue', alpha=0.6))
ax.add_patch(descartes.PolygonPatch(right_only, fc='orange', ec='darkred', alpha=0.6))
ax.add_patch(descartes.PolygonPatch(intersection, fc='yellow', ec='darkgreen', alpha=0.8))

ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
ax.set_aspect('equal')
ax.axis('off')

plt.show()

Conclusion

Using Shapely's geometric operations with Matplotlib allows precise control over overlapping regions. The difference() and intersection() methods enable clean separation and coloring of distinct areas in complex shapes.

Updated on: 2026-03-25T23:39:36+05:30

940 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements