简体   繁体   English

如何在点集上绘制矩形?

[英]How to draw rectangle over set of points?

All of my attempts failed. 我所有的尝试都失败了。 I tried to draw rectangle over set of points with pyplot but I keep getting different errors. 我试图用pyplot在点集上绘制矩形,但我不断遇到不同的错误。 Can someone help? 有人可以帮忙吗? I need to add rectangle of size width = 4 and height= 2sqrt(3) 我需要添加宽度为4且高度为2sqrt(3)的矩形

import matplotlib.pyplot as plt
import matplotlib.patches as patches

def main():
    print("hello")

if __name__ == "__main__":
    x = []
    y = []

    for k in range(30):
      for l in range(30):
        x.append(4*k + 2*(l % 2))
        y.append(2*l*3**(1/2))

    rect = patches.Rectangle((0,0),4,2*3**(1/2),linewidth=1,edgecolor='b',facecolor='none') 
    plt.plot(x, y, 'ro')
    plt.axis([0, 10, 0, 10])


    #plt.add_patch(rect)

    plt.show()

add_patch is an axes method, not something directly under pyplot . add_patch是一个axes方法,而不是直接在pyplot下的pyplot Just change your commented line to: 只需将您的评论行更改为:

plt.gca().add_patch(rect)

gca() gets the current active axes in pyplot. gca()获取pyplot中的当前活动轴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM