matplotlib的子图中的行和列标题 [英] Row and column headers in matplotlib's subplots

查看:212
本文介绍了matplotlib的子图中的行和列标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将行和列标题添加到在matplotlib中的循环中生成的子图网格中的最佳实践是什么?我能想到几个,但不是特别整洁:

What's the best practise to add a row and a column header to a grid of subplots generated in a loop in matplotlib? I can think of a couple, but not particularly neat:

  1. 对于列,如果有一个计数器来循环,则只能将set_title()用于第一行.对于行,这是行不通的.您将不得不在图外绘制text.
  2. 您在顶部添加了一排子图,在左侧添加了一列子图,并在该子图的中间绘制了文本.
  1. For columns, with a counter to your loop you can use set_title() for the first row only. For rows this doesn't work. You would have to draw text outside of the plots.
  2. You add an extra row of subplots on top and an extra column of subplots on the left, and draw text in the middle of that subplot.

您能提出更好的选择吗?

Can you suggest a better alternative?

推荐答案

有几种方法可以做到这一点.最简单的方法是利用图的y标签和标题,然后使用fig.tight_layout()为标签腾出空间.或者,您可以使用annotate将其他文本放置在正确的位置,然后以半手动方式为其留出空间.

There are several ways to do this. The easy way is to exploit the y-labels and titles of the plot and then use fig.tight_layout() to make room for the labels. Alternatively, you can place additional text in the right location with annotate and then make room for it semi-manually.

如果轴上没有y标签,则很容易利用第一行和第一列的标题和y标签.

If you don't have y-labels on your axes, it's easy to exploit the title and y-label of the first row and column of axes.

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标签,或者如果您希望具有更大的灵活性,则可以使用annotate放置标签.这比较复杂,但是除了行和列标签外,还允许您具有单独的打印标题,ylabel等.

If you do have y-labels, or if you prefer a bit more flexibility, you can use annotate to place the labels. This is more complicated, but allows you to have individual plot titles, ylabels, etc in addition to the row and column labels.

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()

这篇关于matplotlib的子图中的行和列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆