def test_revert(): with patch('matplotlib.rcParams.update') as mock_update, \ patch('matplotlib.pyplot.switch_backend') as mock_switch: lp.latexify() lp.revert() mock_update.assert_called_with(dict(plt.rcParams)) mock_switch.assert_called_with(plt.get_backend())
def test_custom_backend(self): with patch('matplotlib.rcParams.update') as mock_update, \ patch('matplotlib.pyplot.switch_backend') as mock_switch: lp.latexify(new_backend='QtAgg') mock_update.assert_called_once_with(lp.PARAMS) mock_switch.assert_called_once_with('QtAgg')
def test_defaults(self): with patch('matplotlib.rcParams.update') as mock_update, \ patch('matplotlib.pyplot.switch_backend') as mock_switch: lp.latexify() mock_update.assert_called_once_with(lp.PARAMS) mock_switch.assert_called_once_with('pgf')
def test_custom_params(self): with patch('matplotlib.rcParams.update') as mock_update, \ patch('matplotlib.pyplot.switch_backend') as mock_switch: params = {'param_a': 1, 'param_b': 2} lp.latexify(params) mock_update.assert_called_once_with(params) mock_switch.assert_called_once_with('pgf')
def test_raises_error_on_bad_backend(self): with patch('matplotlib.rcParams.update') as mock_update: with pytest.raises(ValueError): lp.latexify(new_backend='foo') mock_update.assert_called_once_with(lp.PARAMS)
def generate_figures(suffix, figure=lp.figure, plot_types=PLOT_TYPES): for plot_name, plot_function in plot_types.items(): with figure(plot_name + suffix): plot_function() if __name__ == '__main__': # If you are repeating arguments, you might want to register a partial. figure = partial(lp.figure, directory=DIRECTORY) # You can generate figures without calling lp.latexify(). generate_figures('_no_latex', figure) # latexify chooses values that go well with publications. lp.latexify() generate_figures('_with_latex', figure) # You can use the partial function just as you would the original. with figure('sincos_defaults'): plot_sin_and_cos() # You can change the size. 0.45\textwidth is useful when using two columns. with figure('sincos_small', size=lp.figure_size(0.45)): plot_sin_and_cos() # A ratio of 1 is great for squares. with figure('sincos_square', size=lp.figure_size(ratio=1)): plot_sin_and_cos() # And we can have a high figure if, for instance, stacking subplots.