You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matplotlib绘制两个表格时均出现额外列的问题求助

解决Matplotlib表格出现额外列的问题

问题根源在于**cellText参数需要传入二维数组**——每行对应表格的一行,每个元素对应该行的列值。但你当前传入的是一维数组,导致Matplotlib无法匹配3个列标签,从而出现额外列。

修正方法是将cellText改为二维数组结构,确保每行包含3个元素(和列标签数量一致)。以下是修正后的完整代码:

from matplotlib import pyplot as plt

header= ['Country','HDI','IPV']

header_colors1 = ['#03a1fc', '#03a1fc', '#03a1fc']
row_colors1 = ['#f1f1f2', 'w'] * 10

header_colors2 = ['#dd8452', '#dd8452', '#dd8452']
row_colors2 = ['w', '#f1f1f2'] * 10

fig, axs = plt.subplots(1, 2, figsize=(8, 4))
fig.patch.set_visible(False)

axs[0].axis('off')
axs[0].axis('tight')

# 改为10行3列的二维数组,替换成你实际的数据即可
least_dev_values = [[f"Country{x}", f"{0.7+x*0.02:.2f}", f"{10+x*2}"] for x in range(10)]
most_dev_values = [[f"Country{x}", f"{0.9-x*0.01:.2f}", f"{50-x*3}"] for x in range(10)]

least_dev_tabel= axs[0].table(cellText=least_dev_values, colLabels=header, loc='center',
         cellLoc='center', colLoc='center', rowColours=row_colors1,
         colColours=header_colors1)

axs[1].axis('off')
axs[1].axis('tight')
axs[1].table(cellText=most_dev_values, colLabels=header, loc='center',
         cellLoc='center', colLoc='center', rowColours=row_colors2,
         colColours=header_colors2)

plt.subplots_adjust(wspace=0.8)
plt.show()

你只需要把代码中模拟的二维数据替换成你实际的3列数据,就能保证表格列数和标签完全匹配,不会出现额外列。

内容的提问来源于stack exchange,提问作者Thiago Roque

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 04:37:39