20.5 C
London
Monday, June 2, 2025
HomePythonMatplotlib in PythonHow to Draw Rectangles in Matplotlib (With Examples)

How to Draw Rectangles in Matplotlib (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

To draw a rectangle in Matplotlib, you can use the matplotlib.patches.Rectangle function, which uses the following syntax:

matplotlib.patches.Rectangle(xy, width, height, angle=0.0)

where:

  • xy: The (x, y) coordinates for the anchor point of the rectangle
  • width: Rectangle width
  • height: Rectangle height
  • angle: Rotation in degrees counter-clockwise about xy (Default is 0)

This tutorial provides several examples of how to use this function in practice.

Example 1: Draw a Rectangle on a Plot

The following code shows how to draw a rectangle on a Matplotlib plot with a width of 2 and height of 6:

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

#define Matplotlib figure and axis
fig, ax = plt.subplots()

#create simple line plot
ax.plot([0, 10],[0, 10])

#add rectangle to plot
ax.add_patch(Rectangle((1, 1), 2, 6))

#display plot
plt.show()

Rectangle in matplotlib

Example 2: Style a Rectangle

The following code shows how to apply some styling to the rectangle:

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

#define Matplotlib figure and axis
fig, ax = plt.subplots()

#create simple line plot
ax.plot([0, 10],[0, 10])

#add rectangle to plot
ax.add_patch(Rectangle((1, 1), 2, 6,
             edgecolor = 'pink',
             facecolor = 'blue',
             fill=True,
             lw=5))

#display plot
plt.show()

Custom rectangle with styling in Matplotlib

You can find a complete list of styling properties that you can apply to a rectangle here.

Example 3: Draw a Rectangle on an Image

The following code shows how to draw a rectangle on an image in Matplotilb. Note that the image used in this example comes from this Matplotlib tutorial.

To replicate this example, just download the photo of the stinkbug from that tutorial and save it to your own computer.

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from PIL import Image

#display the image
plt.imshow(Image.open('stinkbug.png'))

#add rectangle
plt.gca().add_patch(Rectangle((50,100),40,80,
                    edgecolor='red',
                    facecolor='none',
                    lw=4))

Draw rectangle on imagine in matplotlib

Note that we can use the angle argument to rotate the rectangle a certain number of degrees counter-clockwise:

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from PIL import Image

#display the image
plt.imshow(Image.open('stinkbug.png'))

#add rectangle
plt.gca().add_patch(Rectangle((50,100),40,80,
                    angle=30,
                    edgecolor='red',
                    facecolor='none',
                    lw=4))

Rotated rectangle on an image in Matplotlib

Related: How to Plot Circles in Matplotlib (With Examples)

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories