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

python3 で、os.environ で環境変数を取得する

環境変数をセットするには、ターミナルで export SOME_VAR=”hello 123″ などとして環境変数(文字列)を設定する。 プログラム(python)内で環境変数を取得するには次のように …

no image

matplotlib で折れ線グラフの下部に色をつけて塗りつぶす方法

pyplot の fill_between を使う。 例 from matplotlib import pyplot as plt data_x = [25,26,27] data_y = & …

no image

python3 で集合(set)の和集合を作る

集合にモノを付け足して和集合を作るには、|= 演算子を使う。(python では、集合(set)を波かっこで囲んで表す。) 例 a = {‘東京’} b = {‘大阪’,’千葉’} c = a | b …

no image

matplotlib で、グラフの点の見た目を変更する方法

「marker」で指定すればよい。 次のページを参考に、plotの「marker」を変更する。 https://matplotlib.org/stable/api/markers_api.html 例 …

no image

matplotlib でエラーバーを設定する方法

pyplot.errorbar を使って設定する。 オプションの詳細は https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.erro …