首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用subplot2grid和“AxesSubplot”对象

使用subplot2grid和“AxesSubplot”对象
EN

Stack Overflow用户
提问于 2021-04-13 18:53:15
回答 2查看 217关注 0票数 0

我使用subplot2grid来定义一个图的网格,如下所示。工作很好,这是一个很好的功能。

代码语言:javascript
复制
    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列上。

代码语言:javascript
复制
    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中使用另一个函数来完成这个任务?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-13 19:38:05

我被你想做的事弄糊涂了。然而,另一种处理不同宽度和高度的方法可能是使用宽度比?

编辑:使用子图来保持轴的逻辑组。

代码语言:javascript
复制
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()

票数 2
EN

Stack Overflow用户

发布于 2021-04-14 09:07:45

我认为这是matplotlib的半图形合成函数的一项工作,即subplot_mosaic函数。这可以在matplotlib > 3.3中找到。您需要为5个面板定义基本布局,然后根据需要的行/列生成完整的布局。据我所见,这将是相当复杂和困难(虽然不是不可能!)由subplot2gridGridspec或任何其他方法创建。

代码语言:javascript
复制
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)生成一个单一的面板:

代码语言:javascript
复制
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)上述单一面板的“瓷砖”

代码语言:javascript
复制
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()

注意事项:

  1. empty_sentinal设置为999。如果你有超过999个子图,那就把它增加到一个更高的数目。
  2. 每个“迷你面板”可以单独访问。您可能需要编写其他函数来访问“面板组”。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67080837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档