diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 8852f12bfdd9..8b872bf1056e 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -217,7 +217,9 @@ def __call__(self, *args, data=None, **kwargs): if not args: return - if data is not None: # Process the 'data' kwarg. + if data is None: # Process dict views + args = [cbook.sanitize_sequence(a) for a in args] + else: # Process the 'data' kwarg. replaced = [mpl._replacer(data, arg) for arg in args] if len(args) == 1: label_namer_idx = 0 @@ -262,6 +264,7 @@ def __call__(self, *args, data=None, **kwargs): # Repeatedly grab (x, y) or (x, y, format) from the front of args and # massage them into arguments to plot() or fill(). + while args: this, args = args[:2], args[2:] if args and isinstance(args[0], str): diff --git a/lib/matplotlib/tests/test_preprocess_data.py b/lib/matplotlib/tests/test_preprocess_data.py index a24184e1e43c..1f4707679508 100644 --- a/lib/matplotlib/tests/test_preprocess_data.py +++ b/lib/matplotlib/tests/test_preprocess_data.py @@ -4,7 +4,8 @@ import pytest from matplotlib import _preprocess_data - +from matplotlib.axes import Axes +from matplotlib.testing.decorators import check_figures_equal # Notes on testing the plotting functions itself # * the individual decorated plotting functions are tested in 'test_axes.py' @@ -73,6 +74,14 @@ def test_function_call_without_data(func): "x: ['x'], y: ['y'], ls: x, w: xyz, label: text") +@pytest.mark.parametrize('func', all_funcs, ids=all_func_ids) +def test_function_call_with_dict_input(func): + """Tests with dict input, unpacking via preprocess_pipeline""" + data = {'a': 1, 'b': 2} + assert(func(None, data.keys(), data.values()) == + "x: ['a', 'b'], y: [1, 2], ls: x, w: xyz, label: None") + + @pytest.mark.parametrize('func', all_funcs, ids=all_func_ids) def test_function_call_with_dict_data(func): """Test with dict data -> label comes from the value of 'x' parameter.""" @@ -215,3 +224,29 @@ def funcy(ax, x, y, z, t=None): assert not re.search(r"every other argument", funcy.__doc__) assert not re.search(r"the following arguments .*: \*x\*, \*t\*\.", funcy.__doc__) + + +class TestPlotTypes: + + plotters = [Axes.scatter, Axes.bar, Axes.plot] + + @pytest.mark.parametrize('plotter', plotters) + @check_figures_equal(extensions=['png']) + def test_dict_unpack(self, plotter, fig_test, fig_ref): + x = [1, 2, 3] + y = [4, 5, 6] + ddict = dict(zip(x, y)) + + plotter(fig_test.subplots(), + ddict.keys(), ddict.values()) + plotter(fig_ref.subplots(), x, y) + + @pytest.mark.parametrize('plotter', plotters) + @check_figures_equal(extensions=['png']) + def test_data_kwarg(self, plotter, fig_test, fig_ref): + x = [1, 2, 3] + y = [4, 5, 6] + + plotter(fig_test.subplots(), 'xval', 'yval', + data={'xval': x, 'yval': y}) + plotter(fig_ref.subplots(), x, y)