Matplotlib 在2Y轴上叠加多个绘图

Matplotlib 在2Y轴上叠加多个绘图,matplotlib,plot,spyder,Matplotlib,Plot,Spyder,我试图在2Y图中绘制多个图 我有以下代码: 有一个文件列表来获取一些数据; 获取要在y轴1和y轴2中绘制的数据的x和y分量; 绘制数据。 循环迭代时,它会绘制在不同的图形上。我希望所有的图都在同一个图中。 有人能帮我吗 import numpy as np import matplotlib.pyplot as plt import pandas as pd file=[list of paths] for i in files: # Loads Data from an excel fi

我试图在2Y图中绘制多个图

我有以下代码:

有一个文件列表来获取一些数据; 获取要在y轴1和y轴2中绘制的数据的x和y分量; 绘制数据。 循环迭代时,它会绘制在不同的图形上。我希望所有的图都在同一个图中。 有人能帮我吗

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
file=[list of paths]

for i in files:

 # Loads Data from an excel file
    data = pd.read_excel(files[i],sheet_name='Results',dtype=float)

 # Gets x and y data from the loaded files
    x=data.iloc[:,-3]
    y1=data.iloc[:,-2]
    y12=data.iloc[:,-1]
    y2=data.iloc[:,3]

    fig1=plt.figure()
    ax1 = fig1.add_subplot(111)    
    ax1.set_xlabel=('x')
    ax1.set_ylabel=('y')

    ax1.plot(x,y1)
    ax1.semilogy(x,y12)

    ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis
    ax2.plot(x,y2)

    fig1.tight_layout()  

    plt.show()









您应该在循环外实例化图形,然后在迭代时添加子图。这样,您将拥有一个图形和其中的所有绘图

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
files=[list of paths]

fig1=plt.figure()

for i in files:

 # Loads Data from an excel file
    data = pd.read_excel(files[i],sheet_name='Results',dtype=float)

 # Gets x and y data from the loaded files
    x=data.iloc[:,-3]
    y1=data.iloc[:,-2]
    y12=data.iloc[:,-1]
    y2=data.iloc[:,3]

    ax1 = fig1.add_subplot(111)    
    ax1.set_xlabel=('x')
    ax1.set_ylabel=('y')

    ax1.plot(x,y1)
    ax1.semilogy(x,y12)

    ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis
    ax2.plot(x,y2)

    fig1.tight_layout()  

    plt.show()

谢谢你的帮助!