gpt4 book ai didi

python pyplot : how to organize the layout?

转载 作者:行者123 更新时间:2023-12-01 05:08:59 27 4
gpt4 key购买 nike

查看 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) produces a subaxes in a figure which represents the top plot (i.e. the first) in a 2 row by 1 column notional grid (no grid actually exists, but conceptually this is how the returned subplot has been positioned).

我现在尝试向左侧的列添加一行(总共 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()

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

最佳答案

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

关于python pyplot : how to organize the layout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24546404/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com