LoginSignup
0
0

Python3-Matplotlibの使い方 ~信号処理~ 複数信号の表示,間隔・空白の調整など

Posted at

はじめに

matplotlibの使い方に関して,少しだけ知識が溜まってきたので一度整理します.

基本のグラフ

例えば以下のようなプログラムを実行してみましょう.

import numpy as np
import matplotlib.pyplot as plt

# 表示する信号 #######################
fs = 44.1e3
T = 1
f = 3
t = np.arange(0, T-1/fs, 1/fs)
x = np.sin(2*np.pi*f*t)
###################################

# Set global plot style
plt.rcParams.update({
    "text.usetex": True,
    "mathtext.fontset": 'stix',
    "font.family": "STIXGeneral",
    "font.sans-serif": "STIXGeneral",
    'font.size': 15,
    'xtick.direction': 'in',
    'ytick.direction': 'in',
    'xtick.major.width': 1.0,
    'ytick.major.width': 1.0,
    'axes.linewidth': 1.0,
})  

# プロットの作成 ########################################
fig, axes = plt.subplots(dpi=300) # プロットを作成
fig.patch.set_facecolor('white') # 背景色
fig.set_size_inches(5, 3) # 比率を設定する

axes.plot(t,x) # 表示

axes.set_xlabel("Time [s]") # x軸ラベル
axes.set_ylabel("Amplitude [a.u.]") # y軸ラベル

fig.tight_layout() # 細いレイアウトにする

plt.savefig("sample.png", dpi=300) # 保存する

plt.show() # 表示する
####################################################

結果は以下のようになります.

sample.png

複数の表示

複数のグラフを表示したい場合,以下のプログラムが考えられます.

import numpy as np
import matplotlib.pyplot as plt

# 表示する信号 #######################
fs = 44.1e3
T = 1
f1 = 3
t = np.arange(0, T-1/fs, 1/fs)
x1 = np.sin(2*np.pi*f1*t)

f2 = 5
x2 = np.sin(2*np.pi*f2*t)
###################################

# Set global plot style
plt.rcParams.update({
    "text.usetex": True,
    "mathtext.fontset": 'stix',
    "font.family": "STIXGeneral",
    "font.sans-serif": "STIXGeneral",
    'font.size': 15,
    'xtick.direction': 'in',
    'ytick.direction': 'in',
    'xtick.major.width': 1.0,
    'ytick.major.width': 1.0,
    'axes.linewidth': 1.0,
})  

# プロットの作成 ########################################
fig, axes = plt.subplots(dpi=300) # プロットを作成
fig.patch.set_facecolor('white') # 背景色
fig.set_size_inches(5, 3) # 比率を設定する

axes.plot(t,x1) # 表示
axes.plot(t,x2)

axes.set_xlabel("Time [s]") # x軸ラベル
axes.set_ylabel("Amplitude [a.u.]") # y軸ラベル

fig.tight_layout() # 細いレイアウトにする

plt.savefig("sample.png", dpi=300) # 保存する

plt.show() # 表示する
####################################################

結果は以下のようになります.

sample.png

複数の表示 ~part2~

重ならず表示したい場合,以下のプログラムが考えられます.

import numpy as np
import matplotlib.pyplot as plt

# 表示する信号 #######################
fs = 44.1e3
T = 1
f1 = 3
t = np.arange(0, T-1/fs, 1/fs)
x1 = np.sin(2*np.pi*f1*t)

f2 = 5
x2 = np.sin(2*np.pi*f2*t)
###################################

# Set global plot style
plt.rcParams.update({
    "text.usetex": True,
    "mathtext.fontset": 'stix',
    "font.family": "STIXGeneral",
    "font.sans-serif": "STIXGeneral",
    'font.size': 15,
    'xtick.direction': 'in',
    'ytick.direction': 'in',
    'xtick.major.width': 1.0,
    'ytick.major.width': 1.0,
    'axes.linewidth': 1.0,
})  

# プロットの作成 ########################################
fig, axes = plt.subplots(nrows=2, dpi=300) # プロットを作成
fig.patch.set_facecolor('white') # 背景色
fig.set_size_inches(5, 3) # 比率を設定する

axes[0].plot(t,x1) # 表示
axes[1].plot(t,x2)

axes[1].set_xlabel("Time [s]") # x軸ラベル
fig.supylabel("Amplitude [a.u.]", y = 0.56, x = 0.06) # y軸ラベル

fig.tight_layout() # 細いレイアウトにする

plt.savefig("sample.png", dpi=300) # 保存する

plt.show() # 表示する
####################################################

結果は以下のようになります.

sample.png

supylabelという関数によって,y軸の表示が単調であることを解消しています.

複数の表示 ~part3~

part2のグラフにおいてxの表示が単調と感じた場合,以下のプログラムが考えられます.

import numpy as np
import matplotlib.pyplot as plt

# 表示する信号 #######################
fs = 44.1e3
T = 1
f1 = 3
t = np.arange(0, T-1/fs, 1/fs)
x1 = np.sin(2*np.pi*f1*t)

f2 = 5
x2 = np.sin(2*np.pi*f2*t)
###################################

# Set global plot style
plt.rcParams.update({
    "text.usetex": True,
    "mathtext.fontset": 'stix',
    "font.family": "STIXGeneral",
    "font.sans-serif": "STIXGeneral",
    'font.size': 15,
    'xtick.direction': 'in',
    'ytick.direction': 'in',
    'xtick.major.width': 1.0,
    'ytick.major.width': 1.0,
    'axes.linewidth': 1.0,
})  

# プロットの作成 ########################################
fig, axes = plt.subplots(nrows=2, dpi=300) # プロットを作成
fig.patch.set_facecolor('white') # 背景色
fig.set_size_inches(5, 3) # 比率を設定する

axes[0].plot(t,x1,color="m") # 表示
axes[1].plot(t,x2,color="c")

axes[0].spines['bottom'].set_visible(False)
axes[0].xaxis.set_ticks([])
axes[1].spines['top'].set_visible(False)

axes[0].yaxis.set_ticks([0],["0"])
axes[1].yaxis.set_ticks([0],["0"])

fig.tight_layout() # 細いレイアウトにする

fig.subplots_adjust(hspace=0.0)

axes[1].set_xlabel("Time [s]") # x軸ラベル
fig.supylabel("Amplitude [a.u.]", y = 0.56, x = -0.05) # y軸ラベル

plt.savefig("sample.png", dpi=300) # 保存する

plt.show() # 表示する
####################################################

結果は以下のようになります.

sample.png

さいごに

グラフの作成の際には,上記のテクニックでなんとかなります.

fig, axes = plt.subplots(ncols=2, dpi=300, gridspec_kw={'width_ratios': [1, 2]})
fig, axes = plt.subplots(nrows=2, dpi=300, gridspec_kw={'height_ratios': [1, 2]})

などのコードによって,比率を調整することができます.

0
0
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
0
0