2017-10-16 29 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