Matplotlib - draw rectangle outside of unit square

时间:2015-07-28 17:02:25

标签: python matplotlib

I would like to draw rectangle with bottom left corner at (2,3) and height and width (3,4) in matplotlib. There is a great tutorial Rectangle tutorial, but it only talks about drawing within unit square. There is even question on stackoverflow.com how to draw outside unit square stackoverflow.com, but when I ran it, it does not draw anything, it only displays axis of unit square. Plus, I am not sure why in the example we use

someX - .5, someY - .5

Any suggestions?

1 个答案:

答案 0 :(得分:1)

您链接的教程中的第一个示例工作正常,您只需要调整轴以查看矩形,如果您在调用它时将其绘制在统一方块之外:

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

fig1 = plt.figure()
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.add_patch(
    patches.Rectangle(
        (2, 3),   # (x,y)
        3,          # width
        4,          # height
    )
)
ax1.axis([0, 10, 0, 10])
fig1.savefig('rect1.png', dpi=90, bbox_inches='tight')

注意savefig命令之前的ax1.axis行。