【问题标题】:Title for each subplotSpec (subplots within subplots)每个 subplotSpec 的标题(子图中的子图)
【发布时间】:2019-10-15 07:06:04
【问题描述】:

我想制作一个具有 2x2 外部布局和这些 2x3 布局中的每一个的地块。我设法使用 gridSpec 让一切正常工作,但我似乎无法为每个 subplotSpec 分配一个标题(有 2x2 subplotSpec)。

Row titles for matplotlib subplot 这原则上应该给我解决方案,但我想知道是否有办法使用gridSpec而不是隐藏白框来实现这一点。

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

netdict = ["chain","V"]
sigdict = ["no","with"]

fig = plt.figure(figsize=(10, 8))
outer = gridspec.GridSpec(2, 2, wspace=0.3, hspace=0.3) # make a 2x2 outer frame

for ni, networktype in enumerate(netdict):
    for si, sigtype in enumerate(sigdict):
        fig.add_subplot(outer[ni*2+si])
        plt.title(networktype+ " " + sigtype) # title for each outer frame
        plt._frameon=False # this should make the unnecessary white frame go away
        inner = gridspec.GridSpecFromSubplotSpec(2, 3, # make 2x3 inner frame
                        subplot_spec=outer[ni*2+si], wspace=0.4, hspace=0.4)
        for ti,tau in enumerate([5.5,12.5]):
            for u, xy_delay in enumerate([0,tau,2*tau]):
                ax = plt.Subplot(fig, inner[ti*3+u])
                ax.plot(np.sin(np.linspace(0,10,0.5))
                ax.set(title=r"$\tau_u={}$".format(xy_delay))
                fig.add_subplot(ax)
plt.show()

【问题讨论】:

  • 必须手工放置。 gridspec.suptitle 是 matplotlib 正在考虑的事情,但还没有完成。

标签: python matplotlib


【解决方案1】:

只是偶然发现了这一点,因为我不得不做类似的事情。我发现一个简单的解决方案是为外部网格创建另一个子图并简单地关闭它们的轴:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure(figsize=(12, 6))
outer = gridspec.GridSpec(1, 2, wspace=0.2, hspace=0.2)

for i, layer in enumerate(layer_list):
    # create inner plots
    inner = gridspec.GridSpecFromSubplotSpec(2, 2,
                    subplot_spec=outer[i], wspace=0.1, hspace=0.1)
    # set outer titles
    ax = plt.Subplot(fig, outer[i])
    ax.set_title("Layer {}".format(layer))
    ax.axis('off')
    fig.add_subplot(ax)
    for j, filter_pos in enumerate(filter_pos_list):
        # create image plot
        ax = plt.Subplot(fig, inner[j])
        # load image
        img = ...
        ax.imshow(img)
        # set inner title
        ax.set_title("Filter {}".format(filter_pos))
        ax.set_xticks([])
        ax.set_yticks([])
        fig.add_subplot(ax)
fig.show()

【讨论】:

  • 谢谢!我注意到“图层”标题与“过滤器”标题处于同一级别。有什么办法可以让这个比真正的子情节标题高一点吗?基本上,类似于为 gridspec 实现 suptitle (目前尚不可用)。
猜你喜欢
  • 1970-01-01
  • 2018-08-30
  • 2021-03-08
  • 2012-09-14
  • 2020-01-14
  • 2017-12-25
  • 1970-01-01
  • 2019-09-10
  • 2016-08-04
相关资源
最近更新 更多