Укладка нескольких участков на оси 2Y - PullRequest
0 голосов
/ 11 апреля 2019

Я пытаюсь построить несколько графиков на графике 2Y.

У меня есть следующий код:

  • Имеет список файлов для получения данных;
  • Получает компоненты x и y данных для построения по оси y 1 и оси y 2;
  • Отображает данные.

Когда цикл повторяется, он строит графики на разныхцифры.Я хотел бы получить все участки на одной фигуре.Может ли кто-нибудь помочь мне в этом?

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()








1 Ответ

1 голос
/ 11 апреля 2019

Вы должны создать экземпляр фигуры вне цикла, а затем добавить субплоты во время итерации.Таким образом, у вас будет одна фигура и все графики внутри нее.

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()
...