# How to Draw a circle in Matplotlib?

In this tutorial, we will look at how to draw a circle in a matplotlib plot with the help of some examples.

Some of the ways using which you can draw a circle in a matplotlib plot are –

1. Using the `matplotlib.patches.Circle` method.
2. Using the equation of a circle.
3. Using a scatter plot marker as a circle.

## Method 1 – Using the `matplotlib.patches.Circle` method

You can use the `Circle` class available in the `matplotlib.patches` module to create a circle (a circular patch) by providing information like the center coordinates and the radius of the circle to draw.

Basic Syntax:

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

Parameters:

• xy: a set(x,y) representing the coordinates of the center of the circle.

To understand more about the remaining parameters, refer this.

Now let us understand the above method with some worked-out examples.

### Example 1 – Draw a circle with filled area

```import matplotlib
import matplotlib.pyplot as plt
figure, axes = plt.subplots()
cc = matplotlib.patches.Circle(( 0.5 , 0.5 ), 0.4, color='r')
axes.set_aspect( 1 )
plt.show()```

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

By default, the circular patches created using `matplotlib.pyplot.Circle` are filled with the given color (in the above example, red).

In the above example, we –

1. First, generate the subplots using `matplotlib.pyplot.subplots()` (refer this).
2. Create a circle using the `matplotlib.patches.Circle` class.
3. Set the aspect_ratio to 1 and add the circle we created to the axes we initially generated.
4. And finally, we plot the figure.

### Example 2 – Draw a circle in matplotlib without filling the inside area

You can pass `fill=False` as an argument to the `matplotlib.patches.Circle` method to avoid filling the area inside the circular patch.

```import matplotlib.pyplot as plt
figure, axes = plt.subplots()
cc = matplotlib.patches.Circle(( 0.5 , 0.5 ), 0.4, fill=False)
axes.set_aspect( 1 )
plt.show()```

Output:

Now the circle is not filled inside.

In the above example, we –

1. First, generate the subplots using `matplotlib.pyplot.subplots()` .
2. Create a circle using `matplotlib.patches.Circle`, note that we pass `fill=False` to avoid filling the area inside the circle.
3. Set the aspect_ratio to 1 and added the circle we created to the axes we initially generated.
4. And finally, we plot the figure.

## Method 2 – Using the equation of a Circle

Here, in this method, we’ll try to create a circle from the equation of a circle.
The following equations describe a circle –

```x = r cos θ
y = r sin θ```

where, x, y are the coordinates of the points and r is the radius of the circle, and the value of θ ranges between [0,2π] (π = 180 degrees (or) 3.141 radians).

The idea here is to get the x and y coordinates of a circle using the above equations and then directly plot the coordinates as a curve on a matplotlib plot.

Now let us understand this above method with some worked-out examples.

### Example 1 – Draw a simple circle

```import numpy as np
import matplotlib.pyplot as plt
# array of angles
angle = np.linspace( 0 , 2 * np.pi , 150 )
# the x-coordinates
x = radius * np.cos( angle )
# the y-coordinates
y = radius * np.sin( angle )
# create a subplot
figure, axes = plt.subplots( 1 )
# plot the circle coordinates on the subplot
axes.plot( x, y )
# set the aspect ratio
axes.set_aspect( 1 )
# display the plot
plt.show()```

Output:

In the above example, we –

1. First, generate the values of θ between its range using `np.linspace` method(refer this).
2. Define the radius of the circle to be 0.4 and generate all the x and y coordinates from the known respective equations of the circle.
3. Generate a subplot using `matplotlib.pyplot.subplots()` and plot the circle using the x and y coordinates generated above.
4. Set the aspect ratio to 1.
5. And finally, we plot the figure.

### Example 2 – Draw a circle with customizations

You can also customize how the circle appears in this method. For example, change the line color, the line style, etc. Let’s plot the above circle again but with a green line color and a dash-dot line style.

```import numpy as np
import matplotlib.pyplot as plt
# array of angles
angle = np.linspace( 0 , 2 * np.pi , 150 )
# the x-coordinates
x = radius * np.cos( angle )
# the y-coordinates
y = radius * np.sin( angle )
# create a subplot
figure, axes = plt.subplots( 1 )
# plot the curve with customiztions
axes.plot( x, y , color='g', linestyle='dashdot')
# set the aspect ratio
axes.set_aspect( 1 )
# display the plot
plt.show()```

Output:

You can see that the circle is now formatted. Note that to customize the circle (the curve), we pass additional arguments to the `axes.plot()` function.

## Method 3 – Scatter plot to plot the circle

This is a more hack-ish approach where we draw a circle by creating a scatter plot with circle markers of large sizes. It is the circle marker that we say is the circle plotted on the graph.

Thus, the idea is to create a scatter plot with only a single point and use a very large marker size to display a circle (a circular marker) at that point.

Basic Syntax:

`matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwargs)`

PARAMETERS:

• x – x-axis data
• y – y-axis data
• s– marker size
• c – color of the sequence of colors for markers
• marker – marker style
• cmap – cmap name
• linewidths – width of marker border
• edgecolor – marker border-color

For more details about the arguments, refer this.

Now let us understand the above method with some worked-out examples.

### Example – Point circle using scatter

Let’s create a circle with the point `(0, 0)` as the center and we will use a large value as the marker size.

Code:

```import matplotlib.pyplot as plt
plt.scatter( 0 , 0 , s = 7000 )
plt.xlim( -0.85 , 0.85 )
plt.ylim( -0.95 , 0.95 )
plt.show()```

Output:

By default, the markers are filled, so, the circles created will be a filled circles.

Here, we’re directly trying to create a scatter with position 0,0 and a marker size of 7000 such that the point looks like a circle. Besides that, we’re adjusting the axes xlim from -0.85 to 0.85 and ylim from -0.95 to 0.95 so that the point lies within the axes. Then, we’re plotting the figure.

## Summary

In this tutorial, we looked at the following different ways to create a circle on a matplotlib plot –

• Using the `matplotlib.patches.Circle()` method – you need to specify the center coordinates and the radius.
• Using the equation of a circle to generate x and y values and plot it as a curve.
• Using scatter plot markers as circles.

You might also be interested in –