How to Load and Switch Plotting Extensions#
This guide shows how to load hvPlot’s plotting extensions and switch between them. hvPlot supports Bokeh (the default), Matplotlib and Plotly. The examples below use Bokeh and Matplotlib, the steps are the same for Plotly.
Load the Bokeh extension#
Importing an accessor such as hvplot.pandas (or hvplot.xarray, hvplot.polars, etc.) registers the .hvplot API and always loads the Bokeh extension, which becomes the active one. Internally, the import calls hvplot.extension('bokeh'), which in a notebook also adds to the cell output the code needed to display Bokeh plots. No further setup is needed to plot with Bokeh.
import hvplot.pandas # noqa
penguins = hvplot.sampledata.penguins('pandas').dropna()
penguins.hvplot.scatter(
x='bill_length_mm', y='bill_depth_mm',
by='species', title='Bokeh'
)
Load the Matplotlib extension#
Call hvplot.extension() after the import to load another extension. The first extension passed becomes the active one, and Bokeh stays loaded.
hvplot.extension('matplotlib')
penguins.hvplot.scatter(
x='bill_length_mm', y='bill_depth_mm',
by='species', title='Matplotlib'
)
To load several extensions at once, pass them all to a single call, e.g. hvplot.extension('matplotlib', 'plotly').
Warning
Importing an accessor for the first time (e.g. import hvplot.xarray) loads Bokeh and makes it the active extension. In a notebook, this also happens every time a cell containing an accessor import is run, so that plots still render after the notebook is reloaded. Either way, the previous choice of extension and any compatibility setting are reset. Call hvplot.extension after all the accessor imports, and call it again if you re-run an import cell.
Switch between loaded extensions#
Use hvplot.output() to change the active extension. It only switches between extensions that are already loaded.
hvplot.output(backend='bokeh')
penguins.hvplot.scatter(
x='bill_length_mm', y='bill_depth_mm',
by='species', title='Bokeh again'
)
Create plots after switching: hvPlot sets the options of a plot for the extension that is active when the plot is created.
Tip
hvplot.extension can also switch the active extension, but prefer hvplot.output: each extension call adds code to the cell output, which increases the size of the notebook.