Row and column headers in matplotlib's subplots
将行和列标题添加到在
您能提出更好的选择吗?
有几种方法可以做到这一点。 最简单的方法是利用图的y标签和标题,然后使用
如果轴上没有y标签,则很容易利用第一行和第一列的标题和y标签。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import matplotlib.pyplot as plt cols = ['Column {}'.format(col) for col in range(1, 4)] rows = ['Row {}'.format(row) for row in ['A', 'B', 'C', 'D']] fig, axes = plt.subplots(nrows=4, ncols=3, figsize=(12, 8)) for ax, col in zip(axes[0], cols): ax.set_title(col) for ax, row in zip(axes[:,0], rows): ax.set_ylabel(row, rotation=0, size='large') fig.tight_layout() plt.show() |
如果您确实有y标签,或者您希望有更多的灵活性,则可以使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import matplotlib.pyplot as plt from matplotlib.transforms import offset_copy cols = ['Column {}'.format(col) for col in range(1, 4)] rows = ['Row {}'.format(row) for row in ['A', 'B', 'C', 'D']] fig, axes = plt.subplots(nrows=4, ncols=3, figsize=(12, 8)) plt.setp(axes.flat, xlabel='X-label', ylabel='Y-label') pad = 5 # in points for ax, col in zip(axes[0], cols): ax.annotate(col, xy=(0.5, 1), xytext=(0, pad), xycoords='axes fraction', textcoords='offset points', size='large', ha='center', va='baseline') for ax, row in zip(axes[:,0], rows): ax.annotate(row, xy=(0, 0.5), xytext=(-ax.yaxis.labelpad - pad, 0), xycoords=ax.yaxis.label, textcoords='offset points', size='large', ha='right', va='center') fig.tight_layout() # tight_layout doesn't take these labels into account. We'll need # to make some room. These numbers are are manually tweaked. # You could automatically calculate them, but it's a pain. fig.subplots_adjust(left=0.15, top=0.95) plt.show() |
上面的答案有效。 只是在第二版答案中您没有:
1 2 3 4 | for ax, row in zip(axes[:,0], rows): ax.annotate(col, xy=(0, 0.5), xytext=(-ax.yaxis.labelpad-pad,0), xycoords=ax.yaxis.label, textcoords='offset points', size='large', ha='right', va='center') |
代替:
1 2 3 4 | for ax, row in zip(axes[:,0], rows): ax.annotate(row,xy=(0, 0.5), xytext=(-ax.yaxis.labelpad-pad,0), xycoords=ax.yaxis.label, textcoords='offset points', size='large', ha='right', va='center') |