这是我的错误“名称‘矩形’未定义”

2024-09-29 02:19:52 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码?有人知道我哪里出错了吗? 我试着从不同的角度画矩形

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np

X1Y1= (.2,.95)
X2Y2= (.4,.915)
X3Y3= (.6,.835)
X4Y4= (.8,.715)
w= .05
h= .15
angle1= -100
angle2= -110
angle3= -120
angle4= -130
plt.figure(figsize=(10,10))
rect = mpatches.Rectangle(X1Y1,w,h,angle1)
rect = mpatches.Rectangle(X2Y2,w,h,angle2)
rect = mpatches.Rectangle(X3Y3,w,h,angle3)
rect = mpatches.Rectangle(X4Y4,w,h,angle4)
ax = plt.gca()
# Add the patch to the Axes
ax.add_patch(Rectangle((X1Y1),w,h,angle1,color='red'))
ax.add_patch(Rectangle((X2Y2),w,h,angle2,color='blue'))
ax.add_patch(rectangle((X3Y3),w,h,angle3,color='blue'))
ax.add_patch(Rectangle((X4Y4),w,h,angle4,color='red'))
plt.show()

Tags: rectimportaddaspltaxpatchcolor
1条回答
网友
1楼 · 发布于 2024-09-29 02:19:52

它不知道什么是Rectangle。这应该可以解决:

from matplotlib.patches import Rectangle

或者使用mpatches.Rectangle,就像您在前面的块中所做的那样。你知道吗

请注意,您还将其中一个错拼为rectangle。Python区分大小写。你知道吗

相关问题 更多 >