2009-07-29 237 views
7

我正在製作一些相當大的圖形,並且邊框中的空白佔用了很多像素,這些像素可以更好地用於數據。看起來邊界隨着圖形的增長而增長。如何限制matplotlib圖上的邊框尺寸?

這裏是我的繪圖代碼的膽量:

 import matplotlib 
     from pylab import figure 

     fig = figure() 
     ax = fig.add_subplot(111) 
     ax.plot_date((dates, dates), (highs, lows), '-', color='black') 
     ax.plot_date(dates, closes, '-', marker='_', color='black') 

     ax.set_title('Title') 
     ax.grid(True) 
     fig.set_figheight(96) 
     fig.set_figwidth(24) 

有減少邊框的大小的方法嗎?也許一個設置的地方,可以讓我保持邊界不變2英寸左右?

回答

6

因爲它看起來像你的SubplotParams '只使用一個單獨的小區,您可能要跳過add_subplot並直接跳至add_axes。這將允許您給出軸的大小(以相對於圖的座標),因此您可以在圖中將其設置得儘可能大。對你來說,這將意味着你的代碼看起來像

import matplotlib.pyplot as plt 

    fig = plt.figure() 

    # add_axes takes [left, bottom, width, height] 
    border_width = 0.05 
    ax_size = [0+border_width, 0+border_width, 
       1-2*border_width, 1-2*border-width] 
    ax = fig.add_axes(ax_size) 
    ax.plot_date((dates, dates), (highs, lows), '-', color='black') 
    ax.plot_date(dates, closes, '-', marker='_', color='black') 

    ax.set_title('Title') 
    ax.grid(True) 
    fig.set_figheight(96) 
    fig.set_figwidth(24) 

如果你願意,你甚至可以把參數set_figheight/set_figwidth直接在figure()通話。

5

嘗試subplots_adjust API:

subplots_adjust(*指定參數時,** kwargs)

fig.subplots_adjust(左=無,底部=無,右=無,wspace =無,HSPACE =無)

更新與kwargs(默認爲c,其中無)和更新的插曲位置

+2

你能提供一個在這種情況下如何使用它的例子嗎? – 2013-02-12 17:22:10