[Python] How to plot multiple data sets from excel file in one plot in matplotlib?

Discussão em 'Python' iniciado por Stack, Dezembro 8, 2024.

  1. Stack

    Stack Membro Participativo

    I would like to learn how to plot multiple data sets taken from excel in one plot. The code below (provided kindly by another user) plots data sets in separate plots. It works great. Now I need to combine data for other purposes to show in one plot.

    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    import openpyxl as xl
    df = pd.read_excel("C:\\temp\\test.xlsx", sheet_name="Sheet2", skiprows=5)
    print(df.columns)
    def grouped(iterable, n):
    return zip(*[iter(iterable)]*n)
    pair_count = len(list(grouped(df.columns,2)))
    pair = 1
    plt.figure(figsize=(10,5))
    for x_col,y_col in grouped(df.columns,2):
    x=np.array(df[x_col].values)
    y=np.array(df[y_col].values)
    plt.subplot(1,pair_count,pair)
    plt.xlim(0.1,15)
    plt.ylim(300,800)
    plt.plot(x,y)
    plt.title(f"{pair}. th plot")
    plt.xlabel(f"{x_col}")
    plt.ylabel(f"{y_col}")
    pair += 1
    plt.show()


    It should look like this: [​IMG] I would really appreciate your help!

    Tried adding fig, ax = plt.subplots() but it created 10 separate graphs.

    Continue reading...

Compartilhe esta Página