Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more Pandas backend options #2541

Merged
merged 2 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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":
emmanuelle marked this conversation as resolved.
Show resolved Hide resolved
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