Skip to content

Commit 37fe165

Browse files
author
Marc Garcia
committed
Not passing default matplotlib parameters to backends (all known kwargs were being passed, now only the ones provided by the user)
1 parent 263ee7a commit 37fe165

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

pandas/plotting/_core.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ def __init__(self, data):
511511
self._parent = data
512512

513513
@staticmethod
514-
def _get_call_args(data, args, kwargs):
514+
def _get_call_args(backend_name, data, args, kwargs):
515515
"""
516516
This function makes calls to this accessor `__call__` method compatible
517517
with the previous `SeriesPlotMethods.__call__` and
@@ -542,9 +542,9 @@ def _get_call_args(data, args, kwargs):
542542
('yerr', None), ('xerr', None), ('secondary_y', False),
543543
('sort_columns', False)]
544544
else:
545-
return TypeError(('Called plot accessor for type {}, expected '
546-
'Series or DataFrame').format(
547-
type(data).__name__))
545+
raise TypeError(('Called plot accessor for type {}, expected '
546+
'Series or DataFrame').format(
547+
type(data).__name__))
548548

549549
if args and isinstance(data, ABCSeries):
550550
msg = ('`Series.plot()` should not be called with positional '
@@ -559,21 +559,26 @@ def _get_call_args(data, args, kwargs):
559559
FutureWarning, stacklevel=3)
560560

561561
pos_args = {name: value for value, (name, _) in zip(args, arg_def)}
562-
kwargs = dict(arg_def, **pos_args, **kwargs)
562+
if backend_name == 'pandas.plotting._matplotlib':
563+
kwargs = dict(arg_def, **pos_args, **kwargs)
564+
else:
565+
kwargs = dict(pos_args, **kwargs)
563566

564567
x = kwargs.pop('x', None)
565568
y = kwargs.pop('y', None)
566569
kind = kwargs.pop('kind', 'line')
567570
return x, y, kind, kwargs
568571

569572
def __call__(self, *args, **kwargs):
570-
x, y, kind, kwargs = self._get_call_args(self._parent, args, kwargs)
573+
plot_backend = _get_plot_backend()
574+
575+
x, y, kind, kwargs = self._get_call_args(plot_backend.__name__,
576+
self._parent, args, kwargs)
571577

572578
kind = self._kind_aliases.get(kind, kind)
573579
if kind not in self._all_kinds:
574580
raise ValueError('{} is not a valid plot kind'.format(kind))
575581

576-
plot_backend = _get_plot_backend()
577582
# The original data structured can be transformed before passed to the
578583
# backend. For example, for DataFrame is common to set the index as the
579584
# `x` parameter, and return a Series with the parameter `y` as values.

pandas/tests/plotting/test_misc.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,41 @@ def test_series_plot_with_positional_arguments_warns():
3131
Series([1, 2, 3]).plot('line', None)
3232

3333

34+
def test_get_accessor_args():
35+
func = plotting._core.PlotAccessor._get_call_args
36+
37+
msg = 'Called plot accessor for type list, expected Series or DataFrame'
38+
with pytest.raises(TypeError, match=msg):
39+
func(backend_name='', data=[], args=[], kwargs={})
40+
41+
with tm.assert_produces_warning(FutureWarning,
42+
check_stacklevel=False):
43+
func(backend_name='', data=Series(), args=[''], kwargs={})
44+
45+
x, y, kind, kwargs = func(backend_name='', data=Series(),
46+
args=['line', None], kwargs={})
47+
assert x is None
48+
assert y is None
49+
assert kind == 'line'
50+
assert kwargs == {'ax': None}
51+
52+
x, y, kind, kwargs = func(backend_name='', data=DataFrame(),
53+
args=['x'], kwargs={'y': 'y',
54+
'kind': 'bar',
55+
'grid': False})
56+
assert x == 'x'
57+
assert y == 'y'
58+
assert kind == 'bar'
59+
assert kwargs == {'grid': False}
60+
61+
x, y, kind, kwargs = func(backend_name='pandas.plotting._matplotlib',
62+
data=Series(), args=[], kwargs={})
63+
assert x is None
64+
assert y is None
65+
assert kind == 'line'
66+
assert len(kwargs) == 22
67+
68+
3469
@td.skip_if_no_mpl
3570
class TestSeriesPlots(TestPlotBase):
3671

0 commit comments

Comments
 (0)