【问题标题】:Exact Subfigure sizing in matplotlibmatplotlib 中的精确子图大小
【发布时间】:2013-07-28 16:03:56
【问题描述】:

我在 A4 纸上绘制了一些索引数据,其中包含 3x2 行列中的 6 张图像。我已经完成了基本代码,并将此数据帧数据绘制到以下 6 个图中。

http://imgur.com/frNjth3

def plot_idx(df,image,key):
    # reset the default parameters
    plt.rcParams['font.size'] = 8.
    plt.rcParams['figure.figsize'] = (8.267, 11.692) #aA paper
    fig = plt.figure()
    #plot all the 6 figures and save the 3x2 image as png

    ax1 = fig.add_subplot(321) # first row first image
    #compute all time 
    alltime = df['Close'].count()
    x,y,x_min, y_min, x_max, y_max = min_max(df,alltime)
    ax1.plot(x, y,'r')
    ax1.plot(x_min,y_min,'o')
    ax1.plot(x_max,y_max,'o')
    ax1.set_xlabel('year')
    ax1.set_ylabel('Index Close')
    ax1.set_title(plot_title[-1])
    ax1.fill_between(x,y,facecolor='red')
    ax1.annotate(y_min, xy=(x_min,y_min), xytext=(x_min,y_min +250))
    ax1.annotate(y_max, xy=(x_max,y_max), xytext=(x_max,y_max +250))

    ax2 = fig.add_subplot(322) # first row second image
    # compute ytd 
    ytd = df.ix[baseline_year:]['Close'].count()
    x,y,x_min, y_min, x_max, y_max = min_max(df,ytd)
    ax2.plot(x, y,'r')
    ....
    # repeat 4 more times for other figs

    fig.suptitle(key, fontsize=10)
    plt.tight_layout()
    plt.savefig(image)

如何将子地块放入 A4/6 大小相等的地块中,并在标题顶部留出一点空间?如 8.267/2 x 11.69/3 尺寸? tight_layout 有帮助,但我想更好地控制尺寸和位置。

【问题讨论】:

  • 我的回答解决了你的问题吗?

标签: python matplotlib subplot


【解决方案1】:

如果你想要更多的控制,你可以使用fig.add_axesdoc

fig = plt.figure()
ax1 = fig.add_axes([.1, .1, .4, .4,])
ax2 = fig.add_axes([.1, .5, .4, .4,])
# ... and so one for as many figures as you want
# or wrap it all up in a loop

缺点是写/维护真的很烦人,因为你必须确保刻度和轴标签不会相互重叠。

还有GridSpec 可以让您更好地控制跨列等。

【讨论】:

  • 矩形的值在子图的上下文中意味着什么?我可以看到 l,b,w,h 部分,但是 left, bottom 是什么意思?
  • leftbottom 是坐标轴左下角的位置,以数字分数单位表示。
猜你喜欢
  • 2015-04-14
  • 2015-07-15
  • 2017-08-19
  • 1970-01-01
  • 2017-11-14
  • 2015-08-15
  • 2021-08-05
  • 2017-03-01
相关资源
最近更新 更多