matplotlib GridSpec中的行标题

时间:2016-01-13 22:26:10

标签: python matplotlib

我有一个GridSpec定义的布局与子网格,一个应该包括一个颜色栏

import pylab as plt
import numpy as np
gs_outer = plt.GridSpec(1, 2, width_ratios=(10, 1))
gs_inner = plt.matplotlib.gridspec.GridSpecFromSubplotSpec(2, 3, gs_outer[0])
ax = []
for i in xrange(6):
    ax.append(plt.subplot(gs_inner[i]))
    plt.setp(ax[i].get_xticklabels(), visible=False)
    plt.setp(ax[i].get_yticklabels(), visible=False)
ax.append(plt.subplot(gs_outer[1]))
plt.show()

我现在想要在左侧部分添加如下行标记:enter image description here

我尝试将另一个GridSpec添加到GridSpec中,但这没有用完:

import pylab as plt
import numpy as np
fig = plt.figure()
gs_outer = plt.GridSpec(1, 2, width_ratios=(10, 1))
gs_medium = plt.matplotlib.gridspec.GridSpecFromSubplotSpec(3, 1, gs_outer[0])
ax_title0 = plt.subplot(gs_medium[0])
ax_title0.set_title('Test!')
gs_row1 = plt.matplotlib.gridspec.GridSpecFromSubplotSpec(1, 3, gs_medium[0])
ax00 = plt.subplot(gs_row1[0]) # toggle this line to see the effect
plt.show()

添加ax00 = plt.subplot...行似乎会删除先前创建的轴

2 个答案:

答案 0 :(得分:0)

在CT朱的评论之后,我想出了以下答案(我不是很喜欢它,但似乎有效)

import pylab as plt
import numpy as np
fig = plt.figure()
rows = 2
cols = 3
row_fraction = 9
row_size = row_fraction / float(rows)
gs_outer = plt.GridSpec(1,2, width_ratios=(9,1))
gs_plots= plt.matplotlib.gridspec.GridSpecFromSubplotSpec(rows * 2, cols, subplot_spec=gs_outer[0], height_ratios = rows * [1, row_size])
# Create title_axes
title_ax = []
for ta in xrange(rows):
    row_index = (ta) * 2
    title_ax.append(plt.subplot(gs_plots[row_index, :]))
# Create Data axes
ax = []
for row in xrange(rows):
    row_index = (row + 1) * 2 -1
    for col in xrange(cols):
        try:
           ax.append(plt.subplot(gs_plots[row_index, col], sharex=ax[0], sharey=ax[0]))
        except IndexError:
            if row == 0 and col == 0:
                ax.append(plt.subplot(gs_plots[row_index, col]))
            else:
                raise IndexError
    # Delete Boxes and Markers from title axes
    for ta in title_ax:
        ta._frameon = False
        ta.xaxis.set_visible(False)
        ta.yaxis.set_visible(False)
    # Add labels to title axes:
    for ta, label in zip(title_ax, ['Row 1', 'Row 2']):
        plt.sca(ta)
        plt.text(
            0.5, 0.5, label, horizontalalignment='center', verticalalignment='center')
# Add common colorbar
gs_cb = plt.matplotlib.gridspec.GridSpecFromSubplotSpec(
    1, 1, subplot_spec=gs_outer[1])
ax.append(plt.subplot(gs_cb[:, :]))

当然可以改进标签和蜱标签。但是如何实现这一点可能已经在SO上解释过了。

答案 1 :(得分:0)

让我们定义一个示例网格 pltgrid

pltgrid = gridspec.GridSpec(ncols=3, nrows=2,
    width_ratios=[1]*3, wspace=0.3,
    hspace=0.6, height_ratios=[1]*2)

for 循环之前,您可以使用 map 定义一个列表 ax

num=list(range(7))
ax=list(map(lambda x : 'ax'+str(x), num))

您可能有一个包含名称的列表 plotnames。例如,我将为 for 循环中的每个 i 绘制一个正态分布 Q-Q 图:

for i in xrange(6):
    ax[i]=fig.add.subplot(pltgrid[i])
    res = stats.probplot(x, dist="norm", plot=ax[i])
    # set title for subplot using existing 'plotnames' list
    ax[i].set_title(plotnames[i])
    # display subplot
    ax[i]