Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be3da1a

Browse files
committed
Merge pull request matplotlib#2389 from bjcohen/patch-1
table.py: fix issue when specifying both column header text and color
2 parents db0fbd4 + 4de70b0 commit be3da1a

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

lib/matplotlib/table.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -495,26 +495,29 @@ def table(ax,
495495
if colWidths is None:
496496
colWidths = [1.0 / cols] * cols
497497

498-
# Check row and column labels
498+
# Fill in missing information for column
499+
# and row labels
499500
rowLabelWidth = 0
500501
if rowLabels is None:
501502
if rowColours is not None:
502-
rowLabels = [''] * cols
503+
rowLabels = [''] * rows
503504
rowLabelWidth = colWidths[0]
504505
elif rowColours is None:
505506
rowColours = 'w' * rows
506507

507508
if rowLabels is not None:
508509
assert len(rowLabels) == rows
509510

510-
offset = 0
511+
# If we have column labels, need to shift
512+
# the text and colour arrays down 1 row
513+
offset = 1
511514
if colLabels is None:
512515
if colColours is not None:
513-
colLabels = [''] * rows
514-
offset = 1
516+
colLabels = [''] * cols
517+
else:
518+
offset = 0
515519
elif colColours is None:
516520
colColours = 'w' * cols
517-
offset = 1
518521

519522
if rowLabels is not None:
520523
assert len(rowLabels) == rows

lib/matplotlib/tests/test_table.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,41 @@ def test_zorder():
4141
zorder=4,
4242
)
4343
plt.yticks([])
44+
45+
46+
@image_comparison(baseline_images=['table_labels'],
47+
extensions=['png'])
48+
def test_label_colours():
49+
dim = 3
50+
51+
c = np.linspace(0, 1, dim)
52+
colours = plt.cm.RdYlGn(c)
53+
cellText = [['1'] * dim] * dim
54+
55+
fig = plt.figure()
56+
57+
ax1 = fig.add_subplot(4, 1, 1)
58+
ax1.axis('off')
59+
ax1.table(cellText=cellText,
60+
rowColours=colours,
61+
loc='best')
62+
63+
ax2 = fig.add_subplot(4, 1, 2)
64+
ax2.axis('off')
65+
ax2.table(cellText=cellText,
66+
rowColours=colours,
67+
rowLabels=['Header'] * dim,
68+
loc='best')
69+
70+
ax3 = fig.add_subplot(4, 1, 3)
71+
ax3.axis('off')
72+
ax3.table(cellText=cellText,
73+
colColours=colours,
74+
loc='best')
75+
76+
ax4 = fig.add_subplot(4, 1, 4)
77+
ax4.axis('off')
78+
ax4.table(cellText=cellText,
79+
colColours=colours,
80+
colLabels=['Header'] * dim,
81+
loc='best')

0 commit comments

Comments
 (0)