matplotlib python3

matplotlib で、y軸に二種類の軸を設定する方法

投稿日:

y軸に、2種類のグラフを異なるスケールでプロットする。

matplotlib axes の twinx() を使う。

https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.twinx.html

import numpy as np
from matplotlib import pyplot as plt

x = np.linspace(-3, 3, num=100, endpoint=True)

y1 = np.sin(x)
y2 = np.exp(-x)

fig, ax = plt.subplots()

ax.plot(x,y1,'red',label='sin(x)')

ax2 = ax.twinx()
ax2.plot(x,y2,'blue',label='exp(x)')

ax.set_ylabel('sin')
ax2.set_ylabel('exp')

plt.show()

結果

-matplotlib, python3
-,

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

matplotlib で、縦横の線(グリッド)を描く方法

pyplot grid で縦横線(罫線のようなもの)を引くことができる。 例 from matplotlib import pyplot as plt data_x = [25,26,27] …

no image

matplotlib で、ヒストグラムを作成する方法

ヒストグラムを描くには、data_hist を使う。 ヒストグラムの縦棒の数は bins で指定する。 例 from matplotlib import pyplot as plt data_hist …

no image

python3 で整数の割り算、商と余りを求める方法

floor divisionを使う。 例 a = 15 // 7 b = 15 % 7 print(a) print(b) 結果 2 1

no image

matplotlib で、縦横の罫線を引く方法

破線をカスタマイズする方法 以下では、dashes = [5,2,2,2] で、線5・空白2・線2・空白2の破線を指定している。 詳しくは次を参照。 https://matplotlib.org/st …

no image

matplotlib でグラフの背景の色を変える方法(facecolor)

次のように、axes で facecolor を変更すればよい。 例 from matplotlib import pyplot as plt x = [2,7,8] y = [7,1 …