Introduction to hvPlot

BackGround: Why hvplot?

Holoviz eco system

The Holoviz ecosystem is a suite of compatible tools designed to make data visualization in Python easier and more powerful. It supports every stage of data analysis, from importing and cleaning data, exploring, hypothesis testing, generating accurate and compelling diagrams, to sharing and deploying live applications.

The main features are as follows:

Representative components include the following:

HoloViews:
Separate data and its visual representation to easily create complex interactive diagrams
Panel:
Build interactive dashboards and web applications using only Python
hvPlot:
Easily create interactive plots from data structures such as pandas and xarray
Datashader:
Fast rendering of large datasets with millions of points or more
GeoViews:
Extend the visualization of geospatial data

Thus, Holoviz is built on top of the Python scientific and technological stack and is an ecosystem that comprehensively supports data visualization.

hvplot

hvplot is a high-level API developed as part of the Holoviz ecosystem (Holoviews, Panel, Datashader, etc.) that allows you to visualize pandas/xarray/Dask data directly.

Features of hvplot

In particular, it is strong in meeting the need for "interactive visualization without increasing the amount of code."

Basic Structure of hvplot

hvplot operates as an extension of pandas.

import hvplot.pandas

This alone adds the .hvplot method to pandas DataFrames/Series. By default, Bokeh is used as the backend, but you can also specify matplotlib.

Basic Examples

  1. !pip install hvplot
    import pandas as pd
    import matplotlib.pyplot as plt
    # Sample DataFrame
    data = {'Year': [2017, 2018, 2019, 2020, 2021],
            'Sales': [250, 300, 400, 350, 500]}
    df = pd.DataFrame(data)
    # Plotting a line graph
    df.plot(x='Year', y='Sales', kind='line')
    plt.xlabel('Year')
    plt.ylabel('Sales')
    plt.title('Yearly Sales')
    plt.show()
    
    Standard matplotlib plotting
  2. import hvplot.pandas
    #hvplot.extension('bokeh')
    plot = df.hvplot(x='Year', y='Sales', kind='line')
    plot.opts(xlabel='Year')
    plot.opts(ylabel='Sales')
    plot.opts(title='Yearly Sales')
    Standard hvplot plotting (bokeh backend)
  3. hvplot.extension('matplotlib')
    plot2 = df.hvplot(x='Year', y='Sales',kind='line')
    plot2.opts(xlabel='Year')
    plot2.opts(ylabel='Sales')
    plot2.opts(title='Yearly Sales')
    hvplot plotting with matplotlib as the backend.

Strengths of hvplot: Interactivity

Because hvplot uses Holoviews internally, zoom, pan, hover, and toolbars are automatically included.

Without additional code, the following operations are possible:

Plot

When drawing a graph with hvplot, specify the graph type as a string or as a method in the hbplot kind option.

line
Line graph
scatter
Scatter plot
bar
Bar graph
barh
Horizontal bar graph
hist
Histogram
box
Box plot
heatmap
Heatmap

* Pie charts are not officially supported.

You can adjust axis labels, colors, fonts, etc. using .opts.

Basic Visualization Examples

Line Graph

import pandas as pd
import hvplot.pandas
df=pd.DataFrame({'x':[1,2,3],'y':[4,5,2]})
df.hvplot.line(x='x',y='y')

Bar Graph

df=pd.DataFrame({'section': ['Div-A', 'Div-B', 'Div-c', 'Div-D', 'Div-E'],'students': [23,17,35,29,12]})
df.hvplot.bar(x='section',y='students')

Histgram

df=pd.DataFrame({'data':[12,80,30,65,60,73,55,54]})
plot=df.hvplot.hist(y='data', bins = 'auto')
plot.opts(title="HISTOGRAM")
plot.opts(xlabel='MARKS')
plot.opts(ylabel='STUDENTS')
plot

Scatter

girls_grades = [83, 90, 71, 80, 100, 87, 92, 99, 40, 34]
boys_grades = [70, 39, 59, 43, 100, 45, 74, 95, 87, 30]
grades_range = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
df=pd.DataFrame({'girls_grades':girls_grades,'boys_grades':boys_grades,'grades_range':grades_range})
plot1=df.hvplot.scatter(x='grades_range', y='girls_grades', color='r')
plot2=df.hvplot.scatter(x='grades_range', y='boys_grades', color='b')
plot=plot1*plot2
plot.opts(xlabel='Grades Range')
plot.opts(ylabel='Grades Scored')
plot.opts(title='scatter plot')
plot

Conclusions


坂本直志 <sakamoto@c.dendai.ac.jp>
東京電機大学工学部情報通信工学科