我需要绘制一组不同的图,每个图至少有两个不同的 y 轴。
我设法单独解决了每个任务:
第一:不同情节的集合:
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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句