Home » How to Plot Circles in Matplotlib (With Examples)

How to Plot Circles in Matplotlib (With Examples)

by Tutor Aspire

You can quickly add circles to a plot in Matplotlib by using the Circle() function, which uses the following syntax:

matplotlib.patches.Circle(xy, radius=5)

where:

  • xy: The (x, y) coordinates for the circle
  • radius: The radius of the circle. Default is 5.

This tutorial shows several examples of how to use this function in practice:

Example 1: Create a Single Circle

The following code shows how to create a single circle on a Matplotlib plot located at (x, y) coordinates (10,10):

import matplotlib.pyplot as plt

#set axis limits of plot (x=0 to 20, y=0 to 20)
plt.axis([0, 20, 0, 20])

#create circle with (x, y) coordinates at (10, 10)
c=plt.Circle((10, 10))

#add circle to plot (gca means "get current axis")
plt.gca().add_artist(c)

Circle in matplotlib

By default, one axis of a Matplotlib plot typically displays more pixels per data unit. To make a circle appear as a circle instead of an ellipse, you need to use the argument plt.axis(“equal”) as follows:

import matplotlib.pyplot as plt

#set axis limits of plot (x=0 to 20, y=0 to 20)
plt.axis([0, 20, 0, 20])
plt.axis("equal")

#create circle with (x, y) coordinates at (10, 10)
c=plt.Circle((10, 10))

#add circle to plot (gca means "get current axis")
plt.gca().add_artist(c)

Circle matplotlib

Example 2: Create Multiple Circles

The following code shows how to create multiple circles on a Matplotlib plot:

import matplotlib.pyplot as plt

#set axis limits of plot (x=0 to 20, y=0 to 20)
plt.axis([0, 20, 0, 20])
plt.axis("equal")

#define circles
c1=plt.Circle((5, 5), radius=1)
c2=plt.Circle((10, 10), radius=2)
c3=plt.Circle((15, 13), radius=3)

#add circles to plot
plt.gca().add_artist(c1)
plt.gca().add_artist(c2)
plt.gca().add_artist(c3)

Multiple circles in Matplotlib

Example 3: Modify Circle Appearance

You can use the following arguments to modify the appearance of a circle in Matplotlib:

  • radius: Specify radius of circle
  • color: Specify color of circle
  • alpha: Specify transparency of circle

The following code shows an example of how to use several of these arguments at once:

import matplotlib.pyplot as plt

#set axis limits of plot (x=0 to 20, y=0 to 20)
plt.axis([0, 20, 0, 20])
plt.axis("equal")

#create circle with (x, y) coordinates at (10, 10)
c=plt.Circle((10, 10), radius=2, color='red', alpha=.3)

#add circle to plot (gca means "get current axis")
plt.gca().add_artist(c)

Circle with alpha in Matplotlib

Note that you can also use custom hex color codes to specify the color of circles.

You may also like