【问题标题】:How to plot multiple dataframes into a single figure如何将多个数据框绘制成一个图形
【发布时间】:2020-09-02 20:16:03
【问题描述】:

我在同一张图表上绘制多个图表时遇到问题,如下所示:

plt.title("Food distribution across countries ")

df1.groupby('Date').size().plot()
plt.xticks(rotation=90)

df2.groupby('Date').size().plot()
plt.xticks(rotation=90)

df3.groupby('Date').size().plot()
plt.xticks(rotation=90)

数据集的一个例子是

df1
        Date                                              
    0     03/26/2020  bread   
    1     03/21/2020  bread   
    2     03/14/2020  prawns   
    3     03/13/2020  eggs   
    4     01/20/2020  prawns   
    ...          ...                                                  
    1033  04/08/2020  chicken   
    1034  04/08/2020  ham  
    1035  04/08/2020  ham   
    1036  04/08/2020  ham   
    1037  04/08/2020  honey

df2
        Date                                              
    0     03/26/2020  bread   
    1     03/21/2020  honey   
    2     03/14/2020  milk   
    3     03/13/2020  eggs   
    4     01/20/2020  prawns   
    ...          ...                                                  
    1033  04/08/2020  chocolate   
    1034  04/08/2020  chocolate  
    1035  04/08/2020  sausage   
    1036  04/08/2020  ham   
    1037  04/08/2020  butter


df3
        Date                                             
    0     03/26/2020  bread   
    1     03/21/2020  honey   
    2     03/14/2020  milk   
    3     03/13/2020  milk   
    4     01/20/2020  beer   
    ...          ...                                                   
    1033  04/08/2020  chocolate   
    1034  04/08/2020  sausage  
    1035  04/08/2020  ham   
    1036  04/08/2020  ham   
    1037  04/08/2020  honey

我想在同一个图中绘制三个折线图,以显示数据在时间上的重叠。

有什么方法(甚至与我一直在尝试的不同)只有一个图表吗?

【问题讨论】:

    标签: python pandas matplotlib plot


    【解决方案1】:
    fig, ax = plt.subplots()
    
    df1.groupby('Date').size().plot(ax=ax, label='df1')
    df2.groupby('Date').size().plot(ax=ax, label='df2')
    df3.groupby('Date').size().plot(ax=ax, label='df3')
    plt.legend()
    

    示例

    • 此示例将 10 个单独的数据框绘制到一个图上。
    • 数据帧位于dictplanets_dict
    • 数据帧也可以放在list中,可以迭代。
    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    # load test data as a pandas dataframe
    planets = sns.load_dataset('planets')
    
    # display(planets.head(3))
                method  number  orbital_period  mass  distance  year
    0  Radial Velocity       1         269.300  7.10     77.40  2006
    1  Radial Velocity       1         874.774  2.21     56.95  2008
    2  Radial Velocity       1         763.000  2.60     19.84  2011
    
    # get unique methods
    methods = planets.method.unique()
    
    # use a dict comprehension to create 10 separate dataframes, 1 for each method
    planets_dict = {k: planets[planets.method == k] for k in methods}
    
    # create a color map so each dataframe will be a different color
    colors = sns.color_palette('husl', n_colors=len(methods))  # get a number of colors
    cmap = dict(zip(methods, colors)) 
    
    # create fig and axes
    fig, ax = plt.subplots(figsize=(10, 10))
    
    # iterate through each of the dataframes and add it to the same plt
    for k, v in planets_dict.items():
        v.plot(kind='scatter', x='distance', y='orbital_period', ax=ax, label=k, color=cmap[k])
    
    # style formatting
    plt.xscale('log')
    plt.yscale('log')
    plt.ylabel('Orbital Period (Earth Days)')
    plt.xlabel('Light Years from Earth')
    plt.legend(title='Discovery Method', bbox_to_anchor=(1.05, 1), loc='upper left')
    

    【讨论】:

      【解决方案2】:

      你可以创建一个单独的 df 并绘制它

      timeline_df = pd.concat(
          [df1.groupby('Date').size(), df2...],
          axis=1)
      timeline_df.columns = ['A', 'B', 'C']
      timeline_df.plot()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-01
        • 1970-01-01
        • 2023-01-20
        • 1970-01-01
        • 2016-05-16
        • 2021-02-13
        • 2020-01-04
        • 2021-03-09
        相关资源
        最近更新 更多