You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用matplotlib.patches绘制带阴影的四分之三圆形或圆弧

用matplotlib.patches绘制带阴影的四分之三圆形

可以通过matplotlib.patches.Wedge实现带阴影的四分之三圆形,以下是具体实现代码和说明:

实现步骤

  1. 导入依赖库
import matplotlib.pyplot as plt
from matplotlib.patches import Wedge
  1. 创建画布与坐标轴
fig, ax = plt.subplots(figsize=(5, 5))
  1. 定义图形参数
  • 中心坐标:center = (0.5, 0.5)
  • 半径:r = 0.3
  • 3/4圆形的角度范围:起始角度theta1 = 90,终止角度theta2 = 90 + 270(逆时针方向覆盖270度,即缺右上角1/4圆)
  1. 绘制阴影(手动偏移实现)
# 阴影楔形:向右下偏移,浅灰色,层级低于主图形
shadow_wedge = Wedge((center[0]+0.02, center[1]-0.02), r, theta1, theta2, color='#e0e0e0', zorder=1)
ax.add_patch(shadow_wedge)
  1. 绘制主3/4圆形
# 主图形:白色填充,黑色边框,层级高于阴影
main_wedge = Wedge(center, r, theta1, theta2, color='white', edgecolor='black', linewidth=1.5, zorder=2)
ax.add_patch(main_wedge)
  1. 调整坐标轴显示
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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 14:08:31