LoginSignup
5
8

More than 5 years have passed since last update.

AtomのHydrogenでMatplotlib, Bokeh, HoloViewsおよび他ライブラリのプロットを表示する

Last updated at Posted at 2019-02-18

はじめに

AtomHydrogenMatplotlib, Bokeh, HoloViewsのプロットを表示する方法です。

Matplotlib

import matplotlib.pyplot as plt
plt.scatter([1, 2, 3], [1, 2, 3])

Jupyter Notebookで必要な

%matplotlib inline

は要りません。

Bokeh

from bokeh.plotting import figure, output_notebook, show
from bokeh.resources import INLINE
output_notebook(INLINE)

plot = figure()
plot.circle([1, 2, 3], [1, 2, 3])
show(plot)

output_notebookがないと外部ブラウザにプロットが表示されてしまいます。INLINEは必ずしも必要ないですが、こうすると、BokehJS x.x.x successfully loaded.のメッセージが確認できます。

HoloViews

プロットの準備

import numpy as np
import pandas as pd
import holoviews as hv

xs = np.arange(-10, 10.5, 0.5)
ys = 100 - xs**2
df = pd.DataFrame(dict(x=xs, y=ys))
curve = hv.Curve(df,'x','y')

ここまでは特定の可視化ライブラリに依存していません。

バックエンドがMatplotlibの場合

hv.extension('matplotlib')
hv.render(curve)

バックエンドがBokehの場合

from bokeh.io import show
hv.extension('bokeh')
show(hv.render(curve))

HoloViewsを通してBokehのプロット表示する際には、output_notebookは不要です。また、Jupyter Notebookの場合は、curveをセルの最後に入力するだけでプロットが表示されますが、Hydrogenの場合は、hv.renderや(Bokehの場合には)showが必要となるようです。

hvPlot

import pandas as pd
import numpy as np
import holoviews as hv
import hvplot.pandas
from bokeh.plotting import show

idx = pd.date_range('1/1/2000', periods=1000)
df = pd.DataFrame(np.random.randn(1000, 4), index=idx,
                  columns=list('ABCD')).cumsum()
show(hv.render(df.hvplot()))

Altair

import altair as alt
import pandas as pd

source = pd.DataFrame({
    'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
    'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})

alt.Chart(source).mark_bar().encode(x='a', y='b')
5
8
0

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
5
8