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

Matplotlib修改表格特定单元格字号时全部单元格字号异常变更的问题求助

Matplotlib修改表格特定单元格字号时全部单元格字号异常变更的问题求助

我想通过Python的Matplotlib修改表格里特定单元格的字号,但每次操作后,整个表格所有单元格的字号都跟着变了,完全不是我想要的效果。

下面是我写的代码:

# 1. Define the variable
import matplotlib.pyplot as plt
from matplotlib.table import Table

# Dummy data
data = [["Name", "Age", "Affiliation"],
        ["Bam", "12", "didoda"],
        ["Gireogi", "13", "Jadory"]]

# Define rows and columns
n_rows, n_cols = len(data), len(data[0])
width, height = 1.0 / n_cols, 1.0 / n_rows

# 2. Import Table and create figure
fig, ax = plt.subplots(figsize=(6, 3))
ax.axis('tight')
ax.axis('off')

table = Table(ax, bbox=[0, 0, 1, 1])

# 4. Add dummy data as cells
for i in range(n_rows):
    for j in range(n_cols):
        cell_text = data[i][j]
        table.add_cell(i, j, width, height, text=cell_text, loc='center', edgecolor='black')

# 3. Refer to what I want to change (specific cell)
cell = table.get_celld()[(1, 2)]  # Access cell at row 1, column 2
print(type(cell))  # Confirm the cell type
cell.set_fontsize(3.5)  # Adjust font size for this specific cell

# Add the table to the axis
ax.add_table(table)

# Display the table
plt.show()

我实在搞不懂为什么会这样。我明明是获取了第1行第2列的单元格(内容是"didoda"),调用的是这个Cell对象的set_fontsize()方法,只想要把这个单元格的字号改成3.5,其他单元格保持默认大小就行,但结果所有单元格的字号都变成3.5了,输出的表格里所有文字都变得特别小。

备注:内容来源于stack exchange,提问作者GUN MA

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 17:58:04