2009-07-29 235 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