準備
まずはmatplotlibをインポートし、適当なグラフを描く
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,2*np.pi)
y = np.sin(x)
plt.plot(x,y)
各種設定
Grid線を表示
Grid線の表示
plt.plot(x,y)
plt.grid()
軸ラベルの設定
plt.plot(x,y)
plt.xlabel("x")
plt.ylabel("y")
表示範囲の変更
plt.plot(x,y)
plt.xlim([0,1])
plt.ylim([0,1])
スタイルシート
matplotlibにはあらかじめ用意されたスタイルシートがあり、それらを用いることもできる。使用可能なスタイルシートを調べるには次のようにします。
print(plt.style.available)
['bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn-dark', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'seaborn', 'Solarize_Light2', 'tableau-colorblind10', '_classic_test']
スタイルシートを使うときは、以下のように記述すると、全体のスタイルに影響を及ぼさないように出来ます。
with plt.style.context('dark_background'):
plt.plot(x,y)




