Matplotlib plt.Circle(): Draw a Circle – Matplotlib Tutorial

By | May 20, 2022

In this tutorial, we will introduce you how to draw a circle using python matplotlib. It is easy to use.

plt.Circle()

This function is defined as:

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

Here xy = (x, y), it will draw a true circle at center xy = (x, y) with given radius

We should notice: plt.Circle() is the child class of artist.Artist

Circle<–Ellipse<—Patch<—artist.Artist

It means we should use matplotlib.axes.Axes.add_artist() function to draw a circle.

Matplotlib draw a circle example 1

This example will draw a circle without creating an axis.

For example:

import numpy as np
import matplotlib.pyplot as plt

draw_circle = plt.Circle((0., 0.), 10, fill=False)
print(type(draw_circle)) # <class 'matplotlib.patches.Circle'>

plt.xlim(xmin=-15,xmax=15)
plt.ylim(ymin=-15,ymax=15)

plt.show()

Run this code, we will see:

Here we have not created an axis, in order to get it, we use plt.gcf().gca() to get an axis on the current figure.

Matplotlib draw a circle example 2

If we have created an axis, we can do as follows:

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

draw_circle = plt.Circle((0., 0.), 10, fill=False)
print(type(draw_circle)) # <class 'matplotlib.patches.Circle'>

plt.xlim(xmin=-15,xmax=15)
plt.ylim(ymin=-15,ymax=15)

fig.suptitle('matplotlib draw a circle')
plt.show()

Run this code, we will see:

How about fill = True?

For example:

draw_circle = plt.Circle((0., 0.), 10, fill=True)
print(type(draw_circle)) # <class 'matplotlib.patches.Circle'>

plt.xlim(xmin=-15,xmax=15)
plt.ylim(ymin=-15,ymax=15)

We will see:

How to change the color of a circle?

We can use some set methods to change the color of a circle.

For example:

draw_circle = plt.Circle((0., 0.), 10, fill=True)
draw_circle.set_facecolor("red")
draw_circle.set_edgecolor("green")
draw_circle.set_linewidth(5)

Run this code, we will see: