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

如何使用matplotlib在显示的图像上对已绘制的矩形进行30度旋转操作

你使用的matplotlib.patches.Rectangle类本身就支持旋转设置,只需要在实例化矩形时添加angle参数即可实现30度旋转,参数值单位为角度,默认以矩形左下角为旋转锚点。

修改后的完整代码如下:

import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image

im = Image.open(r'path...\aero1.jpg')

# Create figure and axes
fig, ax = plt.subplots()

# Display the image
ax.imshow(im)

# 创建旋转30度的矩形,默认以左下角为旋转中心
rect = patches.Rectangle((200, 200), 40, 30, linewidth=1, edgecolor='r', facecolor='none', angle=30)

# 如果需要以矩形中心为旋转锚点,可额外添加rotation_point参数:
# rect = patches.Rectangle((200, 200), 40, 30, linewidth=1, edgecolor='r', facecolor='none', angle=30, rotation_point='center')

# Add the patch to the Axes
ax.add_patch(rect)

plt.show()

效果展示图

内容的提问来源于stack exchange,提问作者user_mechtronics_13

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 11:39:01