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()?
But did you know that it is also possible to create interactive plots with matplotlib directly, provided you are using an interactive backend? This article will look at two such backends and how they render interactivity within the notebooks, using only matplotlib.
Matplotlib can be used in an interactive or non-interactive modes. In the interactive mode, the graph display gets updated after each statement. In the non-interactive mode, the graph does not get displayed until explicitly asked to do so.
With a DataFrame , pandas creates by default one line plot for each of the columns with numeric data.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With