LoginSignup
0
0

More than 1 year has passed since last update.

両軸グラフ

Posted at

両軸グラフの作り方

以下のコードで作れます。twinx()を使って両軸グラフにしています。

import matplotlib.pyplot as plt
import numpy as np


#配列の準備
x = np.linspace(0,2*np.pi,1000)
y = x**2
z = np.sin(x)


#図の準備
fig = plt.figure(figsize=(12,8))

#左軸の作図
ax1 = fig.add_subplot(111)
ln1=ax1.plot(x,y ,label = 'y',c='b')

#右軸の作図
ax2 = ax1.twinx()
ln2=ax2.plot(x,z, label = 'z',c='r')

#凡例の設定
h1, l1 = ax1.get_legend_handles_labels()
h2, l2 = ax2.get_legend_handles_labels()
ax1.legend(h1+h2, l1+l2, loc='lower center', bbox_to_anchor=(0.5,1), ncol=2)

#軸ラベルの指定
ax1.set_xlabel('time (s)')
ax1.set_ylabel('$Y = X^2$')
ax2.set_ylabel('$Z = sin(x)$')
ax1.grid(True)
plt.show()
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