Skip to content

Commit 3e6716a

Browse files
TomAugspurgerrs2
authored andcommitted
start
1 parent 63e8527 commit 3e6716a

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
@@ -480,3 +480,19 @@ def use_inf_as_na_cb(key):
480480
cf.register_option(
481481
'engine', 'auto', parquet_engine_doc,
482482
validator=is_one_of_factory(['auto', 'pyarrow', 'fastparquet']))
483+
484+
485+
# --------
486+
# Plotting
487+
# --------
488+
489+
plotting_engine_doc = """
490+
: str
491+
The name of a plotting engine.
492+
"""
493+
494+
495+
with cf.config_prefix("plotting"):
496+
cf.register_option(
497+
"engine", "auto", plotting_engine_doc, validator=str
498+
)

pandas/core/frame.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
import pandas.io.formats.console as console
100100
from pandas.io.formats.printing import pprint_thing
101101
import pandas.plotting._core as gfx
102+
import pandas.plotting.base as gfx_base
102103

103104
from pandas._libs import lib, algos as libalgos
104105

@@ -6128,8 +6129,8 @@ def isin(self, values):
61286129

61296130
# ----------------------------------------------------------------------
61306131
# Add plotting methods to DataFrame
6131-
plot = accessor.AccessorProperty(gfx.FramePlotMethods,
6132-
gfx.FramePlotMethods)
6132+
plot = accessor.AccessorProperty(gfx_base.Dispatcher,
6133+
gfx_base.Dispatcher)
61336134
hist = gfx.hist_frame
61346135
boxplot = gfx.boxplot_frame
61356136

pandas/core/series.py

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

8181
import pandas.plotting._core as gfx
82+
import pandas.plotting.base as gfx_base
8283

8384
__all__ = ['Series']
8485

@@ -3090,8 +3091,8 @@ def to_period(self, freq=None, copy=True):
30903091

30913092
# ----------------------------------------------------------------------
30923093
# Add plotting methods to Series
3093-
plot = accessor.AccessorProperty(gfx.SeriesPlotMethods,
3094-
gfx.SeriesPlotMethods)
3094+
plot = accessor.AccessorProperty(gfx_base.SeriesPlotMethods,
3095+
gfx_base.SeriesPlotMethods)
30953096
hist = gfx.hist_series
30963097

30973098

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)
@@ -2458,16 +2458,7 @@ def _grouped_plot_by_column(plotf, data, columns=None, by=None,
24582458
return result
24592459

24602460

2461-
class BasePlotMethods(PandasObject):
2462-
2463-
def __init__(self, data):
2464-
self._data = data
2465-
2466-
def __call__(self, *args, **kwargs):
2467-
raise NotImplementedError
2468-
2469-
2470-
class SeriesPlotMethods(BasePlotMethods):
2461+
class MPLSeriesPlotMethods(base.SeriesPlotMethods):
24712462
"""Series plotting accessor and method
24722463
24732464
Examples
@@ -2480,6 +2471,9 @@ class SeriesPlotMethods(BasePlotMethods):
24802471
with the ``kind`` argument:
24812472
``s.plot(kind='line')`` is equivalent to ``s.plot.line()``
24822473
"""
2474+
@property
2475+
def engine_name(self):
2476+
return 'matplotlib'
24832477

24842478
def __call__(self, kind='line', ax=None,
24852479
figsize=None, use_index=True, title=None, grid=None,
@@ -2624,7 +2618,7 @@ def pie(self, **kwds):
26242618
return self(kind='pie', **kwds)
26252619

26262620

2627-
class FramePlotMethods(BasePlotMethods):
2621+
class MPLFramePlotMethods(base.FramePlotMethods):
26282622
"""DataFrame plotting accessor and method
26292623
26302624
Examples
@@ -2637,6 +2631,9 @@ class FramePlotMethods(BasePlotMethods):
26372631
method with the ``kind`` argument:
26382632
``df.plot(kind='line')`` is equivalent to ``df.plot.line()``
26392633
"""
2634+
@property
2635+
def engine_name(self):
2636+
return 'matplotlib'
26402637

26412638
def __call__(self, x=None, y=None, kind='line', ax=None,
26422639
subplots=False, sharex=None, sharey=False, layout=None,
@@ -2844,3 +2841,7 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None,
28442841
if gridsize is not None:
28452842
kwds['gridsize'] = gridsize
28462843
return self(kind='hexbin', x=x, y=y, C=C, **kwds)
2844+
2845+
2846+
base.register_engine("matplotlib", 'series', MPLSeriesPlotMethods)
2847+
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)