如何在 Matplotlib 中繪製圓內矩形?


要繪製圓內矩形,可以使用以下步驟:

  • 使用figure 方法建立一個新圖或啟用現有圖。

  • 向當前軸新增子圖。

  • 使用 Rectangle() Circle() 類建立矩形和圓形例項。

  • 在軸上新增補丁。

  • 使用 xlim() ylim() 方法調整 x 軸和 y 軸的比例。

  • 使用 show() 方法顯示圖片。

示例

import matplotlib
from matplotlib import pyplot as plt, patches
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111)
rect1 = patches.Rectangle((-2, -2), 4, 2, color='yellow')
circle1 = matplotlib.patches.Circle((0, 0), radius=3, color='red')
ax.add_patch(circle1)
ax.add_patch(rect1)
plt.xlim([-5, 5])
plt.ylim([-5, 5])
plt.axis('equal')
plt.show()

輸出

更新於: 11-May-2021

644 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.