LoginSignup
72
68

More than 5 years have passed since last update.

HoloViewsの基本的なグラフをワンライナーで

Last updated at Posted at 2017-08-01

前回 に続き、今回は基本的なグラフを紹介します。

グラフの種類

グラフは基本的に holoviews.element モジュールに属しているクラスを用いて描画します。

holoviews.elements_list を参照することで一覧を確認する方法もあります。

import holoviews as hv

print(hv.elements_list)
['Tabular', 'Curve', 'Annotation', 'Histogram', 'GridImage', 'Bounds', 'VectorField', 'Trisurface', 'HSV', 'Dataset', 'Area', 'Spikes', 'BoxWhisker', 'Spread', 'VLine', 'Spline', 'Chart', 'Surface', 'Element3D', 'Arrow', 'Polygons', 'Points', 'ErrorBars', 'QuadMesh', 'HeatMap', 'Ellipse', 'Box', 'Raster', 'Table', 'Text', 'Image', 'HLine', 'ItemTable', 'Element2D', 'Contours', 'Bars', 'Scatter', 'Element', 'Scatter3D', 'Path', 'BaseShape', 'RGB']

今回は基本的なグラフである holoviews.element.chart を紹介します。

事前準備

必要なモジュールのインポート、サンプルデータを用意します。
(この時点でワンライナーじゃないというツッコミは勘弁してください)

import holoviews as hv
import numpy as np

hv.extension('bokeh')

np.random.seed(111)
x = np.linspace(-np.pi, np.pi, 100)

バックエンドはBokehで設定していますが、matplotlibでも動作します。plotlyでは一部描画されないグラフがあります。

折れ線グラフ

hv.Curve((x, np.sin(x)))

curve.png

面グラフ

hv.Area((x, np.sin(x)))

area.png

散布図

hv.Points(np.random.randn(100, 2))

または

hv.Scatter(np.random.randn(100, 2))

points.png

PointsScatter の違いは確認中です。

棒グラフ

hv.Bars((list('abc'), range(1, 4)))

bars.png

ヒストグラム

自動計算はしてくれないので、度数とビンのエッジはnumpyから算出しています。

hv.Histogram(np.histogram(np.random.randn(1000)))

histogram.png

散布図などのグラフから histメソッドを呼ぶことでヒストグラムを追加できます。すごく簡単ですね。

hv.Points(np.random.randn(100, 2)).hist()

image.png

箱ひげ図

hv.BoxWhisker(np.random.randn(1000))

boxwhisker.png

エラーバー

単体では味気ないので折れ線グラフを追加しています。

hv.ErrorBars([(i, np.sin(i), np.random.rand() / 10)
              for i in x]) * hv.Curve((x, np.sin(x)))

errorbars.png

スプレッド

元のデータに幅を与えたグラフです。(うまく説明できていない気がするのでツッコミ歓迎)
ボリンジャーバンドを作るのによさそうです。

hv.Spread((x, np.sin(x), np.random.rand(100)))

spread.png

スペクトラム

hv.Spikes(np.random.randn(100))

spikes.png

下記のように散布図を組み合わせることもできます。

p = hv.Points((np.random.randn(100, 2)))
p << hv.Spikes(p['y']) << hv.Spikes(p['x'])

image.png

ベクトル場

hv.VectorField((range(10), range(10), np.random.rand(10), np.random.rand(10)))

vectorfield.png

TBD

追加情報があれば順次アップデートしていきます。
* 各引数の説明
* グラフの設定(書式、軸など)

優先して解説がほしい内容などがあればコメントいただければと思います。

72
68
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
72
68