Matplotlib - 雙軸



在圖形中使用雙 x 軸或 y 軸被認為是有用的。更重要的是,當將具有不同單位的曲線一起繪製時。Matplotlib 使用 twinx 和 twiny 函式支援此功能。

在以下示例中,該圖具有雙 y 軸,一個顯示 exp(x),另一個顯示 log(x) -

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
a1 = fig.add_axes([0,0,1,1])
x = np.arange(1,11)
a1.plot(x,np.exp(x))
a1.set_ylabel('exp')
a2 = a1.twinx()
a2.plot(x, np.log(x),'ro-')
a2.set_ylabel('log')
fig.legend(labels = ('exp','log'),loc='upper left')
plt.show()
Twin Axes
廣告