2013-06-05 101 views
95

我不太明白爲什麼我無法在指定的限制下創建水平和垂直線。我想通過此框限制數據。但是,雙方似乎不遵守我的指示。爲什麼是這樣?matplotlib中的垂直和水平線

# CREATING A BOUNDING BOX 
# BOTTOM HORIZONTAL 
plt.axhline(y=.4, xmin=0.25, xmax=0.402, linewidth=2, color = 'k') 
# RIGHT VERTICAL 
plt.axvline(x=0.402, ymin=0.4, ymax = 0.615, linewidth=2, color='k') 
# LEFT VERTICAL 
plt.axvline(x=0.1, ymin=0.58, ymax = 0.79, linewidth=2, color='k') 
plt.show() 

enter image description here

回答

131

的pyplot功能要調用,跨越軸範圍的一部分,無論座標axhline()axvline()畫線。參數xminymin使用值0.0作爲軸的最小值,使用1.0作爲軸的最大值。

取而代之,使用plt.plot((x1, x2), (y1, y2), 'k-')從顏色k中的點(x1,y1)到點(x2,y2)繪製一條線。見pyplot.plot

+2

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.axhline < - 文件。你應該編輯你的答案,包括這個鏈接 – tacaswell

+0

@tcaswell,謝謝,應該做到這一點。 –

+11

繪製水平線和垂直線的另一個解決方案是使用['hlines'](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hlines)或['vlines'](http:// matplotlib .org/api/pyplot_api.html#matplotlib.pyplot.vlines),因爲這比普通的'plot'更加微不足道 – hooy

13

如果你想添加一個邊框,使用矩形:

ax = plt.gca() 
r = matplotlib.patches.Rectangle((.5, .5), .25, .1, fill=False) 
ax.add_artist(r) 

Rectangle doc