diff --git a/CHANGELOG.md b/CHANGELOG.md index f7e606d0c2..e4d4c23860 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/doc/python/pandas-backend.md b/doc/python/pandas-backend.md index 41be97978a..3e117b8421 100644 --- a/doc/python/pandas-backend.md +++ b/doc/python/pandas-backend.md @@ -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 @@ -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). \ No newline at end of file +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 --git a/packages/python/plotly/plotly/__init__.py b/packages/python/plotly/plotly/__init__.py index c6baf88f45..838ed2377b 100644 --- a/packages/python/plotly/plotly/__init__.py +++ b/packages/python/plotly/plotly/__init__.py @@ -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"]} @@ -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 ) diff --git a/packages/python/plotly/plotly/tests/test_core/test_px/test_pandas_backend.py b/packages/python/plotly/plotly/tests/test_core/test_px/test_pandas_backend.py index 33df17d904..894baf7a52 100644 --- a/packages/python/plotly/plotly/tests/test_core/test_px/test_pandas_backend.py +++ b/packages/python/plotly/plotly/tests/test_core/test_px/test_pandas_backend.py @@ -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):