# 如何在 Matplotlib 中绘制圆

Suraj Joshi 2023年1月30日 2020年6月9日 Matplotlib Matplotlib Artist

• `matplotlib.patches.Circle()` 方法
• 圆方程
• 点的散点图

## 在 Matplotlib 中使用 `matplotlib.patches.Circle()` 方法绘制圆

``````matplotlib.patches.Circle((x, y), r=5, **kwargs)
``````

``````import matplotlib.pyplot as plt

figure, axes = plt.subplots()
draw_circle = plt.Circle((0.5, 0.5), 0.3)

axes.set_aspect(1)
plt.title('Circle')
plt.show()
``````

``````import matplotlib.pyplot as plt

figure, axes = plt.subplots()
draw_circle = plt.Circle((0.5, 0.5), 0.3,fill=False)

axes.set_aspect(1)
plt.title('Circle')
plt.show()
``````

``````import matplotlib.pyplot as plt

figure, axes = plt.subplots()
draw_circle = plt.Circle((0.5, 0.5), 0.3,fill=False)

plt.title('Circle')
axes.set_aspect(1)
plt.show()
``````

## 在 Matplotlib 中用圆方程绘制圆

### 圆的参数方程

``````import numpy as np
import matplotlib.pyplot as plt

theta = np.linspace(0, 2*np.pi, 100)

figure, axes = plt.subplots(1)

axes.plot(a, b)
axes.set_aspect(1)

plt.title('Circle using Parametric Equation')
plt.show()
``````

### 圆方程的中心半径形式

``````import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-0.5, 0.5, 100)
y = np.linspace(-0.5, 0.5, 100)

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.show()
``````

## 点的散点图

``````import matplotlib.pyplot as plt

plt.scatter(0, 0, s=4000)
plt.title('Circle')

plt.xlim(-0.75,0.75)
plt.ylim(-0.75,0.75)

plt.show()
``````

Author: Suraj Joshi

Suraj Joshi is a backend software engineer at Matrice.ai.