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 でグラフの背景の色を変える方法(facecolor)

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

no image

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

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

no image

matplotlib で、2種類のデータをプロットする方法

下では、凡例(legend)を設定している。 例 from matplotlib import pyplot as plt data_x = [25,26,27] data_y = [ …

no image

python3 で辞書(dictionary)を削除する

clear() することによって、辞書が存在した状態のまま、キーと値をすべて削除することができる。 例 dict1 = {“名前”:”太郎”, “年齢”: 20, “住所”: “東京都千代田区大手町1 …

no image

python でリストに、コロン演算子2つ(::)を使う

リストのコロンで指定できるパラメータには、「start, end, step」という意味がある。 以下の例では、:: 5 とした場合には、5 をステップとして、次に進んでいく。 ::-5 とした場合に …