How to plot a rectangle inside a circle in Matplotlib?

To plot a rectangle inside a circle in Matplotlib, we can use the patches module to create geometric shapes and add them to a plot. This technique is useful for data visualization, geometric illustrations, and creating custom plot elements.

Step-by-Step Approach

Here are the main steps to create this visualization ?

  • Create a new figure using figure() method

  • Add a subplot to the current axes

  • Create rectangle and circle instances using Rectangle() and Circle() classes

  • Add patches to the axes using add_patch()

  • Set axis limits and ensure equal scaling

  • Display the figure using show() method

Basic Example

Let's create a simple rectangle inside a circle ?

import matplotlib.pyplot as plt
import matplotlib.patches as patches

# Create figure and subplot
fig, ax = plt.subplots(figsize=(8, 8))

# Create a circle centered at (0, 0) with radius 3
circle = patches.Circle((0, 0), radius=3, color='lightblue', alpha=0.7)

# Create a rectangle centered inside the circle
# Rectangle parameters: (x, y, width, height)
rectangle = patches.Rectangle((-2, -1), 4, 2, color='yellow', alpha=0.8)

# Add patches to the axes
ax.add_patch(circle)
ax.add_patch(rectangle)

# Set equal aspect ratio and axis limits
ax.set_xlim(-4, 4)
ax.set_ylim(-4, 4)
ax.set_aspect('equal')

# Add grid and labels for better visualization
ax.grid(True, alpha=0.3)
ax.set_title('Rectangle Inside Circle')

plt.show()

Multiple Rectangles Example

You can add multiple rectangles with different colors and positions ?

import matplotlib.pyplot as plt
import matplotlib.patches as patches

fig, ax = plt.subplots(figsize=(8, 8))

# Create circle
circle = patches.Circle((0, 0), radius=3, color='lightcoral', alpha=0.6, linewidth=2)

# Create multiple rectangles
rect1 = patches.Rectangle((-2.5, -0.5), 2, 1, color='blue', alpha=0.7)
rect2 = patches.Rectangle((0.5, -1.5), 1.5, 3, color='green', alpha=0.7)
rect3 = patches.Rectangle((-1, 1), 2, 0.8, color='purple', alpha=0.7)

# Add all patches
ax.add_patch(circle)
ax.add_patch(rect1)
ax.add_patch(rect2)
ax.add_patch(rect3)

# Configure axes
ax.set_xlim(-4, 4)
ax.set_ylim(-4, 4)
ax.set_aspect('equal')
ax.grid(True, alpha=0.3)
ax.set_title('Multiple Rectangles Inside Circle')

plt.show()

Key Parameters

Understanding the main parameters for creating shapes ?

Shape Parameters Description
Circle (x, y), radius Center coordinates and radius
Rectangle (x, y), width, height Bottom-left corner and dimensions
color Color name/code Fill color of the shape
alpha 0.0 to 1.0 Transparency level

Customization Options

You can customize the appearance with borders and styling ?

import matplotlib.pyplot as plt
import matplotlib.patches as patches

fig, ax = plt.subplots(figsize=(8, 8))

# Circle with custom border
circle = patches.Circle((0, 0), radius=3, 
                       facecolor='lightsteelblue', 
                       edgecolor='navy', 
                       linewidth=3, 
                       alpha=0.8)

# Rectangle with custom styling
rectangle = patches.Rectangle((-1.5, -1), 3, 2, 
                            facecolor='gold', 
                            edgecolor='darkorange', 
                            linewidth=2, 
                            alpha=0.9)

ax.add_patch(circle)
ax.add_patch(rectangle)

ax.set_xlim(-4, 4)
ax.set_ylim(-4, 4)
ax.set_aspect('equal')
ax.set_title('Styled Rectangle and Circle')

plt.show()

Conclusion

Use matplotlib.patches to create geometric shapes like rectangles and circles. The add_patch() method adds shapes to axes, while set_aspect('equal') ensures proper proportions for circular shapes.

Updated on: 2026-03-25T19:51:29+05:30

847 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements