【问题标题】:python pyplot : how to organize the layout?python pyplot:如何组织布局?
【发布时间】:2014-08-24 03:31:08
【问题描述】:

查看 matplotlib 文档,我找到了这个例子:

http://matplotlib.org/users/tight_layout_guide.html

import matplotlib.pyplot as plt

def example_plot(ax,pid, fontsize=12):
    ax.plot([1, 2])
    ax.locator_params(nbins=3)
    ax.set_xlabel('x-label', fontsize=fontsize)
    ax.set_ylabel('y-label', fontsize=fontsize)
    ax.set_title('Title'+str(pid), fontsize=fontsize)

plt.close('all')
fig = plt.figure()

ax1 = plt.subplot(221)
ax2 = plt.subplot(223)
ax4 = plt.subplot(122)

example_plot(ax1,1)
example_plot(ax2,2)
example_plot(ax4,4)

plt.tight_layout()

plt.show()

它产生一个 2 列布局,左侧是两行的列,右侧是 1 行的列。 这似乎与 subplot 的 API 所说的相符: http://matplotlib.org/api/pyplot_api.html

subplot(211) 在图中生成一个子轴,表示顶部 在 2 行 x 1 列的名义网格(无网格)中绘制(即第一个) 确实存在,但从概念上讲,这就是返回的子图的方式 已定位)。

我现在尝试在左侧的列中添加一行(总共 3 行)。 据我了解,应该这样做:

import matplotlib.pyplot as plt

def example_plot(ax,pid, fontsize=12):
    ax.plot([1, 2])
    ax.locator_params(nbins=3)
    ax.set_xlabel('x-label', fontsize=fontsize)
    ax.set_ylabel('y-label', fontsize=fontsize)
    ax.set_title('Title'+str(pid), fontsize=fontsize)

plt.close('all')
fig = plt.figure()

ax1 = plt.subplot(321) # changed "2" by "3"
ax2 = plt.subplot(323) # changed "2" by "3"
ax3 = plt.subplot(324) # line added
ax4 = plt.subplot(122)

example_plot(ax1,1)
example_plot(ax2,2)
example_plot(ax3,3) # line added
example_plot(ax4,4)

plt.tight_layout()

plt.show()

我一定做错了,因为这显示了正确的布局,但是第一列的第三个图没有显示出来......

【问题讨论】:

    标签: python layout matplotlib


    【解决方案1】:

    当您进行 3 x 2 子图网格时,左列中的图编号为 1、3 和 5。将行更改为ax3 = plt.subplot(325),它应该可以工作。

    【讨论】:

    • 谢谢...只是为了确保我理解为什么,这是因为数字 4 用于右列上的“虚构”(行跨度)图?
    • 正确。你先给行编号,在本例中为 1、2; 3、4;和 5、6。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多