我使用subplot2grid来定义一个图的网格,如下所示。工作很好,这是一个很好的功能。
plot_axes_1 = plt.subplot2grid((6, 4), (0, 0), rowspan=2, colspan=3) ##1
plot_axes_2 = plt.subplot2grid((6, 4), (2, 0), rowspan=2, colspan=3, sharex=scatter_axes_1) ##2
x_hist_axes_2 = plt.subplot2grid((6, 4), (4, 0), colspan=3, sharex=scatter_axes_2) ##3
y_hist_axes_1 = plt.subplot2grid((6, 4), (0, 3), rowspan=2, sharey=scatter_axes_1) ##4
y_hist_axes_2 = plt.subplot2grid((6, 4), (2, 3), rowspan=2, sharey=scatter_axes_2, sharex= y_hist_axes_1) ##5

现在我想把图像中的5个地块作为一个单元,并把它的6个拷贝,排列在3行2列上。
fig, ax= plt.subplots(3,2)
for l in range(3):
for m in range(2):
ax[l,m].subplot2grid((6, 4), (0, 0), rowspan=2, colspan=3) ##1
ax[l,m].subplot2grid((6, 4), (2, 0), rowspan=2, colspan=3, sharex=scatter_axes_1) ##2
ax[l,m].subplot2grid((6, 4), (4, 0), colspan=3, sharex=scatter_axes_2) ##3
ax[l,m].subplot2grid((6, 4), (0, 3), rowspan=2, sharey=scatter_axes_1) ##4
ax[l,m].subplot2grid((6, 4), (2, 3), rowspan=2, sharey=scatter_axes_2, sharex= y_hist_axes_1) ##5但是我不能像这样使用subplot2grid,我得到了错误'AxesSubplot' object has no attribute 'subplot2grid'
我是否还可以在AxesSubplot中使用另一个函数来完成这个任务?
发布于 2021-04-13 19:38:05
我被你想做的事弄糊涂了。然而,另一种处理不同宽度和高度的方法可能是使用宽度比?
编辑:使用子图来保持轴的逻辑组。
import matplotlib.pyplot as plt
fig = plt.figure(constrained_layout=True, figsize=(8, 12))
sfigs = fig.subfigures(3, 2)
for nn, sf in enumerate(sfigs.flat):
sf.suptitle(nn)
axs = sf.subplots(3, 2, gridspec_kw={'width_ratios': [2, 1],
'height_ratios': [2, 2, 1]})
sf.delaxes(axs[2, 1])
plt.show()

发布于 2021-04-14 09:07:45
我认为这是matplotlib的半图形合成函数的一项工作,即subplot_mosaic函数。这可以在matplotlib > 3.3中找到。您需要为5个面板定义基本布局,然后根据需要的行/列生成完整的布局。据我所见,这将是相当复杂和困难(虽然不是不可能!)由subplot2grid或Gridspec或任何其他方法创建。
import matplotlib.pyplot as plt
import numpy as np
def layout(panel, rows=3, cols=2, empty_sentinal=999):
"""Takes in a single layout and arranges it in multiple
rows and columns"""
npanels = rows * cols
panel[panel >= empty_sentinal] = empty_sentinal
minipanels = len(np.unique(panel))
panels = np.array([i * (minipanels) + panel for i in range(npanels)])
panel_rows = [np.hstack(panels[i : i + cols]) for i in range(0, npanels, cols)]
panel_cols = np.vstack(panel_rows)
panel_cols[panel_cols > empty_sentinal] = empty_sentinal
return panel_cols( A)生成一个单一的面板:
single_panel = np.array([
[1, 1, 1, 1, 1, 1, 2, 2, 999],
[1, 1, 1, 1, 1, 1, 2, 2, 999],
[1, 1, 1, 1, 1, 1, 2, 2, 999],
[1, 1, 1, 1, 1, 1, 2, 2, 999],
[3, 3, 3, 3, 3, 3, 4, 4, 999],
[3, 3, 3, 3, 3, 3, 4, 4, 999],
[3, 3, 3, 3, 3, 3, 4, 4, 999],
[3, 3, 3, 3, 3, 3, 4, 4, 999],
[5, 5, 5, 5, 5, 5, 999, 999, 999],
[5, 5, 5, 5, 5, 5, 999, 999, 999],
[5, 5, 5, 5, 5, 5, 999, 999, 999],
[999] * 9,
[999] * 9,
])
fig, ax = plt.subplot_mosaic(single_panel, figsize=(10, 10), empty_sentinel=999)
for k, v in ax.items():
v.set_xticklabels([])
v.set_yticklabels([])
v.text(0.5, 0.5, k, ha="center", va="center", fontsize=25)
plt.show()

(B)上述单一面板的“瓷砖”
my_layout = layout(panel=single_panel, rows=3, cols=2)
fig, ax = plt.subplot_mosaic(my_layout, figsize=(10, 10), empty_sentinel=999)
for k, v in ax.items():
v.set_xticklabels([])
v.set_yticklabels([])
v.text(0.5, 0.5, k, ha="center", va="center", fontsize=25)
plt.show()

注意事项:
empty_sentinal设置为999。如果你有超过999个子图,那就把它增加到一个更高的数目。https://stackoverflow.com/questions/67080837
复制相似问题