Matplotlib
pyplot
circle plotting
data visualization
Python

plot a circle with Matplotlib.pyplot

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Plotting a circle in Matplotlib is straightforward once you separate the geometric idea from the plotting API. You can either draw the circle from its equation using points, or add a circle patch object to an axes and let Matplotlib render it for you. Both approaches are common in scientific plotting, teaching material, and annotation-heavy dashboards.

Drawing a Circle With a Patch

For most plotting code, matplotlib.patches.Circle is the cleanest approach. You specify the center and radius, add the patch to an axes, and make sure the aspect ratio is equal so the shape does not appear stretched.

python
1import matplotlib.pyplot as plt
2from matplotlib.patches import Circle
3
4fig, ax = plt.subplots()
5
6circle = Circle((2, 3), radius=1.5, edgecolor="navy", facecolor="none", linewidth=2)
7ax.add_patch(circle)
8
9ax.set_xlim(0, 5)
10ax.set_ylim(0, 6)
11ax.set_aspect("equal")
12ax.grid(True)
13
14plt.show()

This method works well when the circle is an annotation or overlay on an existing chart. It is also easy to style with fill color, transparency, and line width.

Drawing a Circle From Coordinates

If you want explicit control over the points, use the parametric form of a circle. Generate a range of angles, then compute x and y coordinates with sine and cosine.

python
1import numpy as np
2import matplotlib.pyplot as plt
3
4theta = np.linspace(0, 2 * np.pi, 200)
5radius = 2.0
6center_x = 1.0
7center_y = -1.0
8
9x = center_x + radius * np.cos(theta)
10y = center_y + radius * np.sin(theta)
11
12plt.plot(x, y, color="crimson")
13plt.gca().set_aspect("equal")
14plt.show()

This version is useful when you need the coordinates for further computation, collision checks, or custom transformations before plotting.

Why the Circle Sometimes Looks Like an Ellipse

The most common plotting issue is forgetting to set an equal aspect ratio. By default, Matplotlib scales the x and y axes independently to fit the figure, so a mathematically correct circle can look distorted on screen.

Use either of these lines:

python
ax.set_aspect("equal")

or

python
plt.axis("equal")

Without one of them, the circle may render as an ellipse even though the code that generated the points is correct.

Adding Filled Circles and Labels

You can also fill the circle or place it inside a larger plot with labels and legends.

python
1import matplotlib.pyplot as plt
2from matplotlib.patches import Circle
3
4fig, ax = plt.subplots()
5
6zone = Circle((0, 0), radius=3, facecolor="gold", edgecolor="black", alpha=0.4, label="range")
7ax.add_patch(zone)
8
9ax.scatter([0], [0], color="black")
10ax.text(0.1, 0.1, "center")
11ax.set_xlim(-4, 4)
12ax.set_ylim(-4, 4)
13ax.set_aspect("equal")
14ax.legend()
15
16plt.show()

This pattern is common in physics diagrams, map overlays, and visualizations where the circle represents a boundary or effective radius.

Common Pitfalls

The first pitfall is using only a few points in the coordinate method. The plot still works, but the circle looks polygonal rather than smooth. Increase the number of angle samples if the curve appears jagged.

Another mistake is forgetting to adjust axis limits. A correctly drawn circle can be partly off-screen if the viewing window is too small.

It is also common to mix the stateful plt API and the object-oriented ax API inconsistently. Both styles work, but sticking to one style in a function makes the code easier to maintain.

Summary

  • Use Circle from matplotlib.patches when you want a simple shape object on an axes.
  • Use sine and cosine with angle samples when you need explicit circle coordinates.
  • Always set an equal aspect ratio, or the circle may look distorted.
  • Increase point count for smoother coordinate-based circles.
  • Set axis limits deliberately so the full circle is visible in the final plot.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.