#!/usr/bin/env python # coding: utf-8 # # B.1 pyplotでの可視化 # In[4]: import matplotlib as mpl import matplotlib.pyplot as plt import seaborn as sns import numpy as np # numpyも使用するので、併せてimport # In[7]: # jupyter notebookでは必要 get_ipython().run_line_magic('matplotlib', 'inline') # In[2]: x = np.array([1,2,3,4,5]) # xにnumpyの配列を代入 y = np.array([0,0,0,1,1]) # yにnumpyの配列を代入 # ## B.1.1 散布図 # In[8]: plt.scatter(x,y); # 散布図を描画 # ## B.1.2 折れ線グラフ # In[10]: plt.plot(x, y); # In[11]: plt.plot(x,y, label = '0') # labelを0に設定 plt.plot(x,y+1, label = '1') # yをy+1に設定、labelは1に設定 plt.plot(x,y+2, label = '2') # yをy+2に設定、labelは2に設定 plt.legend() # 凡例を表示 # ## B.1.3 棒グラフ # # In[13]: plt.bar(x,x); # ## B.1.4 複数の図を同時に描く # In[18]: plt.subplot(221) # 2行2列の1つめ plt.scatter(x,y) plt.subplot(222) # 2行2列の2つめ plt.plot(x,y) plt.subplot(2,2,3) # 2行2列の3つめ、このように,区切りも可能 plt.plot(x,y, label = '0') plt.plot(x,y+1, label = '1') plt.plot(x,y+2, label = '2') plt.legend(loc = 'upper left'); plt.subplot(224) # 2行2列の4つめ plt.bar(x,x) # # B.2 pyplotのfigure関数を用いる方法 # In[21]: # Figureのインスタンスを生成 fig = plt.figure() # Axesのインスタンスを生成 ax1 = fig.add_subplot(311) ax2 = fig.add_subplot(312) ax3 = fig.add_subplot(313) # ax1,ax2,ax3にplot ax1.bar(x, x) ax2.scatter(x, y+1) ax3.plot(x,y, label = '0') ax3.plot(x,y+1, label = '1') ax3.plot(x,y+2, label = '2') ax3.legend(loc='upper left')