Hello coders!! In this article, we will learn how to make a circle using matplotlib in Python. A circle is a figure of round shape with no corners. There are various ways in which one can plot a circle in matplotlib. Let us discuss them in detail.
Method 1: matplotlib.patches.Circle():
- SYNTAX:
- class
matplotlib.patches.Circle
(xy, radius=r, **kwargs)
- class
- PARAMETERS:
- xy: (x,y) center of the circle
- r: radius of the circle
- RESULT: a circle of radius r with center at (x,y)
import matplotlib.pyplot as plt figure, axes = plt.subplots() cc = plt.Circle(( 0.5 , 0.5 ), 0.4 ) axes.set_aspect( 1 ) axes.add_artist( cc ) plt.title( 'Colored Circle' ) plt.show()
Output & Explanation:

Here, we have used the circle() method of the matplotlib module to draw the circle. We adjusted the ratio of y unit to x unit using the set_aspect() method. We set the radius of the circle as 0.4 and made the coordinate (0.5,0.5) as the center of the circle.
Method 2: Using the equation of circle:
The equation of circle is:
- x = r cos θ
- y = r sin θ
r: radius of the circle
This equation can be used to draw the circle using matplotlib.
import numpy as np import matplotlib.pyplot as plt angle = np.linspace( 0 , 2 * np.pi , 150 ) radius = 0.4 x = radius * np.cos( angle ) y = radius * np.sin( angle ) figure, axes = plt.subplots( 1 ) axes.plot( x, y ) axes.set_aspect( 1 ) plt.title( 'Parametric Equation Circle' ) plt.show()
Output & Explanation:

In this example, we used the parametric equation of the circle to plot the figure using matplotlib. For this example, we took the radius of the circle as 0.4 and set the aspect ratio as 1.
Method 3: Scatter Plot to plot a circle:
A scatter plot is a graphical representation that makes use of dots to represent values of the two numeric values. Each dot on the xy axis indicates value for an individual data point.
- SYNTAX:
matplotlib.pyplot.scatter(x_axis_data, y_axis_data, s=None, c=None, marker=None, cmap=None, vmin=None, vmax=None, alpha=None, linewidths=None, edgecolors=None)
- PARAMETERS:
- x_axis_data- x-axis data
- y_axis_data- y-axis data
- s- marker size
- c- color of sequence of colors for markers
- marker- marker style
- cmap- cmap name
- linewidths- width of marker border
- edgecolor- marker border-color
- alpha- blending value
import matplotlib.pyplot as plt plt.scatter( 0 , 0 , s = 7000 ) plt.xlim( -0.85 , 0.85 ) plt.ylim( -0.95 , 0.95 ) plt.title( "Scatter plot of points Circle" ) plt.show()
Output & Explanation:

Here, we have used the scatter plot to draw the circle. The xlim() and the ylim() methods are used to set the x limits and the y limits of the axes respectively. We’ve set the marker size as 7000 and got the circle as the output.
Method 4: Matplotlib hollow circle:
import matplotlib.pyplot as plt plt.scatter( 0 , 0 , s=10000 , facecolors='none', edgecolors='blue' ) plt.xlim( -0.5 , 0.5 ) plt.ylim( -0.5 , 0.5 ) plt.show()
Output & Explanation:

To make the circle hollow, we have set the facecolor parameter as none, so that the circle is hollow. To differentiate the circle from the plane we have set the edgecolor as blue for better visualization.
Method 5: Matplotlib draw circle on image:
import matplotlib.pyplot as plt import matplotlib.patches as patches import matplotlib.cbook as cb with cb.get_sample_data('C:\\Users\\Prachee\\Desktop\\cds\\img1.jpg') as image_file: image = plt.imread(image_file) fig, ax = plt.subplots() im = ax.imshow(image) patch = patches.Circle((100, 100), radius=80, transform=ax.transData) im.set_clip_path(patch) ax.axis('off') plt.show()
Output & Explanation:

In this example, we first loaded our data and then used the axes.imshow() method. This method is used to display data as an image. We then set the radius and the center of the circle. Then using the set_clip_path() method we set the artist’s clip-path.
Method 6: Matplotlib transparent circle:
import matplotlib.pyplot as plt figure, axes = plt.subplots() cc = plt.Circle(( 0.5 , 0.5 ), 0.4 , alpha=0.1) axes.set_aspect( 1 ) axes.add_artist( cc ) plt.title( 'Colored Circle' ) plt.show()
Output & Explanation:

To make the circle transparent we changed the value of the alpha parameter which is used to control the transparency of our figure.
Conclusion:
With this, we come to an end with this article. These are the various ways in which one can plot a circle using matplotlib in Python.
However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.
Happy Pythoning!
The post 6 Ways to Plot a Circle in Matplotlib appeared first on Python Pool.