{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import hvplot.pandas\n", "\n", "from bokeh.sampledata.penguins import data as df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " The Explorer has been added to hvPlot in version 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.\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "To create an *Explorer* you pass your data to the high-level `hvplot.explorer` function which returns a [Panel](https://panel.holoviz.org/) 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)`.\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hvexplorer = hvplot.explorer(df)\n", "hvexplorer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The *Explorer* has a few useful methods:\n", "\n", "* `save(filename, **kwargs)` to save the plot to file\n", "* `settings()` to obtain a dictionary of your customized settings\n", "* `plot_code(var_name)` to obtain a formatted string to copy/paste that includes your customized settings\n", "* `hvplot()` to get a handle on the displayed HoloViews plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You will now see how to use the *Explorer* in combination with these methods.\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hvexplorer.param.set_param(kind='scatter', x='bill_length_mm', y_multi=['bill_depth_mm'], by=['species'])\n", "hvexplorer.labels.title = 'Penguins Scatter'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hvexplorer.save('plot.html')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "settings = hvexplorer.settings()\n", "settings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.hvplot(**settings)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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'`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hvexplorer.plot_code()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copy/pasting this string without the outer quotes into the next cell is all you need to recreate the exact same plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.hvplot(by=['species'], kind='scatter', title='Penguins Scatter', x='bill_length_mm', y=['bill_depth_mm'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 5 }