Skip to content

Commit 0b442b1

Browse files
cleaner pandas backend
1 parent 1882f51 commit 0b442b1

File tree

3 files changed

+98
-81
lines changed

3 files changed

+98
-81
lines changed

packages/python/plotly/plotly/__init__.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,75 @@
7575
],
7676
[".version.__version__"],
7777
)
78+
79+
80+
def plot(data_frame, kind, **kwargs):
81+
"""
82+
Pandas plotting backend function, not meant to be called directly.
83+
To activate, set pandas.options.plotting.backend="plotly.express.pandas_backend"
84+
See https://github.com/pandas-dev/pandas/blob/master/pandas/plotting/__init__.py
85+
"""
86+
from .express import scatter, line, area, bar, box, histogram
87+
88+
if kind == "scatter":
89+
new_kwargs = {k: kwargs[k] for k in kwargs if k not in ["s", "c"]}
90+
return scatter(data_frame, **new_kwargs)
91+
if kind == "line":
92+
return line(data_frame, **kwargs)
93+
if kind == "area":
94+
return area(data_frame, **kwargs)
95+
if kind == "bar":
96+
return bar(data_frame, **kwargs)
97+
if kind == "barh":
98+
return bar(data_frame, orientation="h", **kwargs)
99+
if kind == "box":
100+
new_kwargs = {k: kwargs[k] for k in kwargs if k not in ["by"]}
101+
return box(data_frame, **new_kwargs)
102+
if kind in "hist":
103+
new_kwargs = {k: kwargs[k] for k in kwargs if k not in ["by", "bins"]}
104+
return histogram(data_frame, **new_kwargs)
105+
raise NotImplementedError(
106+
"The plotly.express backend doesn't yet support kind='%s'" % kind
107+
)
108+
109+
110+
def boxplot_frame(data_frame, **kwargs):
111+
"""
112+
Pandas plotting backend function, not meant to be called directly.
113+
To activate, set pandas.options.plotting.backend="plotly.express.pandas_backend"
114+
See https://github.com/pandas-dev/pandas/blob/master/pandas/plotting/__init__.py
115+
"""
116+
from .express import box
117+
118+
skip = ["by", "column", "ax", "fontsize", "rot", "grid", "figsize", "layout"]
119+
skip += ["return_type"]
120+
new_kwargs = {k: kwargs[k] for k in kwargs if k not in skip}
121+
return box(data_frame, **new_kwargs)
122+
123+
124+
def hist_frame(data_frame, **kwargs):
125+
"""
126+
Pandas plotting backend function, not meant to be called directly.
127+
To activate, set pandas.options.plotting.backend="plotly.express.pandas_backend"
128+
See https://github.com/pandas-dev/pandas/blob/master/pandas/plotting/__init__.py
129+
"""
130+
from .express import histogram
131+
132+
skip = ["column", "by", "grid", "xlabelsize", "xrot", "ylabelsize", "yrot"]
133+
skip += ["ax", "sharex", "sharey", "figsize", "layout", "bins"]
134+
new_kwargs = {k: kwargs[k] for k in kwargs if k not in skip}
135+
return histogram(data_frame, **new_kwargs)
136+
137+
138+
def hist_series(data_frame, **kwargs):
139+
"""
140+
Pandas plotting backend function, not meant to be called directly.
141+
To activate, set pandas.options.plotting.backend="plotly.express.pandas_backend"
142+
See https://github.com/pandas-dev/pandas/blob/master/pandas/plotting/__init__.py
143+
"""
144+
from .express import histogram
145+
146+
skip = ["by", "grid", "xlabelsize", "xrot", "ylabelsize", "yrot", "ax"]
147+
skip += ["figsize", "bins"]
148+
new_kwargs = {k: kwargs[k] for k in kwargs if k not in skip}
149+
return histogram(data_frame, **new_kwargs)

packages/python/plotly/plotly/express/pandas_backend.py

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import plotly.express as px
2+
import numpy as np
3+
import pandas as pd
4+
import pytest
5+
6+
7+
@pytest.mark.parametrize(
8+
"pandas_fn,px_fn",
9+
[
10+
(lambda df: df.plot(), px.line),
11+
(lambda df: df.plot.scatter("A", "B"), lambda df: px.scatter(df, "A", "B"),),
12+
(lambda df: df.plot.line(), px.line),
13+
(lambda df: df.plot.area(), px.area),
14+
(lambda df: df.plot.bar(), px.bar),
15+
(lambda df: df.plot.barh(), lambda df: px.bar(df, orientation="h")),
16+
(lambda df: df.plot.box(), px.box),
17+
(lambda df: df.plot.hist(), px.histogram),
18+
(lambda df: df.boxplot(), px.box),
19+
(lambda df: df.hist(), px.histogram),
20+
(lambda df: df["A"].hist(), lambda df: px.histogram(df["A"])),
21+
],
22+
)
23+
def test_pandas_equiv(pandas_fn, px_fn):
24+
pd.options.plotting.backend = "plotly"
25+
df = pd.DataFrame(np.random.randn(100, 4), columns=list("ABCD")).cumsum()
26+
assert pandas_fn(df).to_json() == px_fn(df).to_json()

0 commit comments

Comments
 (0)