matplotlib pyplot - 如何组合多个 y 轴和多个图

matplotlib pyplot - how to combine multiple y-axis and multiple plots

我需要绘制一组不同的图,每个图至少有两个不同的 y 轴。

我设法单独解决了每个任务:

1st:不同地块的集合:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

a1 = np.random.randint(0,10,(6,2))
a2 = np.random.randint(0,10,(6,2)) * 10
df = pd.DataFrame(np.hstack([a1,a2]), columns = list('abcd'))

plt.subplot(1,2,1)
plt.plot(df.index,df.a,'-b')
plt.plot(df.index,df.c,'-g')

plt.subplot(1,2,2)
plt.plot(df.index,df.b,'-b')
plt.plot(df.index,df.d,'-g')

第二:绘制至少两个不同的 y 轴:

fig, ax = plt.subplots()
ax2 = ax.twinx()

ax.plot(df.index,df.a,'-b')
ax2.plot(df.index,df.c,'-g')

但是我将这两者结合起来的所有尝试都失败了。有人有解决办法吗?

为每个子图设置两个轴。

ax0 = plt.subplot(1,2,1)
ax1 = ax0.twinx()
ax2 = plt.subplot(1,2,2)
ax3 = ax2.twinx()

完整代码

fig = plt.figure()
fig.subplots_adjust(wspace=0.3)
ax0 = plt.subplot(1,2,1)
ax1 = ax0.twinx()
ax0.plot(df.index,df.a,'-b')
ax1.plot(df.index,df.c,'-g')

ax2 = plt.subplot(1,2,2)
ax3 = ax2.twinx()
ax2.plot(df.index,df.b,'-b')
ax3.plot(df.index,df.d,'-g')

plt.show()