16
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

matplotlibで、2軸、3軸でグラフを描画する

Last updated at Posted at 2020-01-03

matplotlibでy軸を複数持たせる方法です。
ax2 = ax1.twinx()
で複数の軸を持つことが出来ます。

テスト用データ

x=[0,1,2,3]
y1=[1,2,3,4]
y2=[10,25,30,45]
y3=[150,200,350,400]

2軸でプロット

ソース

import matplotlib.pyplot as plt

fig, ax1 = plt.subplots( )
ax1.plot(x,y1 ,"b-")
ax1.set_ylabel("y1")

ax2 = ax1.twinx()

ax2.plot(x,y2  ,"r-")
ax2.set_ylabel("y2")

plt.show()

結果

ダウンロード.png

参考

Different scales on the same axes

3軸でプロット

ソース

import matplotlib

fig, ax1 = plt.subplots( )
ax1.plot(x,y1 ,"b-")
ax1.set_ylabel("y1")

ax2 = ax1.twinx()
ax3 = ax1.twinx()

ax2.plot(x,y2  ,"r-")
ax2.set_ylabel("y2")

ax3.plot(x,y3  ,"c-")
ax3.set_ylabel("y3")

ax3.spines["right"].set_position(("axes", 1.2))


plt.show()

結果

ダウンロード (1).png

参考

[Multiple Yaxis With Spines]
(https://matplotlib.org/3.1.0/gallery/ticks_and_spines/multiple_yaxis_with_spines.html)

16
10
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
16
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?