Saturday, November 16, 2024
Google search engine
HomeLanguagesHow to Draw a Circle Using Matplotlib in Python?

How to Draw a Circle Using Matplotlib in Python?

A Circle is a mathematical figure formed by joining all points lying on the same plane and are at equal distance from a given point. We can plot a circle in python using Matplotlib. There are multiple ways to plot a Circle in python using Matplotlib. 

Method 1: Using matplotlib.patches.Circle() function.

Matplotlib has a special function matplotlib.patches.Circle() in order to plot circles. 

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

Example 1: Plotting a colored Circle using matplotlib.patches.Circle()

Python3




# Demonstrating use of matplotlib.patches.Circle() function
# to plot a colored Circle
 
import matplotlib.pyplot as plt
 
figure, axes = plt.subplots()
Drawing_colored_circle = plt.Circle(( 0.6 , 0.6 ), 0.2 )
 
axes.set_aspect( 1 )
axes.add_artist( Drawing_colored_circle )
plt.title( 'Colored Circle' )
plt.show()


Output:

Example 2: Plotting an un-colored Circle using matplotlib.patches.Circle()

Python3




# Demonstrating use of matplotlib.patches.Circle() function
# to plot an un-colored Circle
 
import matplotlib.pyplot as plt
 
figure, axes = plt.subplots()
Drawing_uncolored_circle = plt.Circle( (0.6, 0.6 ),
                                      0.3 ,
                                      fill = False )
 
axes.set_aspect( 1 )
axes.add_artist( Drawing_uncolored_circle )
plt.title( 'Circle' )
plt.show()


Output:

Method 2: Using Circle Equation

Example 1: Plotting a Circle using Parametric equation of a circle

Python3




# Program to plot a Circle
# using Parametric equation of a Circle
 
import numpy as np
import matplotlib.pyplot as plt
 
theta = np.linspace( 0 , 2 * np.pi , 150 )
 
radius = 0.4
 
a = radius * np.cos( theta )
b = radius * np.sin( theta )
 
figure, axes = plt.subplots( 1 )
 
axes.plot( a, b )
axes.set_aspect( 1 )
 
plt.title( 'Parametric Equation Circle' )
plt.show()


Output:

Example 2: Using Center-Radius form of a circle equation

Python3




# Program to plot a Circle
# using Center-Radius form of circle equation
 
import numpy as np
import matplotlib.pyplot as plt
 
x = np.linspace( -0.7 , 0.7 , 150 )
y = np.linspace( -0.7 , 0.7 , 150 )
 
a, b = np.meshgrid( x , y )
 
C = a ** 2 + b ** 2 - 0.2
 
figure, axes = plt.subplots()
 
axes.contour( a , b , C , [0] )
axes.set_aspect( 1 )
 
plt.title( 'Center-Radius form Circle' )
plt.show()


Output:

Method 3: Using the Scatter Plot of points 

Example:

Python3




# Program to plot a Circle
# using Scatter plot of points
 
import matplotlib.pyplot as plt
 
plt.scatter( 0 , 0 , s = 7000 )
plt.title( 'Circle' )
 
plt.xlim( -0.85 , 0.85 )
plt.ylim( -0.95 , 0.95 )
 
plt.title( "Scatter plot of points Circle" )
plt.show()


Output:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments