【发布时间】: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