NumPyのndarrayから2次元グラフ
準備するデータはこれ
Notebook
import numpy as np
x = np.linspace(0, 10)
y1 = np.sin(x)
y2 = np.cos(x) * 2
Matplotlib
Notebook
import matplotlib.pyplot as plt
# Notebook出力には次の1行が必要
%matplotlib inline
plt.figure(figsize=(8, 6)) # グラフのサイズ指定(この行は省略可)
plt.plot(x, y1)
plt.plot(x, y2)
Bokeh
bokeh.plottingモジュールの場合
Notebook
from bokeh.plotting import output_notebook, figure, show
output_notebook() # <- Notebook出力にはこの1行が必要
fig = figure(width = 600, height=400) # width, heightは省略可
fig.line(x, y1)
fig.line(x, y2)
show(fig)
![スクリーンショット 2017-01-28 17.51.46.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F37701%2F6dbedcb0-d29b-8c2f-5544-610ef73d5583.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=322da978bf719b69939c42e2bbd6009b)
Matplotlibに近い感じで覚えやすいけど、色を個別に指定する必要があるのが面倒。
公式ドキュメント:Plotting with Basic Glyphs
bokeh.chartsモジュールの場合
Notebook
from bokeh.charts import output_notebook, Line, show
output_notebook() # <- Notebook出力にはこの1行が必要
chart = Line(data={'x': x, 'y1': y1, 'y2': y2}, x='x',
width=600, height=400) # width, heightは省略可
show(chart)
![スクリーンショット 2017-01-28 17.55.39.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F37701%2F008e9ba3-5bcc-2c4c-7be0-c49736c804f9.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=ed221f92828e279d6535a2eb7f0b1229)
単純なデータを素早くプロットするには向いていないけど、自動的に軸ラベルやレジェンドが入ったりと気が利いている。
公式ドキュメント:Making High-level Charts
MatplotlibからにBokehに変換
notebook
import matplotlib.pyplot as plt
from bokeh.plotting import output_notebook, show
from bokeh.mpl import to_bokeh
output_notebook() # <- Notebook出力にはこの1行が必要
plt.plot(x, y1)
plt.plot(x, y2)
show(to_bokeh())
![スクリーンショット 2017-02-02 23.49.33.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F37701%2F1bcf912c-e128-2195-98f6-994b47dd4ae4.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=326ed98e5f1eac88c6c86815ba90c90b)
公式ドキュメント:Leveraging Other Libraries
Plotly
グラフのサイズを指定しない場合
Notebook
from plotly.graph_objs import Scatter
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode() # <- Notebook出力にはこの1行が必要
scatter1 = Scatter(x=x, y=y1)
scatter2 = Scatter(x=x, y=y2)
iplot([scatter1, scatter2]) # <- Notebookに出力するにはiplot関数を使う
![スクリーンショット 2017-02-02 23.40.48.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F37701%2Fa121440a-c204-cbed-c371-1486f54e7391.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=b2d018de02ccafcb857ceb773ff964a1)
公式ドキュメント:Plotly Offline for IPython Notebooks
グラフのサイズを指定する場合
Notebook
from plotly.graph_objs import Scatter, Figure, Layout
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode() # <- Notebook出力にはこの1行が必要
scatter1 = Scatter(x=x, y=y1)
scatter2 = Scatter(x=x, y=y2)
fig = Figure(data=[scatter1, scatter2], layout=Layout(width=600, height=400))
iplot(fig) # <- Notebookに出力するにはiplot関数を使う
![スクリーンショット 2017-02-02 23.44.25.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F37701%2Fe14d47ee-bc29-765d-997c-b3d0c8ac9a4e.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=f62f2db53e5113a85bed30f573dfacbd)
公式ドキュメント:Python Setting Graph Size
MatplotlibからPlotlyに変換
Notebook
import matplotlib.pyplot as plt
from plotly.offline import init_notebook_mode, iplot_mpl
init_notebook_mode() # <- Notebook出力にはこの1行が必要
plt.plot(x, y1)
plt.plot(x, y2)
iplot_mpl(plt.gcf()) # <- MatplotlibのグラフをNotebookに出力するにはiplot_mpl関数を使う
![スクリーンショット 2017-02-02 23.50.29.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F37701%2F87aa60be-e963-a2be-dcb2-c48e66d33dea.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=174462cf03192992a763547e2baa7258)
pandasのDataFrameから散布図
Bokehのライブラリに最初からDataFrameになっているサンプルデータが含まれているのでそれを使ってプロットする。
Notebook
from bokeh.sampledata import iris
df = iris.flowers
df.head()
![スクリーンショット 2017-02-10 8.04.53.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F37701%2Fd23d03d1-3a4a-b78c-adb2-55c107cd16e6.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=4cdf170bac65b05b04f00cf7b00a1d2f)
Matplotlib
Notebook
import matplotlib.pyplot as plt
# Notebook出力には次の1行が必要
%matplotlib inline
plt.scatter(df['sepal_length'], df['sepal_width'])
Bokeh
Notebook
from bokeh.charts import output_notebook, Scatter, show
output_notebook() # <- Notebook出力にはこの1行が必要
chart = Scatter(data=df, x='sepal_length', y='sepal_width',
width=600, height=400) # width, heightは省略可
show(chart)
![スクリーンショット 2017-02-10 8.01.39.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F37701%2Fc2ac0484-fcf5-b890-798a-4a644ce623da.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=c7910bd4140f2f18cee17726e02fdea8)
公式ドキュメント:Making High-level Charts - Scatter Plots
pandas
Notebook
import matplotlib.pyplot as plt
# Notebook出力には次の1行が必要
%matplotlib inline
df.plot.scatter(x='sepal_length', y='sepal_width')
seaborn
Notebook
import seaborn
# Notebook出力には次の1行が必要
%matplotlib inline
seaborn.jointplot(x='sepal_length', y='sepal_width', data=df)
参考リンク:
Pythonの可視化パッケージの使い分け - Qiita
なんでもかんでもJupyter Notebookに表示するためのチートシート 3次元プロット編 - Qiita