【问题标题】:How to fit subplot and table into figure area matplotlib如何将子图和表格放入图形区域 matplotlib
【发布时间】:2021-04-14 08:36:02
【问题描述】:

预设或调整 pyplot 图形区域以适应子图和表格的“最佳”方法是什么? 我正在尝试生成一个条形图,它的正下方有一个表格。但是,如果表中的行数超过一定数量,它们就会超出数字限制并且不可见。确保子图和表格符合界限的正确方法是什么。

在提供的示例中,到目前为止,我所发现的只是使用 plt.subplot_adjust(left = 0.2, bottom = 0.2) 调整子图,但“0.2”调整是任意的,每次有一个包含更多或更少行的表时都需要更改。

import csv
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

dataset = [
    [
        "29%",
        "40%",
        "51%",
        "63%",
        "69%",
        "71%",
        "74%",
        "77%",
        "80%",
        "83%",
        "86%",
        "89%",
        "91%",
        "94%",
        "97%",
        "100%",
    ],
    [
        6524.0,
        8749.0,
        10470.0,
        13096.0,
        13126.0,
        12965.0,
        13493.0,
        13717.0,
        14351.0,
        14993.0,
        15308.0,
        14320.0,
        13179.0,
        9809.0,
        10168.0,
        10621.0,
    ],
    [
        6524.0,
        8749.0,
        10470.0,
        13096.0,
        6827.0,
        5586.0,
        7697.0,
        13717.0,
        14351.0,
        14993.0,
        15308.0,
        14320.0,
        13179.0,
        9809.0,
        10168.0,
        10621.0,
    ],
    [
        6827.0,
        5586.0,
        7697.0,
        13717.0,
        6827.0,
        5586.0,
        7697.0,
        8205.0,
        8298.0,
        8733.0,
        8887.0,
        9278.0,
        9659.0,
        9809.0,
        10168.0,
        10621.0,
    ],
]

fig = plt.figure()
ax = fig.add_subplot(111)

order = np.arange(len(dataset[0]))
print(order)

this = dataset[0]
that = dataset[1]
the_other = dataset[2]


## Create Masks to define which bar chart is shown in front (depending on values)

# Always align = center charts
bar1 = ax.bar(x=order, height=that, width=0.5, bottom=0, align="center", alpha=0.5)
bar2 = ax.bar(x=order, height=the_other, width=0.5, bottom=0, align="center", alpha=0.5)


# Set axes limits and labels *the xlim should number of columns +/- half a unit*
ax.set_xlim(-0.5, len(dataset[0]) - 0.5)
# ax.xaxis.set(ticks=order, ticklabels=elev)

# add the table *The table width is set by 1/ncol by default, plays into what the figures xlim should be*
the_table = plt.table(cellText=dataset, cellLoc="center")
plt.xticks([])
# plt.subplots_adjust(left=0.2, bottom=0.2)

plt.show()

【问题讨论】:

  • plt.tight_layout() 在这里工作吗?
  • fig, axs = plt.subplots(2, 1, gridspec_kw={'height_ratios':[2, 1]}),然后把横杆放在axs[0],把桌子放在axs[1],调整高度比例以适应

标签: python matplotlib


【解决方案1】:

为了进行自动调整,您需要向table() 构造函数提供bbox 参数,其中左下角和边界框的高度将根据轴底部和高度位置自动计算。下面是一段代码,我将轴放置在图形上的特定位置,然后从轴坐标中计算出bbox 坐标。

在这个例子中,我设置了fig_height=3,目的是在图的底部显示表格如何调整。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.table import table


dataset = [
    [
        "29%", "40%", "51%", "63%", "69%", "71%", "74%", "77%", "80%", "83%", "86%", "89%", "91%", "94%", "97%", "100%",
    ], [
        6524.0, 8749.0, 10470.0, 13096.0, 13126.0, 12965.0, 13493.0, 13717.0, 14351.0, 14993.0, 15308.0, 14320.0,
        13179.0, 9809.0, 10168.0, 10621.0,
    ], [
        6524.0, 8749.0, 10470.0, 13096.0, 6827.0, 5586.0, 7697.0, 13717.0, 14351.0, 14993.0, 15308.0, 14320.0,
        13179.0, 9809.0, 10168.0, 10621.0,
    ], [
        6827.0, 5586.0, 7697.0, 13717.0, 6827.0, 5586.0, 7697.0, 8205.0, 8298.0, 8733.0, 8887.0, 9278.0,
        9659.0, 9809.0, 10168.0, 10621.0,
    ],
]

# Figure size
fig_width, fig_height = 13, 3

# Axes location
ax_left, ax_bottom, ax_width, ax_height = 0.1, 0.3, 0.8, 0.65

# Create a figure
fig = plt.figure(figsize=(fig_width, fig_height))

# Create axes
ax = plt.axes([ax_left, ax_bottom, ax_width, ax_height])

order = np.arange(len(dataset[0]))

this = dataset[0]
that = dataset[1]
the_other = dataset[2]

# Create Masks to define which bar chart is shown in front (depending on values)

# Always align = center charts
bar1 = ax.bar(x=order, height=that, width=0.5, bottom=0, align="center", alpha=0.5)
bar2 = ax.bar(x=order, height=the_other, width=0.5, bottom=0, align="center", alpha=0.5)


# Set axes limits and labels *the xlim should number of columns +/- half a unit*
ax.set_xlim(-0.5, len(dataset[0]) - 0.5)

# add the table *The table width is set by 1/ncol by default, plays into what the figures xlim should be*
# the_table = plt.table(cellText=dataset, cellLoc="center")

#
# Draw a table in a bounding box in relative axes coordinates
# If set bbox to (0, 0, 1, 1) then the table will be drawn perfectly on top of the chart. This means that
# bbox=(0, 0, 1 1) in axes coordinates is equivalent to (ax_left, ax_bottom, ax_width, ax_height) in figure coordinates.
# So, to place the table in the bottom area we just need to scale axes coordinates to figure coordinates.
#
the_table = table(ax, cellText=dataset, cellLoc="center", bbox=(0, -ax_bottom / ax_height, 1, ax_bottom / ax_height))

plt.xticks([])
plt.show()

你应该得到这样的东西:

如果设置fig_height=7,那么图表看起来会更正常:

为了增加或减少表格占用的高度,只需更改ax_bottom 参数。我会把它保持在 0.1 和 0.35 之间。此外,调整ax_bottom 参数会改变图表在图形上的位置,看起来可能会有所偏移。为了让它看起来不错,只需确保 ax_bottom + ax_height 小于 1 但不能太小。我更喜欢保留ax_bottom + ax_height = 0.95

【讨论】:

    猜你喜欢
    • 2019-10-04
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多