def test_set_figure_size_long_labels(self): """Test setting a figure size that has really long labels.""" saved_stdout = sys.stdout try: out = StringIO() sys.stdout = out fig, ax = _create_plot() _set_axes_options( ax, 'foo', 'x_foo', 'y_foo', x_tick_labels=[ 'foofoofooooooooooooooooooooooooo' 'ooooooooooooooooooooooooooooooooooooooooooooooo' 'ooooooooooooooooooooo', 'barbarbar' ], x_tick_labels_orientation='vertical') _set_figure_size(fig, 3, 3) self.assertFloatEqual(fig.get_size_inches(), (3, 3)) output = out.getvalue().strip() self.assertEqual( output, "Warning: could not automatically resize plot to make room for " "axes labels and plot title. This can happen if the labels or " "title are extremely long and the plot size is too small. Your " "plot may have its labels and/or title cut-off. To fix this, " "try increasing the plot's size (in inches) and try again.") finally: sys.stdout = saved_stdout
def test_set_figure_size(self): """Test setting a valid figure size.""" fig, ax = _create_plot() _set_axes_options(ax, 'foo', 'x_foo', 'y_foo', x_tick_labels=['foofoofoo', 'barbarbar'], x_tick_labels_orientation='vertical') _set_figure_size(fig, 3, 4) self.assertFloatEqual(fig.get_size_inches(), (3, 4))
def test_set_figure_size_invalid(self): """Test setting a figure size using invalid dimensions.""" fig, ax = _create_plot() _set_axes_options(ax, 'foo', 'x_foo', 'y_foo', x_tick_labels=['foofoofoo', 'barbarbar'], x_tick_labels_orientation='vertical') orig_fig_size = fig.get_size_inches() _set_figure_size(fig, -1, 0) self.assertFloatEqual(fig.get_size_inches(), orig_fig_size)
def test_set_axes_options_ylim(self): """_set_axes_options() should set the y-axis limits.""" fig, ax = _create_plot() _set_axes_options(ax, "Plot Title", "x-axis label", "y-axis label", x_tick_labels=["T0", "T1", "T2"], y_min=0, y_max=1) self.assertEqual(ax.get_title(), "Plot Title") self.assertEqual(ax.get_ylabel(), "y-axis label") self.assertEqual(ax.get_xticklabels()[0].get_text(), "T0") self.assertEqual(ax.get_xticklabels()[1].get_text(), "T1") self.assertFloatEqual(ax.get_ylim(), [0, 1])
def test_set_axes_options(self): """_set_axes_options() should set the labels on the axes and not raise any exceptions.""" fig, ax = _create_plot() _set_axes_options(ax, "Plot Title", "x-axis label", "y-axis label", x_tick_labels=["T0", "T1"]) self.assertEqual(ax.get_title(), "Plot Title") self.assertEqual(ax.get_ylabel(), "y-axis label") self.assertEqual(ax.get_xticklabels()[0].get_text(), "T0") self.assertEqual(ax.get_xticklabels()[1].get_text(), "T1")
def test_set_figure_size_long_labels(self): """Test setting a figure size that has really long labels.""" saved_stdout = sys.stdout try: out = StringIO() sys.stdout = out fig, ax = _create_plot() _set_axes_options(ax, 'foo', 'x_foo', 'y_foo', x_tick_labels=['foofoofooooooooooooooooooooooooo' 'ooooooooooooooooooooooooooooooooooooooooooooooo' 'ooooooooooooooooooooo', 'barbarbar'], x_tick_labels_orientation='vertical') _set_figure_size(fig, 3, 3) self.assertFloatEqual(fig.get_size_inches(), (3, 3)) output = out.getvalue().strip() self.assertEqual(output, "Warning: could not automatically resize plot to make room for " "axes labels and plot title. This can happen if the labels or " "title are extremely long and the plot size is too small. Your " "plot may have its labels and/or title cut-off. To fix this, " "try increasing the plot's size (in inches) and try again.") finally: sys.stdout = saved_stdout