Skip to content

Commit 5f29a57

Browse files
committed
start
1 parent 48d0460 commit 5f29a57

File tree

5 files changed

+131
-16
lines changed

5 files changed

+131
-16
lines changed

pandas/core/config_init.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,19 @@ def use_inf_as_na_cb(key):
479479
cf.register_option(
480480
'engine', 'auto', parquet_engine_doc,
481481
validator=is_one_of_factory(['auto', 'pyarrow', 'fastparquet']))
482+
483+
484+
# --------
485+
# Plotting
486+
# --------
487+
488+
plotting_engine_doc = """
489+
: str
490+
The name of a plotting engine.
491+
"""
492+
493+
494+
with cf.config_prefix("plotting"):
495+
cf.register_option(
496+
"engine", "auto", plotting_engine_doc, validator=str
497+
)

pandas/core/frame.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
import pandas.io.formats.console as console
9898
from pandas.io.formats.printing import pprint_thing
9999
import pandas.plotting._core as gfx
100+
import pandas.plotting.base as gfx_base
100101

101102
from pandas._libs import lib, algos as libalgos
102103

@@ -5905,8 +5906,8 @@ def isin(self, values):
59055906

59065907
# ----------------------------------------------------------------------
59075908
# Add plotting methods to DataFrame
5908-
plot = accessor.AccessorProperty(gfx.FramePlotMethods,
5909-
gfx.FramePlotMethods)
5909+
plot = accessor.AccessorProperty(gfx_base.Dispatcher,
5910+
gfx_base.Dispatcher)
59105911
hist = gfx.hist_frame
59115912
boxplot = gfx.boxplot_frame
59125913

pandas/core/series.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
from pandas.core.config import get_option
7878

7979
import pandas.plotting._core as gfx
80+
import pandas.plotting.base as gfx_base
8081

8182
__all__ = ['Series']
8283

@@ -2919,8 +2920,8 @@ def to_period(self, freq=None, copy=True):
29192920

29202921
# ----------------------------------------------------------------------
29212922
# Add plotting methods to Series
2922-
plot = accessor.AccessorProperty(gfx.SeriesPlotMethods,
2923-
gfx.SeriesPlotMethods)
2923+
plot = accessor.AccessorProperty(gfx_base.SeriesPlotMethods,
2924+
gfx_base.SeriesPlotMethods)
29242925
hist = gfx.hist_series
29252926

29262927

pandas/plotting/_core.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import numpy as np
1111

1212
from pandas.util._decorators import cache_readonly
13-
from pandas.core.base import PandasObject
1413
from pandas.core.dtypes.missing import isna, notna, remove_na_arraylike
1514
from pandas.core.dtypes.common import (
1615
is_list_like,
@@ -30,6 +29,7 @@
3029
from pandas.io.formats.printing import pprint_thing
3130
from pandas.util._decorators import Appender
3231

32+
from pandas.plotting import base
3333
from pandas.plotting._compat import (_mpl_ge_1_3_1,
3434
_mpl_ge_1_5_0,
3535
_mpl_ge_2_0_0)
@@ -2460,16 +2460,7 @@ def _grouped_plot_by_column(plotf, data, columns=None, by=None,
24602460
return result
24612461

24622462

2463-
class BasePlotMethods(PandasObject):
2464-
2465-
def __init__(self, data):
2466-
self._data = data
2467-
2468-
def __call__(self, *args, **kwargs):
2469-
raise NotImplementedError
2470-
2471-
2472-
class SeriesPlotMethods(BasePlotMethods):
2463+
class MPLSeriesPlotMethods(base.SeriesPlotMethods):
24732464
"""Series plotting accessor and method
24742465
24752466
Examples
@@ -2482,6 +2473,9 @@ class SeriesPlotMethods(BasePlotMethods):
24822473
with the ``kind`` argument:
24832474
``s.plot(kind='line')`` is equivalent to ``s.plot.line()``
24842475
"""
2476+
@property
2477+
def engine_name(self):
2478+
return 'matplotlib'
24852479

24862480
def __call__(self, kind='line', ax=None,
24872481
figsize=None, use_index=True, title=None, grid=None,
@@ -2642,7 +2636,7 @@ def pie(self, **kwds):
26422636
return self(kind='pie', **kwds)
26432637

26442638

2645-
class FramePlotMethods(BasePlotMethods):
2639+
class MPLFramePlotMethods(base.FramePlotMethods):
26462640
"""DataFrame plotting accessor and method
26472641
26482642
Examples
@@ -2655,6 +2649,9 @@ class FramePlotMethods(BasePlotMethods):
26552649
method with the ``kind`` argument:
26562650
``df.plot(kind='line')`` is equivalent to ``df.plot.line()``
26572651
"""
2652+
@property
2653+
def engine_name(self):
2654+
return 'matplotlib'
26582655

26592656
def __call__(self, x=None, y=None, kind='line', ax=None,
26602657
subplots=False, sharex=None, sharey=False, layout=None,
@@ -2882,3 +2879,7 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None,
28822879
if gridsize is not None:
28832880
kwds['gridsize'] = gridsize
28842881
return self(kind='hexbin', x=x, y=y, C=C, **kwds)
2882+
2883+
2884+
base.register_engine("matplotlib", 'series', MPLSeriesPlotMethods)
2885+
base.register_engine("matplotlib", 'frame', MPLFramePlotMethods)

pandas/plotting/base.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""
2+
Base module for plotting engines to override and register
3+
with pandas.
4+
"""
5+
from pandas.core.base import PandasObject
6+
7+
engines = {}
8+
9+
10+
def register_engine(name, kind, engine):
11+
# XXX: get rid of the kind parameter
12+
global engines
13+
engines[(name, kind)] = engine
14+
15+
16+
def deregister_engine(name, kind):
17+
# XXX: get rid of the kind parameter
18+
global engines
19+
engines.pop((name, kind))
20+
21+
22+
def get_engine(kind):
23+
# XXX: get rid of the kind parameter
24+
from pandas import get_option
25+
26+
active = get_option('plotting.engine')
27+
if active == 'auto':
28+
active = 'matplotlib'
29+
30+
return engines[(active, kind)]
31+
32+
33+
class Dispatcher(object):
34+
35+
def __init__(self, data):
36+
self._data = data
37+
38+
def __call__(self, *args, **kwargs):
39+
kind = 'frame' if self._data.ndim == 2 else 'series'
40+
engine = get_engine(kind)
41+
return engine(self._data)(*args, **kwargs)
42+
43+
def __getattribute__(self, name):
44+
if name == '_data':
45+
return object.__getattribute__(self, name)
46+
kind = 'frame' if self._data.ndim == 2 else 'series'
47+
48+
engine = get_engine(kind)(self._data)
49+
return getattr(engine, name)
50+
51+
52+
class BasePlotMethods(PandasObject):
53+
54+
def __init__(self, data):
55+
self._data = data
56+
57+
def __call__(self, *args, **kwargs):
58+
"""Make a plot"""
59+
raise NotImplementedError
60+
61+
def area(self, **kwargs):
62+
raise NotImplementedError("This backend doesn't support this method")
63+
64+
def bar(self, **kwargs):
65+
raise NotImplementedError("This backend doesn't support this method")
66+
67+
def barh(self, **kwargs):
68+
raise NotImplementedError("This backend doesn't support this method")
69+
70+
def box(self, **kwargs):
71+
raise NotImplementedError("This backend doesn't support this method")
72+
73+
def density(self, **kwargs):
74+
raise NotImplementedError("This backend doesn't support this method")
75+
76+
def hist(self, **kwargs):
77+
raise NotImplementedError("This backend doesn't support this method")
78+
79+
def line(self, **kwargs):
80+
raise NotImplementedError("This backend doesn't support this method")
81+
82+
def pie(self, **kwargs):
83+
raise NotImplementedError("This backend doesn't support this method")
84+
85+
86+
class SeriesPlotMethods(BasePlotMethods):
87+
pass
88+
89+
90+
class FramePlotMethods(BasePlotMethods):
91+
92+
def hexbin(self, x, y, **kwargs):
93+
raise NotImplementedError("This backend doesn't support this method")
94+
95+
def scatter(self, x, y, **kwargs):
96+
raise NotImplementedError("This backend doesn't support this method")

0 commit comments

Comments
 (0)