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

Skip to content

Commit 963e51d

Browse files
committed
Merge pull request #6047 from ryanbelt/master
FIX: table auto_column_width fix #5479
2 parents c62f88f + 8a3349f commit 963e51d

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

lib/matplotlib/table.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,15 @@ def _do_cell_alignment(self):
410410

411411
def auto_set_column_width(self, col):
412412

413-
self._autoColumns.append(col)
413+
# check for col possibility on iteration
414+
try:
415+
iter(col)
416+
except (TypeError, AttributeError):
417+
self._autoColumns.append(col)
418+
else:
419+
for cell in col:
420+
self._autoColumns.append(cell)
421+
414422
self.stale = True
415423

416424
def _auto_set_column_width(self, col, renderer):

lib/matplotlib/tests/test_table.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,59 @@ def test_customcell():
121121
cell = CustomCell((0, 0), visible_edges=t, width=1, height=1)
122122
code = tuple(s for _, s in cell.get_path().iter_segments())
123123
assert_equal(c, code)
124+
125+
126+
@image_comparison(baseline_images=['table_auto_column'],
127+
extensions=['png'])
128+
def test_auto_column():
129+
fig = plt.figure()
130+
131+
#iteratble list input
132+
ax1 = fig.add_subplot(4, 1, 1)
133+
ax1.axis('off')
134+
tb1 = ax1.table(cellText=[['Fit Text', 2],
135+
['very long long text, Longer text than default', 1]],
136+
rowLabels=["A", "B"],
137+
colLabels=["Col1", "Col2"],
138+
loc="center")
139+
tb1.auto_set_font_size(False)
140+
tb1.set_fontsize(12)
141+
tb1.auto_set_column_width([-1, 0, 1])
142+
143+
#iteratble tuple input
144+
ax2 = fig.add_subplot(4, 1, 2)
145+
ax2.axis('off')
146+
tb2 = ax2.table(cellText=[['Fit Text', 2],
147+
['very long long text, Longer text than default', 1]],
148+
rowLabels=["A", "B"],
149+
colLabels=["Col1", "Col2"],
150+
loc="center")
151+
tb2.auto_set_font_size(False)
152+
tb2.set_fontsize(12)
153+
tb2.auto_set_column_width((-1, 0, 1))
154+
155+
#3 single inputs
156+
ax3 = fig.add_subplot(4, 1, 3)
157+
ax3.axis('off')
158+
tb3 = ax3.table(cellText=[['Fit Text', 2],
159+
['very long long text, Longer text than default', 1]],
160+
rowLabels=["A", "B"],
161+
colLabels=["Col1", "Col2"],
162+
loc="center")
163+
tb3.auto_set_font_size(False)
164+
tb3.set_fontsize(12)
165+
tb3.auto_set_column_width(-1)
166+
tb3.auto_set_column_width(0)
167+
tb3.auto_set_column_width(1)
168+
169+
#4 non integer interable input
170+
ax4 = fig.add_subplot(4, 1, 4)
171+
ax4.axis('off')
172+
tb4 = ax4.table(cellText=[['Fit Text', 2],
173+
['very long long text, Longer text than default', 1]],
174+
rowLabels=["A", "B"],
175+
colLabels=["Col1", "Col2"],
176+
loc="center")
177+
tb4.auto_set_font_size(False)
178+
tb4.set_fontsize(12)
179+
tb4.auto_set_column_width("-101")

0 commit comments

Comments
 (0)