除了@olenscki的回答之外,这里有一个关于单元格背景颜色的技巧。
正如@user1185790和@Jorge所说,如果合并单元格,则
cell.set_facecolor()
会出现错误。您可以通过像下面这样
重叠背景表来避免这种情况。
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
def mergecells(table, cells):
'''
Merge N matplotlib.Table cells
Parameters
-----------
table: matplotlib.Table
the table
cells: list[set]
list of sets od the table coordinates
- example: [(0,1), (0,0), (0,2)]
Notes
------
https://dev59.com/jLDla4cB1Zd3GeqP9IBW#53819765
'''
cells_array = [np.asarray(c) for c in cells]
h = np.array([cells_array[i+1][0] - cells_array[i][0] for i in range(len(cells_array) - 1)])
v = np.array([cells_array[i+1][1] - cells_array[i][1] for i in range(len(cells_array) - 1)])
if not np.any(h):
cells = np.array(sorted(list(cells), key=lambda v: v[1]))
edges = ['BTL'] + ['BT' for i in range(len(cells) - 2)] + ['BTR']
elif not np.any(v):
cells = np.array(sorted(list(cells), key=lambda h: h[0]))
edges = ['TRL'] + ['RL' for i in range(len(cells) - 2)] + ['BRL']
else:
raise ValueError("Only horizontal and vertical merges allowed")
for cell, e in zip(cells, edges):
table[cell[0], cell[1]].visible_edges = e
txts = [table[cell[0], cell[1]].get_text() for cell in cells]
tpos = [np.array(t.get_position()) for t in txts]
trans = (tpos[-1] - tpos[0])/2
txts[0].set_transform(mpl.transforms.Affine2D().translate(*trans))
for txt in txts[1:]:
txt.set_visible(False)
contents = (
("Apple", "Banana", "Strawberry", "Melon"),
("Apple", "Banana", "Strawberry", "Melon"),
("Apple", "Banana", "Strawberry", "Melon")
)
bg_colors = (
("r", "y", "r", "g"),
("r", "y", "r", "g"),
("r", "y", "r", "g")
)
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.axis("off")
ax1.set_title("Figure 1")
table = ax1.table(cellText=contents, bbox=[0, 0, 1, 1], cellLoc="center", cellColours=bg_colors)
mergecells(table, [(0, 1), (1, 1), (2, 1)])
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
ax2.axis("off")
ax2.set_title("Figure 2")
table_bg = ax2.table(bbox=[0, 0, 1, 1], cellColours=bg_colors)
for cell in table_bg._cells.values():
cell.set_edgecolor("none")
bg_none = (
("none", "none", "none", "none"),
("none", "none", "none", "none"),
("none", "none", "none", "none")
)
table = ax2.table(cellText=contents, bbox=[0, 0, 1, 1], cellLoc="center", cellColours=bg_none)
mergecells(table, [(0, 1), (1, 1), (2, 1)])
plt.show()
![Wrong cells color](https://istack.dev59.com/keiCN.webp)
![Corrected cells color](https://istack.dev59.com/n1wdq.webp)