Skip to content

Commit

Permalink
Pass new options when plotting from Variable Explorer editors
Browse files Browse the repository at this point in the history
  • Loading branch information
jitseniesen committed Nov 30, 2023
1 parent 1931bcf commit 0464320
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
27 changes: 23 additions & 4 deletions spyder/plugins/variableexplorer/widgets/namespacebrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0464320

Please sign in to comment.