如何用matplotlib.patches绘制带阴影的四分之三圆形或圆弧
用matplotlib.patches绘制带阴影的四分之三圆形
可以通过matplotlib.patches.Wedge实现带阴影的四分之三圆形,以下是具体实现代码和说明:
实现步骤
- 导入依赖库
import matplotlib.pyplot as plt from matplotlib.patches import Wedge
- 创建画布与坐标轴
fig, ax = plt.subplots(figsize=(5, 5))
- 定义图形参数
- 中心坐标:
center = (0.5, 0.5) - 半径:
r = 0.3 - 3/4圆形的角度范围:起始角度
theta1 = 90,终止角度theta2 = 90 + 270(逆时针方向覆盖270度,即缺右上角1/4圆)
- 绘制阴影(手动偏移实现)
# 阴影楔形:向右下偏移,浅灰色,层级低于主图形 shadow_wedge = Wedge((center[0]+0.02, center[1]-0.02), r, theta1, theta2, color='#e0e0e0', zorder=1) ax.add_patch(shadow_wedge)
- 绘制主3/4圆形
# 主图形:白色填充,黑色边框,层级高于阴影 main_wedge = Wedge(center, r, theta1, theta2, color='white', edgecolor='black', linewidth=1.5, zorder=2) ax.add_patch(main_wedge)
- 调整坐标轴显示
ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.set_aspect('equal') # 保证圆形比例正确 plt.axis('off') # 隐藏坐标轴 plt.show()
关键说明
Wedge是绘制扇形的核心补丁,通过theta1和theta2控制扇形角度范围,270度的角度差对应3/4圆形。- 阴影效果通过绘制偏移的同形状楔形实现,设置
zorder确保阴影在主图形下方,偏移量可根据需求调整。 - 如果需要其他方向的3/4圆,只需修改
theta1和theta2的数值,比如从0度到270度会得到缺右下角的3/4圆。
内容的提问来源于stack exchange,提问作者meng luo
相关产品推荐
相关产品推荐

