diff --git a/lib/matplotlib/table.py b/lib/matplotlib/table.py index 2d8adfbd5db3..0556511d5d9e 100644 --- a/lib/matplotlib/table.py +++ b/lib/matplotlib/table.py @@ -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 + 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): diff --git a/lib/matplotlib/tests/baseline_images/test_table/table_auto_column.png b/lib/matplotlib/tests/baseline_images/test_table/table_auto_column.png new file mode 100644 index 000000000000..9e0472b3c011 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_table/table_auto_column.png differ diff --git a/lib/matplotlib/tests/test_table.py b/lib/matplotlib/tests/test_table.py index ef768d89e935..9919fdc43949 100644 --- a/lib/matplotlib/tests/test_table.py +++ b/lib/matplotlib/tests/test_table.py @@ -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")