如何使用 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 千次以上的浏览

开启你的职业

获得认证以完成课程

开始
广告
© . All rights reserved.