Change pandas plotting backend to get interactive plots instead of matplotlib static plots
Asked Answered
O

4

26

When I use pandas df.plot() it has matplotlib as a default plotting backend. But this creates static plots.

I would like interactive plots, so I have to change the pandas plotting background.

How do I do change the plotting backend of pandas to have a different library creating my plots when i use .plot()?

Olva answered 17/10, 2019 at 20:47 Comment(0)
O
41

You need pandas >= 0.25 to change the plotting backend of pandas.

The available plotting backends are:

So, the default setting is:

pd.options.plotting.backend = 'matplotlib'

You can change the plotting library that pandas uses as follows. In this case it sets hvplot / holoviews as the plotting backend:

pd.options.plotting.backend = 'hvplot'

Or you can also use (which is basically the same):

pd.set_option('plotting.backend', 'hvplot')

Now you have hvplot / holoviews as your plotting backend for pandas and it will give you interactive holoviews plots instead of static matplotlib plots.

Of course you need to have library hvplot / holoviews + dependencies installed for this to work.

Here's a code example resulting in an interactive plot. It uses the standard .plot() pandas syntax:

import numpy as np
import pandas as pd

import hvplot
import hvplot.pandas

pd.options.plotting.backend = 'hvplot'

data = np.random.normal(size=[50, 2])

df = pd.DataFrame(data, columns=['x', 'y'])

df.plot(kind='scatter', x='x', y='y')
Olva answered 17/10, 2019 at 20:47 Comment(5)
Is it possible to use plotly as plotting backend as well?Omari
Plotly backend is available with plotly >= 4.8.0Olva
How would you adjust the size of the plot when using plotly as a backend?Janelljanella
Note that the arguments of the plot functions do not seem to be standardized. Therefore, it might not be possible to easily switch the alternative plotting backends for existing code. For example the matplotlib based plot function supports an "ax" argument, that can be used to consider subplot layouts: pandas.pydata.org/pandas-docs/stable/reference/api/… The plotly based plot function supports a facet argument that could be used for similar purpose: plotly.com/python/pandas-backend plotly.com/python/facet-plotsLandlady
For subplots with bokeh see for example #66906279Landlady
R
8

As of plotly 4.8.0 you can use plotly for interactive plotting with pandas 1.0+.

Update with pip install -U plotly

Set the plotting backend to plotly:

pd.options.plotting.backend = "plotly" 

df = pd.DataFrame(dict(a=[1, 2, 3], b=[2, 4, 6]))
df.plot()

Here's the announcement

Note that these backends don't have full alignment with all the arguments that work with the default matplotlib backend.

Also note that Altair requires altair_pandas installed.

Reticent answered 26/5, 2020 at 18:16 Comment(2)
somehow I keep getting the error 'Could not find plotting backend 'plotly'. Ensure that you've installed the package providing the 'plotly' entrypoint, or that the package has a top-level .plot method.' Should we not mention anything with df.plot(backend = 'plotly') along with the above statement?Cresset
@julianjoseph sounds like pandas is looking for plotly and not finding it. Do you have an updated version of plotly and an updated version of pandas installed - and they are the versions that are active in your environment? You can check by importing both libraries and checking their version attributes. If using Jupyter and recently upgraded or installed the packages, you could try restarting your kernel.Reticent
O
5

To change the pandas plotting backend for the whole session, use:

pd.options.plotting.backend = 'plotly'

To change the pandas plotting backend only for one particular plot, use:

df.plot(backend='plotly')

See also:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html

https://pandas.pydata.org/docs/user_guide/options.html?highlight=plotting%20backend#available-options

Olva answered 2/9, 2020 at 20:29 Comment(0)
H
1

You can also alternate backends using the backend to the plot method itself:

df.plot(backend='matplotlib')

I'm learning a new backend and I use it when I want to reproduce my chart in matplotlib. The parameters of the plot method aren't the same in different backends.

Hygrostat answered 4/5, 2021 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.