2017-10-16 57 views
-2

我试图用python绘制一个矩形。有些事情出错了,但我无法弄清楚。python matplotlib patch plot going wrong

下面的代码:

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

box=[1,3,5] 
box_pos=[0,30,4] 

fig, ax = plt.subplots() 
ax.add_patch(
    patches.Rectangle(
     (box_pos[0], box_pos[1]), # (x,y) 
     box[0],   # width 
     box[1],   # height 
    ) 
) 

ax.set_aspect(aspect='equal', adjustable='box') 
plt.show() 

你有任何想法,我可以做些什么来解决这个问题?

+0

“事情错了”?什么是“什么”?请清楚说明你有什么问题。 – ImportanceOfBeingErnest

回答

0

当您添加patches到matplotlib Axes,它不会自动设置以同样的方式轴限制其他情节类型,所以做你的极限被设置为默认(0, 1)在x和y,你的矩形超出这些限制。

您有几个选项:

  1. 使用ax.autoscale()plt.show()之前自动让matplotlib设置的轴限制

enter image description here

  • 如果需要更多控制,可以手动设置轴限制,在012之前使用ax.set_xlim()和,例如:

    ax.set_xlim(-20, 20) 
    ax.set_ylim(0, 40) 
    
  • enter image description here