# 如何在 Matplotlib 中繪製圓

Suraj Joshi 2023年1月30日

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

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