Introduction to hvPlot

1. 背景:なぜ hvplot なのか

Holovizエコシステム

Holovizエコシステムは、Pythonでのデータ可視化をより簡単かつ強力にするための一連の互換性のあるツール群です。データのインポートやクリーニング、探索、仮説検証、正確で説得力のある図の生成、ライブアプリの共有・展開など、データ分析のあらゆる段階をサポートします。

主な特徴は以下の通りです。

代表的なコンポーネントには、以下があります。

HoloViews:
データとその視覚表現を分離し、複雑なインタラクティブ図を簡単に作成
Panel:
インタラクティブなダッシュボードやWebアプリをPythonだけで構築
hvPlot:
pandas やxarrayなどのデータ構造から簡単にインタラクティブなプロットを作成
Datashader:
数百万点以上の大規模データを高速に描画
GeoViews:
地理空間データの可視化を拡張

このように、HolovizはPythonの科学技術スタックの上に構築され、データの可視化を包括的に支援するエコシステムです。

hvplot

hvplot は Holoviz エコシステム(Holoviews, Panel, Datashader など)の一部として開発された、 pandas / xarray / Dask データをそのまま可視化できる高水準 API です。

hvplot の特徴

特に、「コード量を増やさずにインタラクティブな可視化をしたい」というニーズに強いです。

2. hvplot の基本構造

hvplot は pandas の拡張として動作します。

import hvplot.pandas

これだけで、pandas の DataFrame / Series に .hvplot メソッドが追加されます。 デフォルトではバックエンドに Bokeh が使われますが、matplotlibを指定す ることも可能です。

基本例

  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()
    
    通常のmatplotlib描画
  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')
    通常のhvplot描画(bokehバックエンド)
  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')
    matplotlibをバックエンドに指定したhvplot描画

3. hvplot の強み:インタラクティブ性

hvplot は Holoviews を内部で使っているため、 ズーム・パン・ホバー・ツールバーが自動で付きます。

追加コードなしで、次のような操作が可能になります。

プロット

hvplotでグラフを描画するときは、hbplotのkindオプションにグラフの種別 を文字列で指定するか、メソッドとして指定するかする。

line
折れ線
scatter
散布図
bar
棒グラフ
barh
横棒グラフ
hist
ヒストグラム
box
箱ひげ図
heatmap
ヒートマップ

※ pie chart は公式にサポートされていません。

.opts を使うことで、軸ラベル・色・フォントなどを調整できます。

4. 基本的な可視化例

4.1 折れ線グラフ

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')

4.2 棒グラフ

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')

4.3ヒストグラム

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

スキャッター

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

9. まとめ


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