Skip to content

Commit

Permalink
Merge pull request #2541 from plotly/more_pandas
Browse files Browse the repository at this point in the history
more Pandas backend options
  • Loading branch information
nicolaskruchten authored Jul 8, 2020
2 parents c7282c7 + fa7a4dc commit b4cb434
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Updated

- Added all cartesian-2d Plotly Express functions, plus `imshow` to Pandas backend with `kind` option
- `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))


Expand Down
4 changes: 2 additions & 2 deletions doc/python/pandas-backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fig.show()

### Supported Methods

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.
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.

```python
import pandas as pd
Expand Down Expand Up @@ -198,4 +198,4 @@ fig.show()

### What about Cufflinks?

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).
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).
35 changes: 33 additions & 2 deletions packages/python/plotly/plotly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,20 @@ def plot(data_frame, kind, **kwargs):
To activate, set pandas.options.plotting.backend="plotly"
See https://github.com/pandas-dev/pandas/blob/master/pandas/plotting/__init__.py
"""
from .express import scatter, line, area, bar, box, histogram
from .express import (
scatter,
line,
area,
bar,
box,
histogram,
violin,
strip,
funnel,
density_contour,
density_heatmap,
imshow,
)

if kind == "scatter":
new_kwargs = {k: kwargs[k] for k in kwargs if k not in ["s", "c"]}
Expand All @@ -96,9 +109,27 @@ def plot(data_frame, kind, **kwargs):
if kind == "box":
new_kwargs = {k: kwargs[k] for k in kwargs if k not in ["by"]}
return box(data_frame, **new_kwargs)
if kind in "hist":
if kind in ["hist", "histogram"]:
new_kwargs = {k: kwargs[k] for k in kwargs if k not in ["by", "bins"]}
return histogram(data_frame, **new_kwargs)
if kind == "violin":
return violin(data_frame, **kwargs)
if kind == "strip":
return strip(data_frame, **kwargs)
if kind == "funnel":
return funnel(data_frame, **kwargs)
if kind == "density_contour":
return density_contour(data_frame, **kwargs)
if kind == "density_heatmap":
return density_heatmap(data_frame, **kwargs)
if kind == "imshow":
return imshow(data_frame, **kwargs)
if kind == "heatmap":
raise ValueError(
"kind='heatmap' not supported plotting.backend='plotly'. "
"Please use kind='imshow' or kind='density_heatmap'."
)

raise NotImplementedError(
"kind='%s' not yet supported for plotting.backend='plotly'" % kind
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
(lambda df: df.boxplot(), px.box),
(lambda df: df.hist(), px.histogram),
(lambda df: df["A"].hist(), lambda df: px.histogram(df["A"])),
(lambda df: df.plot(kind="line"), px.line),
(lambda df: df.plot(kind="area"), px.area),
(lambda df: df.plot(kind="bar"), px.bar),
(lambda df: df.plot(kind="box"), px.box),
(lambda df: df.plot(kind="hist"), px.histogram),
(lambda df: df.plot(kind="histogram"), px.histogram),
(lambda df: df.plot(kind="violin"), px.violin),
(lambda df: df.plot(kind="strip"), px.strip),
(lambda df: df.plot(kind="funnel"), px.funnel),
(lambda df: df.plot(kind="density_contour"), px.density_contour),
(lambda df: df.plot(kind="density_heatmap"), px.density_heatmap),
(lambda df: df.plot(kind="imshow"), px.imshow),
],
)
def test_pandas_equiv(pandas_fn, px_fn):
Expand Down

0 comments on commit b4cb434

Please sign in to comment.