【问题标题】:How can I plot multiple dataframes on the same figure from an excel sheet using pandas?如何使用 pandas 在 Excel 工作表中的同一图形上绘制多个数据框?
【发布时间】:2020-02-11 16:22:29
【问题描述】:

我试图在同一个图上绘制多个 y 值(力)和 x 值(位移)。当所有图的 x 值都是恒定的但随着它的变化而变化时,我通常会发现它很容易,我还没有找到解决它的方法。拜托,我需要一些帮助来解决这个问题。

我附上了我要绘制的数据的图片。这三个样本具有不同的 x 值和 y 值。

https://imgur.com/gallery/ckYAJVO

【问题讨论】:

    标签: python excel pandas matplotlib


    【解决方案1】:

    解决方案

    让我们假设您有三个数据框:df0df1df2。此外,让我们假设在每个数据框中都有两列:xy。如果我们列出这些数据框dfs = [df0, df1, df2],那么您可以将它们全部绘制在同一个图上,如下所示。

    这里的解决方案分为两部分:

    • 制作虚拟数据(仅在复制一些虚拟数据时需要)。
    • 绘制数据帧(您将需要使用此代码)。
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    %matplotlib inline
    %config InlineBackend.figure_format = 'svg' # 'svg', 'retina'
    plt.style.use('seaborn-white')
    
    # Make Dummy Data
    N = [100, 125, 150]
    P = [[3, 2.2], [5, 3.7], [2, 4]]
    dfs = list()
    for n, p in zip(N, P):
        x = np.arange(n)
        y = p[0]*x**p[1] + np.average(p)
        df = pd.DataFrame({'x': x, 'y': y})
        dfs.append(df.copy())
    
    # Make Plots
    #dfs = [df1, df2, df3]
    for i,df in enumerate(dfs):
        plt.plot(df['x'], df['y'], 
                 linestyle='--', 
                 alpha=0.8, 
                 label='df{}'.format(i))
    
    plt.legend(loc='best')
    plt.show()
    

    输出

    【讨论】:

      猜你喜欢
      • 2016-10-21
      • 2021-11-25
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      • 2014-03-25
      • 2021-01-26
      相关资源
      最近更新 更多