📌  相关文章
📜  如何在Python中使用 Matplotlib 绘制圆?

📅  最后修改于: 2022-05-13 01:55:34.117000             🧑  作者: Mango

如何在Python中使用 Matplotlib 绘制圆?

圆是通过连接位于同一平面上并与给定点等距的所有点形成的数学图形。我们可以使用 Matplotlib 在Python中绘制一个圆。有多种方法可以使用 Matplotlib 在Python中绘制圆。

方法一:使用 matplotlib.patches.Circle()函数。

Matplotlib 有一个特殊的函数matplotlib.patches.Circle() 来绘制圆。

示例 1:使用 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()


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()


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()


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()


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()


输出:

示例 2:使用 matplotlib.patches.Circle() 绘制未着色的圆

蟒蛇3

# 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()

输出:

方法二:使用圆方程

示例 1:使用圆的参数方程绘制圆

蟒蛇3

# 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()

输出:

示例 2:使用圆方程的中心半径形式

蟒蛇3

# 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()

输出:

方法 3:使用点的散点图

例子:

蟒蛇3

# 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()

输出: