Skip to content

Fix dicts unpacking for .plot #17506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
37 changes: 36 additions & 1 deletion lib/matplotlib/tests/test_preprocess_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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)