Skip to content
10 changes: 9 additions & 1 deletion lib/matplotlib/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,15 @@ def _do_cell_alignment(self):

def auto_set_column_width(self, col):

self._autoColumns.append(col)
# check for col possibility on iteration
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be worded in another way. Suggestion:

If col is iterable, concatenate to self._autoColumns. Otherwise, append it to self._autoColumns.

try:
iter(col)
except (TypeError, AttributeError):
self._autoColumns.append(col)
else:
for cell in col:
self._autoColumns.append(cell)

self.stale = True

def _auto_set_column_width(self, col, renderer):
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions lib/matplotlib/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,59 @@ def test_customcell():
cell = CustomCell((0, 0), visible_edges=t, width=1, height=1)
code = tuple(s for _, s in cell.get_path().iter_segments())
assert_equal(c, code)


@image_comparison(baseline_images=['table_auto_column'],
extensions=['png'])
def test_auto_column():
fig = plt.figure()

#iteratble list input
ax1 = fig.add_subplot(4, 1, 1)
ax1.axis('off')
tb1 = ax1.table(cellText=[['Fit Text', 2],
['very long long text, Longer text than default', 1]],
rowLabels=["A", "B"],
colLabels=["Col1", "Col2"],
loc="center")
tb1.auto_set_font_size(False)
tb1.set_fontsize(12)
tb1.auto_set_column_width([-1, 0, 1])

#iteratble tuple input
ax2 = fig.add_subplot(4, 1, 2)
ax2.axis('off')
tb2 = ax2.table(cellText=[['Fit Text', 2],
['very long long text, Longer text than default', 1]],
rowLabels=["A", "B"],
colLabels=["Col1", "Col2"],
loc="center")
tb2.auto_set_font_size(False)
tb2.set_fontsize(12)
tb2.auto_set_column_width((-1, 0, 1))

#3 single inputs
ax3 = fig.add_subplot(4, 1, 3)
ax3.axis('off')
tb3 = ax3.table(cellText=[['Fit Text', 2],
['very long long text, Longer text than default', 1]],
rowLabels=["A", "B"],
colLabels=["Col1", "Col2"],
loc="center")
tb3.auto_set_font_size(False)
tb3.set_fontsize(12)
tb3.auto_set_column_width(-1)
tb3.auto_set_column_width(0)
tb3.auto_set_column_width(1)

#4 non integer interable input
ax4 = fig.add_subplot(4, 1, 4)
ax4.axis('off')
tb4 = ax4.table(cellText=[['Fit Text', 2],
['very long long text, Longer text than default', 1]],
rowLabels=["A", "B"],
colLabels=["Col1", "Col2"],
loc="center")
tb4.auto_set_font_size(False)
tb4.set_fontsize(12)
tb4.auto_set_column_width("-101")