Ben Chuanlong Du's Blog

It is never too late to learn.

Best Way of Using HoloViews

Comments

  1. DataFrame.hvplot (via the Python package hvplot) is the most convenient way to use HoloViews.

  2. If you want to use HoloViews directly, it is better to create a HoloViews.Dataset object and use it for visualization.

  3. The groupby option generates a plot with a dropdown list for interactively showing plots of different groups. You can overlay plots of different groups together on the same plot simplify by calling the .overlay() method.

In [1]:
!curl -sSL www.legendu.net/media/data/iris.csv -o iris.csv
In [2]:
import pandas as pd
In [3]:
iris = pd.read_csv("iris.csv")
iris.head()
Out[3]:
id sepal_length_cm sepal_width_cm petal_length_cm petal_width_cm species
0 1 5.1 3.5 1.4 0.2 Iris-setosa
1 2 4.9 3.0 1.4 0.2 Iris-setosa
2 3 4.7 3.2 1.3 0.2 Iris-setosa
3 4 4.6 3.1 1.5 0.2 Iris-setosa
4 5 5.0 3.6 1.4 0.2 Iris-setosa

Use the hvplot Library

In [4]:
import holoviews as hv
# import hvplot
import hvplot.pandas

# hv.extension("bokeh")
In [5]:
fig = iris.hvplot.scatter("sepal_width_cm", "sepal_length_cm")
fig
Out[5]:
In [6]:
type(fig)
Out[6]:
holoviews.element.chart.Scatter
In [7]:
iris.hvplot.scatter("sepal_width_cm", "sepal_length_cm", groupby="species")
Out[7]:
In [8]:
iris.hvplot.scatter("sepal_width_cm", "sepal_length_cm", groupby="species").overlay()
Out[8]:
In [9]:
iris.hvplot.scatter("petal_width_cm", "petal_length_cm", groupby="species").overlay()
Out[9]:

Use The HoloViews Library Directly

Pass a tuple (of x and y) to HoloViews.Scatter. You have to manually specify the labels for x and y-axis, otherwise, the default lables x and y will be used.

In [10]:
%%opts Scatter [width=600 height=500 tools=["hover"]]

hv.Scatter((iris.sepal_width_cm, iris.sepal_length_cm)).opts(xlabel="Sepal Width", ylabel="Sepal Length")
Out[10]:
In [11]:
%%opts Scatter [width=600 height=500 tools=['hover']]

hv.Scatter(iris, kdims="sepal_width_cm", vdims="sepal_length_cm")
Out[11]:

Use Dataset In The HoloViews Library

In [12]:
%%opts Scatter [width=600 height=500 tools=["hover"]]
hv.Scatter(iris, kdims=["sepal_width_cm", "species"], vdims=["sepal_length_cm"])
WARNING:param.Scatter: Chart elements should only be supplied a single kdim
Out[12]:
In [13]:
hv.Dataset(iris).to.scatter("sepal_width_cm", "sepal_length_cm", groupby=[])
Out[13]:
In [14]:
hv.Dataset(iris).to.scatter("sepal_width_cm", "sepal_length_cm", groupby="species")
Out[14]:
In [15]:
%%opts Scatter [width=600 height=500]

hv.Dataset(iris).to.scatter("sepal_width_cm", "sepal_length_cm", groupby="species").overlay()
Out[15]:
In [ ]:
 

Comments