Plotly Express can be used as a Pandas .plot() backend.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.
Introduction¶The popular Pandas data analysis and manipulation tool provides plotting functions on its DataFrame
and Series
objects, which have historically produced matplotlib
plots. Since version 0.25, Pandas has provided a mechanism to use different backends, and as of version 4.8 of plotly
, you can now use a Plotly Express-powered backend for Pandas plotting. This means you can now produce interactive plots directly from a data frame, without even needing to import Plotly.
To activate this backend, you will need to have Plotly installed, and then just need to set pd.options.plotting.backend
to "plotly"
and call .plot()
to get a plotly.graph_objects.Figure
object back, just like if you had called Plotly Express directly:
In [1]:
import pandas as pd pd.options.plotting.backend = "plotly" df = pd.DataFrame(dict(a=[1,3,2], b=[3,2,1])) fig = df.plot() fig.show()
In [2]:
import pandas as pd pd.options.plotting.backend = "plotly" df = pd.DataFrame(dict(a=[1,3,2], b=[3,2,1])) fig = df.plot(title="Pandas Backend Example", template="simple_white", labels=dict(index="time", value="money", variable="option")) fig.update_yaxes(tickprefix="$") fig.show()A Note on API Compatibility¶
The Plotly plotting backend for Pandas is not intended to be a drop-in replacement for the default; it does not implement all or even most of the same keyword arguments, such as
subplots=True
etc.
The Plotly plotting backend for Pandas is a more convenient way to invoke certain Plotly Express functions by chaining a .plot()
call without having to import Plotly Express directly. Plotly Express, as of version 4.8 with wide-form data support in addition to its robust long-form data support, implements behaviour for the x
and y
keywords that are very similar to the matplotlib
backend.
In practice, this means that the following two ways of making a chart are identical and support the same additional arguments, because they call the same underlying code:
In [3]:
import pandas as pd pd.options.plotting.backend = "plotly" df = pd.DataFrame(dict(a=[1,3,2], b=[3,2,1])) # using Plotly Express via the Pandas backend fig1 = df.plot.bar() fig1.show() # using Plotly Express directly import plotly.express as px fig2 = px.bar(df) fig2.show()
In [4]:
import pandas as pd pd.options.plotting.backend = "plotly" df = pd.DataFrame(dict(a=[1,3,2], b=[3,2,1])) fig = df.plot.bar(facet_row="variable") 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. 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.
In [5]:
import pandas as pd import numpy as np pd.options.plotting.backend = "plotly" np.random.seed(1) df = pd.DataFrame(dict( a=np.random.normal(loc=1, scale=2, size=100), b=np.random.normal(loc=2, scale=1, size=100) )) fig = df.plot.scatter(x="a", y="b") fig.show()
In [6]:
import pandas as pd pd.options.plotting.backend = "plotly" df = pd.DataFrame(dict(a=[1,3,2], b=[3,2,1])) fig = df.plot.line() fig.show()
In [7]:
import pandas as pd pd.options.plotting.backend = "plotly" df = pd.DataFrame(dict(a=[1,3,2], b=[3,2,1])) fig = df.plot.area() fig.show()
In [8]:
import pandas as pd pd.options.plotting.backend = "plotly" df = pd.DataFrame(dict(a=[1,3,2], b=[3,2,1])) fig = df.plot.bar() fig.show()
In [9]:
import pandas as pd pd.options.plotting.backend = "plotly" df = pd.DataFrame(dict(a=[1,3,2], b=[3,2,1])) fig = df.plot.barh() fig.show()
In [10]:
import pandas as pd import numpy as np pd.options.plotting.backend = "plotly" np.random.seed(1) df = pd.DataFrame(dict( a=np.random.normal(loc=1, scale=2, size=100), b=np.random.normal(loc=2, scale=1, size=100) )) fig = df.plot.hist() fig.show()
In [11]:
import pandas as pd import numpy as np pd.options.plotting.backend = "plotly" np.random.seed(1) df = pd.DataFrame(dict( a=np.random.normal(loc=1, scale=2, size=100), b=np.random.normal(loc=2, scale=1, size=100) )) fig = df.plot.box() fig.show()
Series
and DataFrame
functions: hist
and boxplot
¶
The Pandas plotting API also exposes .hist()
on DataFrame
s and Series
objects, and .boxplot()
on DataFrames
, which can also be used with the Plotly backend.
In [12]:
import pandas as pd import numpy as np pd.options.plotting.backend = "plotly" np.random.seed(1) df = pd.DataFrame(dict( a=np.random.normal(loc=1, scale=2, size=100), b=np.random.normal(loc=2, scale=1, size=100) )) fig = df.boxplot() fig.show()What about Cufflinks?¶
There also exists an independent third-party wrapper library around Plotly called 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.
Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash at https://dash.plot.ly/installation.
Everywhere in this page that you see fig.show()
, you can display the same figure in a Dash application by passing it to the figure
argument of the Graph
component from the built-in dash_core_components
package like this:
import plotly.graph_objects as go # or plotly.express as px fig = go.Figure() # or any Plotly Express function e.g. px.bar(...) # fig.add_trace( ... ) # fig.update_layout( ... ) from dash import Dash, dcc, html app = Dash() app.layout = html.Div([ dcc.Graph(figure=fig) ]) app.run(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4