import hvplot.pandas
from bokeh.sampledata.penguins import data as df
0.8.0
, and improved and documented in version 0.8.1
. It does not yet support all the data structures supported by hvPlot, for now it works best with Pandas DataFrame objects. Please report any issue or feature request on GitHub.
hvPlot API provides a simple and intuitive way to create plots. However when you are exploring data you don't always know in advance the best way to display it, or even what kind of plot would be best to visualize the data. You will very likely embark in an iterative process that implies choosing a kind of plot, setting various options, running some code, and repeat until you're satisfied with the output and the insights you get. The Explorer is a Graphical User Interface that allows you to easily generate customized plots, which in practice gives you the possibility to explore both your data and hvPlot's extensive API.
To create an Explorer you pass your data to the high-level hvplot.explorer
function which returns a Panel layout that can be displayed in a notebook or served in a web application. This object displays on the right-hand side a preview of the plot you are building, and on the left-hand side the various options that you can set to customize the plot. These options can passed to the constructor if you already have pre-defined some, for example hvplot.explorer(data, title='Penguins', width=200)
.
Note that for the explorer to be displayed in a notebook you need to load the hvPlot extension, which happens automatically when you execute import hvplot.pandas
. If instead of building Bokeh plots you would rather build Matplotlib or Plotly plot, simply execute once hvplot.extension('matplotlib')
or hvplot.extension('matplotlib')
before displaying the explorer.
hvexplorer = hvplot.explorer(df)
hvexplorer
The Explorer has a few useful methods:
save(filename, **kwargs)
to save the plot to filesettings()
to obtain a dictionary of your customized settingsplot_code(var_name)
to obtain a formatted string to copy/paste that includes your customized settingshvplot()
to get a handle on the displayed HoloViews plotYou will now see how to use the Explorer in combination with these methods.
First we will update the Explorer as the default configuration doesn't lead to a very interesting preview for this dataset. We will do so programmatically for the purpose of building this website but you would never have to do such thing, so just assume you've changed a few options directly in the explorer with your mouse and keyboard.
hvexplorer.param.set_param(kind='scatter', x='bill_length_mm', y_multi=['bill_depth_mm'], by=['species'])
hvexplorer.labels.title = 'Penguins Scatter'
We may already be satisfied with the plot as is and decide to save a copy, in this case in an HTML file as we have created a Bokeh plot and would like to preserve its interactivity.
hvexplorer.save('plot.html')
The options we have changed in the Explorer can obtained as a dictionary from the settings()
method and be passed as kwargs to hvplot.explorer
to initialize another Explorer, or directly to the .hvplot()
data accessor. It means that in your workflow you can temporarily create Explorers, use them to quickly iterate on building new plots, record their settings, and finally replace the Explorers by shorter .hvplot()
calls.
settings = hvexplorer.settings()
settings
Note that for the next line to display a plot hvplot.pandas
has to be imported, which we did at the beginning of this notebook.
df.hvplot(**settings)
Another practical way to create a plot from the recorded options is to use the plot_code
method that generates a string ready to be executed after a copy/paste into a notebook code cell. plot_code
assumes that you data variable is named 'df'
, you can change that by passing e.g. var_name='df_othername'
.
hvexplorer.plot_code()
Copy/pasting this string without the outer quotes into the next cell is all you need to recreate the exact same plot.
df.hvplot(by=['species'], kind='scatter', title='Penguins Scatter', x='bill_length_mm', y=['bill_depth_mm'])
The Explorer makes it very easy to quickly spin up a small application in a notebook with which you can explore your data, generate the visualization that you want, record it in a simple way, and keep going with your analysis.