如何使用 Matplotlib 繪製半黑半白的圓形?


若要使用 Matplotlib 繪製半黑半白的圓形,我們可以採取以下步驟 −

  • 設定圖形大小並調整子圖之間的內邊距和周圍的填充。
  • 建立一個圖形和一組子圖。
  • 初始化 theta1theta2 以從 theta1 繪製邊直至 theta2,反之亦然。
  • 在當前座標上新增楔形例項.
  • 透過更改軸限制,設定等比例縮放。
  • 設定 xy 縮放。
  • 使用 show() 方法顯示該圖形。

示例

import matplotlib.pyplot as plt
from matplotlib.patches import Wedge

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig, ax = plt.subplots()
theta1, theta2 = 0, 0 + 180
radius = 2
center = (0, 0)
w1 = Wedge(center, radius, theta1, theta2, fc='black', edgecolor='black')
w2 = Wedge(center, radius, theta2, theta1, fc='white', edgecolor='black')

for wedge in [w1, w2]:
   ax.add_artist(wedge)

ax.axis('equal')
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)

plt.show()

輸出

更新於: 16-Jun-2021

1 千次以上的瀏覽

開啟你的職業

獲得認證以完成課程

開始
廣告