2017-06-05 71 views
2

這個開始是我繪製的圖表:如何設置軸從角落Matplotlib

# MatPlotlib 
import matplotlib.pyplot as plt 
# Scientific libraries 
import numpy as np 

plt.figure(1) 

points = np.array([(100, 6.09), 
        (111, 8.42), 
        (119, 10.6), 
        (129, 12.5), 
        (139, 14.9), 
        (149, 17.2), 
        (200, 28.9), 
        (250, 40.9), 
        (299, 52.4), 
        (349, 64.7), 
        (400, 76.9)]) 

# get x and y vectors 
x = points[:,0] 
y = points[:,1] 
# calculate polynomial 
z = np.polyfit(x, y, 3) 
f = np.poly1d(z) 
# calculate new x's and y's 
x_new = np.linspace(x[0], x[-1], 50) 
y_new = f(x_new) 

plt.plot(x,y,'bo', x_new, y_new) 

plt.show() 

enter image description here

我發現,所有的圖形我陰謀沒有從角落開始他們的軸箱子裏,有誰能告訴我如何解決這個問題?除了圖中的設置限制

回答

3

默認情況下,matplotlib在軸的所有邊上添加5%的邊距。 要擺脫這個邊際,你可以使用plt.margins(0)

import matplotlib.pyplot as plt 

plt.plot([1,2,3],[1,2,3], marker="o") 
plt.margins(0) 
plt.show() 

enter image description here

要更改頁邊距爲完整的腳本,你可以使用

plt.rcParams['axes.xmargin'] = 0 
plt.rcParams['axes.ymargin'] = 0 

或者,你也許會改變你rc file包括那些設置。

+0

謝謝,這幫了我一把 – Tian