Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib GridSpec中的行标题_Python_Matplotlib - Fatal编程技术网

Python matplotlib GridSpec中的行标题

Python matplotlib GridSpec中的行标题,python,matplotlib,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

我有一个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()
现在,我想为左侧部分创建一个行式标签,如下所示:

我尝试将另一个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…
行似乎会删除之前创建的轴

下面的注释,我得出了以下答案(我不太喜欢,但似乎有效)


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

看起来你希望
gs\u medium
位于
gs\u outer
的内部,而
gs\u row1
位于
gs\u medium
的内部。如果你只是创建一个n*m的网格,并使用
rowspan
colspan
关键字来获得类似的布局,这难道不可行吗?如果我理解正确,这将导致每个“真实”数据线上方隐藏轴。我认为这可能会很麻烦,因为需要调整大小(因为与包含acutal图的线相比,行跨度轴的长度会非常小)
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[:, :]))