diff --git a/spyder/plugins/variableexplorer/widgets/namespacebrowser.py b/spyder/plugins/variableexplorer/widgets/namespacebrowser.py index 0e2d0695bf6..2314e98638d 100644 --- a/spyder/plugins/variableexplorer/widgets/namespacebrowser.py +++ b/spyder/plugins/variableexplorer/widgets/namespacebrowser.py @@ -479,6 +479,13 @@ def plot_in_plots_plugin(self, data, funcname): import spyder.pyplot as plt from IPython.core.pylabtools import print_figure + try: + from matplotlib import rc_context + except ImportError: + # Ignore fontsize and bottom options if guiqwt is used + # as plotting library + from contextlib import nullcontext as rc_context + if self.get_conf('pylab/inline/figure_format', section='ipython_console') == 1: figure_format = 'svg' @@ -497,11 +504,23 @@ def plot_in_plots_plugin(self, data, funcname): bbox_inches = 'tight' else: bbox_inches = None + matplotlib_rc = { + 'font.size': self.get_conf('pylab/inline/fontsize', + section='ipython_console'), + 'figure.subplot.bottom': self.get_conf('pylab/inline/bottom', + section='ipython_console') + } + + with rc_context(matplotlib_rc): + fig, ax = plt.subplots(figsize=(width, height)) + getattr(ax, funcname)(data) + image = print_figure( + fig, + fmt=figure_format, + bbox_inches=bbox_inches, + dpi=resolution + ) - fig, ax = plt.subplots(figsize=(width, height)) - getattr(ax, funcname)(data) - image = print_figure(fig, fmt=figure_format, bbox_inches=bbox_inches, - dpi=resolution) if figure_format == 'svg': image = image.encode() self.sig_show_figure_requested.emit(image, mime_type, self.shellwidget) diff --git a/spyder/plugins/variableexplorer/widgets/tests/test_namespacebrowser.py b/spyder/plugins/variableexplorer/widgets/tests/test_namespacebrowser.py index 71b33421eb4..ab2cf611700 100644 --- a/spyder/plugins/variableexplorer/widgets/tests/test_namespacebrowser.py +++ b/spyder/plugins/variableexplorer/widgets/tests/test_namespacebrowser.py @@ -231,6 +231,35 @@ def test_namespacebrowser_plot_with_mute_inline_plotting_true( assert blocker.args == expected_args +def test_namespacebrowser_plot_options(namespacebrowser): + """ + Test that font.size and figure.subplot.bottom in matplotlib.rcParams are + set to the values from the Spyder preferences when plotting. + """ + def check_rc(*args): + from matplotlib import rcParams + assert rcParams['font.size'] == 20.5 + assert rcParams['figure.subplot.bottom'] == 0.314 + + namespacebrowser.set_conf('mute_inline_plotting', True, section='plots') + namespacebrowser.plots_plugin_enabled = True + namespacebrowser.set_conf( + 'pylab/inline/fontsize', 20.5, section='ipython_console') + namespacebrowser.set_conf( + 'pylab/inline/bottom', 0.314, section='ipython_console') + + mock_figure = Mock() + mock_axis = Mock() + mock_png = b'fake png' + + with patch('spyder.pyplot.subplots', + return_value=(mock_figure, mock_axis)), \ + patch('IPython.core.pylabtools.print_figure', + return_value=mock_png), \ + patch.object(mock_axis, 'plot', check_rc): + namespacebrowser.plot([4, 2], 'plot') + + def test_namespacebrowser_plot_with_mute_inline_plotting_false(namespacebrowser): """ Test that plotting a list from the namespace browser shows a plot if