Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fcb0a41

Browse files
authored
Merge pull request #17506 from story645/fix-dicts
FIX: dict unpacking for `.plot`
2 parents 626d54f + 58a59ed commit fcb0a41

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ def __call__(self, *args, data=None, **kwargs):
217217
if not args:
218218
return
219219

220-
if data is not None: # Process the 'data' kwarg.
220+
if data is None: # Process dict views
221+
args = [cbook.sanitize_sequence(a) for a in args]
222+
else: # Process the 'data' kwarg.
221223
replaced = [mpl._replacer(data, arg) for arg in args]
222224
if len(args) == 1:
223225
label_namer_idx = 0
@@ -262,6 +264,7 @@ def __call__(self, *args, data=None, **kwargs):
262264

263265
# Repeatedly grab (x, y) or (x, y, format) from the front of args and
264266
# massage them into arguments to plot() or fill().
267+
265268
while args:
266269
this, args = args[:2], args[2:]
267270
if args and isinstance(args[0], str):

lib/matplotlib/tests/test_preprocess_data.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import pytest
55

66
from matplotlib import _preprocess_data
7-
7+
from matplotlib.axes import Axes
8+
from matplotlib.testing.decorators import check_figures_equal
89

910
# Notes on testing the plotting functions itself
1011
# * the individual decorated plotting functions are tested in 'test_axes.py'
@@ -73,6 +74,14 @@ def test_function_call_without_data(func):
7374
"x: ['x'], y: ['y'], ls: x, w: xyz, label: text")
7475

7576

77+
@pytest.mark.parametrize('func', all_funcs, ids=all_func_ids)
78+
def test_function_call_with_dict_input(func):
79+
"""Tests with dict input, unpacking via preprocess_pipeline"""
80+
data = {'a': 1, 'b': 2}
81+
assert(func(None, data.keys(), data.values()) ==
82+
"x: ['a', 'b'], y: [1, 2], ls: x, w: xyz, label: None")
83+
84+
7685
@pytest.mark.parametrize('func', all_funcs, ids=all_func_ids)
7786
def test_function_call_with_dict_data(func):
7887
"""Test with dict data -> label comes from the value of 'x' parameter."""
@@ -215,3 +224,29 @@ def funcy(ax, x, y, z, t=None):
215224
assert not re.search(r"every other argument", funcy.__doc__)
216225
assert not re.search(r"the following arguments .*: \*x\*, \*t\*\.",
217226
funcy.__doc__)
227+
228+
229+
class TestPlotTypes:
230+
231+
plotters = [Axes.scatter, Axes.bar, Axes.plot]
232+
233+
@pytest.mark.parametrize('plotter', plotters)
234+
@check_figures_equal(extensions=['png'])
235+
def test_dict_unpack(self, plotter, fig_test, fig_ref):
236+
x = [1, 2, 3]
237+
y = [4, 5, 6]
238+
ddict = dict(zip(x, y))
239+
240+
plotter(fig_test.subplots(),
241+
ddict.keys(), ddict.values())
242+
plotter(fig_ref.subplots(), x, y)
243+
244+
@pytest.mark.parametrize('plotter', plotters)
245+
@check_figures_equal(extensions=['png'])
246+
def test_data_kwarg(self, plotter, fig_test, fig_ref):
247+
x = [1, 2, 3]
248+
y = [4, 5, 6]
249+
250+
plotter(fig_test.subplots(), 'xval', 'yval',
251+
data={'xval': x, 'yval': y})
252+
plotter(fig_ref.subplots(), x, y)

0 commit comments

Comments
 (0)