在 matplotlib 中,如何在不调整其他子图大小的情况下将 table 添加到子图
In matplotlib, how to add a table to a subplot without resizing other subplots
考虑这个例子:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 3, figsize=(9,3), constrained_layout=False)
[ax.plot([[0,0],[1,1]]) for ax in axs]
fig.set_facecolor('silver')
table = axs[0].table(
[[col + str(row) for col in 'ABC'] for row in range(3)],
loc='top',
)
我想在一个子图的顶部(或底部)添加一个 table 并将这个特定的子图缩小 table 的高度,以便 table 不会突出,图形不会放大,所有子图的高度都相同(包括 table)。我不想将 table 放入绘图区域,即 loc='upper center'
.
想法是绘制图形,获取渲染的 table 高度,然后将第一个轴缩小该高度(转换为图形坐标)。由于 table 大小在轴坐标中,因此 table 也会在降低轴高度时缩小。因此,我们必须在 y 方向以相同的系数重新缩放它以获得原始 table 大小并使顶部与其他轴整齐对齐。
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 3, figsize=(9,3), constrained_layout=False)
[ax.plot([[0,0],[1,1]]) for ax in axs]
fig.set_facecolor('silver')
table = axs[0].table(
[[col + str(row) for col in 'ABC'] for row in range(3)],
loc='top',
)
table_box = fig.transFigure.inverted().transform_bbox(table.get_window_extent(fig.canvas.get_renderer()))
ax_box = axs[0].get_position()
axs[0].set_position([ax_box.x0, ax_box.y0, ax_box.width, (ax_box.height-table_box.height)])
table.scale(1, ax_box.height/(ax_box.height-table_box.height))
最后我使用了一个常量 table_height
来设置用于 table 的轴的分数。
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 3, figsize=(9,3))
[ax.plot([[0,0],[1,1]]) for ax in axs]
fig.set_facecolor('silver')
table_height = 0.3
ax_box = axs[0].get_position()
axs[0].set_position(
[ax_box.x0, ax_box.y0, ax_box.width, ax_box.height * (1 - table_height)])
table = axs[0].table(
[[col + str(row) for col in 'ABC'] for row in range(3)],
bbox=[0., 1., 1., table_height / (1 - table_height)]
)
考虑这个例子:
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 3, figsize=(9,3), constrained_layout=False)
[ax.plot([[0,0],[1,1]]) for ax in axs]
fig.set_facecolor('silver')
table = axs[0].table(
[[col + str(row) for col in 'ABC'] for row in range(3)],
loc='top',
)
我想在一个子图的顶部(或底部)添加一个 table 并将这个特定的子图缩小 table 的高度,以便 table 不会突出,图形不会放大,所有子图的高度都相同(包括 table)。我不想将 table 放入绘图区域,即 loc='upper center'
.
想法是绘制图形,获取渲染的 table 高度,然后将第一个轴缩小该高度(转换为图形坐标)。由于 table 大小在轴坐标中,因此 table 也会在降低轴高度时缩小。因此,我们必须在 y 方向以相同的系数重新缩放它以获得原始 table 大小并使顶部与其他轴整齐对齐。
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 3, figsize=(9,3), constrained_layout=False)
[ax.plot([[0,0],[1,1]]) for ax in axs]
fig.set_facecolor('silver')
table = axs[0].table(
[[col + str(row) for col in 'ABC'] for row in range(3)],
loc='top',
)
table_box = fig.transFigure.inverted().transform_bbox(table.get_window_extent(fig.canvas.get_renderer()))
ax_box = axs[0].get_position()
axs[0].set_position([ax_box.x0, ax_box.y0, ax_box.width, (ax_box.height-table_box.height)])
table.scale(1, ax_box.height/(ax_box.height-table_box.height))
最后我使用了一个常量 table_height
来设置用于 table 的轴的分数。
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 3, figsize=(9,3))
[ax.plot([[0,0],[1,1]]) for ax in axs]
fig.set_facecolor('silver')
table_height = 0.3
ax_box = axs[0].get_position()
axs[0].set_position(
[ax_box.x0, ax_box.y0, ax_box.width, ax_box.height * (1 - table_height)])
table = axs[0].table(
[[col + str(row) for col in 'ABC'] for row in range(3)],
bbox=[0., 1., 1., table_height / (1 - table_height)]
)