matplotlib グラフ作成とオプション設定の基本

インポート

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

基本的なグラフ

y=x とy=2x のグラフの描写
#グラフタイトルの設定
plt.title('y=x')
#x軸ラベルの設定
plt.xlabel('x') 
#y軸ラベルの設定
plt.ylabel('y') 
#x軸の範囲の設定
plt.xlim(xmin=0,xmax=10) 
#プロット
plt.plot(range(0,10),range(0,10),  label='y=x')
plt.plot(range(0,10),range(0,20,2),  label='y=2x')
#凡例をつける
plt.legend()

https://cdn-ak.f.st-hatena.com/images/fotolife/m/munemakun/20180719/20180719013651.png?1531931898



横にグラフを並べる

横一列に2つのグラフを表示

#subplot(全体の行数,全体の列数,このグラフは何番目のプロットか)
#1つ目のグラフ
plt.subplot(121)
plt.plot(range(0,10),range(0,10),  label='y=x')
plt.legend()

#2つ目のグラフ
plt.subplot(122) 
x_right = np.array([1, 2, 3, 4, 5])
plt.plot(range(0,10),range(0,20,2),  label='y=2x')
plt.legend()

#タイトルが被るのを防ぐ
plt.tight_layout()

https://cdn-ak.f.st-hatena.com/images/fotolife/m/munemakun/20180719/20180719013655.png?1531931916




もしグラフ中に日本語を使いたい場合はこちらを参考に
munemakun.hatenablog.com