Skip to content

Commit b4cb434

Browse files
Merge pull request #2541 from plotly/more_pandas
more Pandas backend options
2 parents c7282c7 + fa7a4dc commit b4cb434

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77

88
### Updated
99

10+
- Added all cartesian-2d Plotly Express functions, plus `imshow` to Pandas backend with `kind` option
1011
- `plotly.express.imshow` now uses data frame index and columns names and values to populate axis parameters by default ([#2539](https://github.com/plotly/plotly.py/pull/2539))
1112

1213

Diff for: doc/python/pandas-backend.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fig.show()
9898

9999
### Supported Methods
100100

101-
The Plotly backend supports the following `kind`s of Pandas plots: `scatter`, `line`, `area`, `bar`, `barh`, `hist` and `box`, via the call pattern `df.plot(kind='scatter')` or `df.plot.scatter()`. These delegate to the corresponding Plotly Express functions.
101+
The Plotly backend supports the following `kind`s of Pandas plots: `scatter`, `line`, `area`, `bar`, `barh`, `hist` and `box`, via the call pattern `df.plot(kind='scatter')` or `df.plot.scatter()`. These delegate to the corresponding Plotly Express functions. In addition, the following are valid options to the `kind` argument of `df.plot()`: `violin`, `strip`, `funnel`, `density_heatmap`, `density_contour` and `imshow`, even though the call pattern `df.plot.violin()` is not supported for these kinds of charts, per the Pandas API.
102102

103103
```python
104104
import pandas as pd
@@ -198,4 +198,4 @@ fig.show()
198198

199199
### What about Cufflinks?
200200

201-
There also exists an independent third-party wrapper library around Plotly called [Cufflinks](https://github.com/santosjorge/cufflinks), which provides similar functionality (with an API closer to that of Pandas' default `matplotlib` backend) by adding a `.iplot()` method to Pandas dataframes, as it was developed before Pandas supported configurable backends. Issues and questions regarding Cufflinks should be [raised in the Cufflinks repository](https://github.com/santosjorge/cufflinks/issues/new).
201+
There also exists an independent third-party wrapper library around Plotly called [Cufflinks](https://github.com/santosjorge/cufflinks), which provides similar functionality (with an API closer to that of Pandas' default `matplotlib` backend) by adding a `.iplot()` method to Pandas dataframes, as it was developed before Pandas supported configurable backends. Issues and questions regarding Cufflinks should be [raised in the Cufflinks repository](https://github.com/santosjorge/cufflinks/issues/new).

Diff for: packages/python/plotly/plotly/__init__.py

+33-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,20 @@ def plot(data_frame, kind, **kwargs):
8080
To activate, set pandas.options.plotting.backend="plotly"
8181
See https://github.com/pandas-dev/pandas/blob/master/pandas/plotting/__init__.py
8282
"""
83-
from .express import scatter, line, area, bar, box, histogram
83+
from .express import (
84+
scatter,
85+
line,
86+
area,
87+
bar,
88+
box,
89+
histogram,
90+
violin,
91+
strip,
92+
funnel,
93+
density_contour,
94+
density_heatmap,
95+
imshow,
96+
)
8497

8598
if kind == "scatter":
8699
new_kwargs = {k: kwargs[k] for k in kwargs if k not in ["s", "c"]}
@@ -96,9 +109,27 @@ def plot(data_frame, kind, **kwargs):
96109
if kind == "box":
97110
new_kwargs = {k: kwargs[k] for k in kwargs if k not in ["by"]}
98111
return box(data_frame, **new_kwargs)
99-
if kind in "hist":
112+
if kind in ["hist", "histogram"]:
100113
new_kwargs = {k: kwargs[k] for k in kwargs if k not in ["by", "bins"]}
101114
return histogram(data_frame, **new_kwargs)
115+
if kind == "violin":
116+
return violin(data_frame, **kwargs)
117+
if kind == "strip":
118+
return strip(data_frame, **kwargs)
119+
if kind == "funnel":
120+
return funnel(data_frame, **kwargs)
121+
if kind == "density_contour":
122+
return density_contour(data_frame, **kwargs)
123+
if kind == "density_heatmap":
124+
return density_heatmap(data_frame, **kwargs)
125+
if kind == "imshow":
126+
return imshow(data_frame, **kwargs)
127+
if kind == "heatmap":
128+
raise ValueError(
129+
"kind='heatmap' not supported plotting.backend='plotly'. "
130+
"Please use kind='imshow' or kind='density_heatmap'."
131+
)
132+
102133
raise NotImplementedError(
103134
"kind='%s' not yet supported for plotting.backend='plotly'" % kind
104135
)

Diff for: packages/python/plotly/plotly/tests/test_core/test_px/test_pandas_backend.py

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@
2222
(lambda df: df.boxplot(), px.box),
2323
(lambda df: df.hist(), px.histogram),
2424
(lambda df: df["A"].hist(), lambda df: px.histogram(df["A"])),
25+
(lambda df: df.plot(kind="line"), px.line),
26+
(lambda df: df.plot(kind="area"), px.area),
27+
(lambda df: df.plot(kind="bar"), px.bar),
28+
(lambda df: df.plot(kind="box"), px.box),
29+
(lambda df: df.plot(kind="hist"), px.histogram),
30+
(lambda df: df.plot(kind="histogram"), px.histogram),
31+
(lambda df: df.plot(kind="violin"), px.violin),
32+
(lambda df: df.plot(kind="strip"), px.strip),
33+
(lambda df: df.plot(kind="funnel"), px.funnel),
34+
(lambda df: df.plot(kind="density_contour"), px.density_contour),
35+
(lambda df: df.plot(kind="density_heatmap"), px.density_heatmap),
36+
(lambda df: df.plot(kind="imshow"), px.imshow),
2537
],
2638
)
2739
def test_pandas_equiv(pandas_fn, px_fn):

0 commit comments

Comments
 (0)