【问题标题】:How to I make a line graph out of this?如何从中制作折线图?
【发布时间】:2023-03-30 21:27:01
【问题描述】:

我已经导入了 seaborn 并输入了这个:

Bunker2019_Jan_to_Jun.plot(x='2019', y='Total')
Bunker2019_Jan_to_Jun.plot(x='2019', y='MGO')

它显示了两个图表。有什么方法可以显示 2019(1 到 12 月)和 2020(1 到 6 月)的年份吗?

【问题讨论】:

    标签: python dataframe matplotlib seaborn


    【解决方案1】:

    如果您喜欢它们在同一个图上,则需要合并数据框,不太确定您的“2019”列是什么(日期或字符串?),所以下面我尝试创建一个与您类似的 data.frame :

    import seaborn as sns
    import matplotlib. pyplot as plt
    import pandas as pd
    import numpy as np
    
    mths = pd.date_range(start='1/1/2019', periods=12,freq="M").strftime("%b").to_list()
    Bunker2019 = pd.DataFrame({'2019':mths,'Total':np.random.uniform(0,1,12),
                               'MGO':np.random.uniform(0,1,12)})
    Bunker2020 = pd.DataFrame({'2020':mths,'Total':np.random.uniform(0,1,12),
                               'MGO':np.random.uniform(0,1,12)})
    

    添加年份以创建新日期的简单方法:

    Bunker2019['Date'] = '2019_'+ Bunker2019['2019'].astype(str)
    Bunker2020['Date'] = '2020_'+ Bunker2020['2020'].astype(str)
    

    我们concat和melt,设置顺序:

    df = pd.concat([Bunker2019[['Date','Total','MGO']],Bunker2020[['Date','Total','MGO']]])
    df = df.melt(id_vars='Date')
    df['Date'] = pd.Categorical(df['Date'],categories=df['Date'].unique(),ordered=True)
    

    所以现在它是一个长格式,包含 2020 年和 2019 年的信息:

    Date    variable    value
    0   2019_Jan    Total   0.187751
    1   2019_Feb    Total   0.091374
    2   2019_Mar    Total   0.929739
    3   2019_Apr    Total   0.621981
    4   2019_May    Total   0.371236
    5   2019_Jun    Total   0.027078
    6   2019_Jul    Total   0.719046
    7   2019_Aug    Total   0.138531
    

    现在开始绘制:

    plt.figure(figsize=(12,5))
    
    ax = sns.lineplot(data=df,x='Date',y='value',hue='variable')
    sns.scatterplot(data=df,x='Date',y='value',hue='variable',ax=ax,legend=False)
    plt.xticks(rotation=65, horizontalalignment='right')
    plt.show()
    

    【讨论】:

      【解决方案2】:

      我将源 DataFrame 创建为:

            Month    MGO   MFO
      0   2019-01   79.1  85.0
      1   2019-02   69.9  91.2
      2   2019-03   68.9  90.4
      3   2019-04   71.1  87.0
      4   2019-05   75.9  85.6
      5   2019-06   60.9  82.1
      6   2019-07   68.4  75.0
      7   2019-08   75.8  60.7
      8   2019-09   82.0  58.8
      9   2019-10   95.3  56.6
      10  2019-11   90.2  59.7
      11  2019-12   86.5  57.7
      12  2020-01   79.1  50.0
      13  2020-02   88.9  52.2
      14  2020-03   74.9  54.4
      15  2020-04   87.1  51.0
      16  2020-05   92.9  52.6
      17  2020-06  105.9  53.1
      

      (现在 Month 列作为字符串)。 如果您有 2 个单独的源 DataFrame,请将它们连接起来。

      第一个处理步骤是将 Month 列转换为 datetime 键入并将其设置为索引:

      df.Month = pd.to_datetime(df.Month)
      df.set_index('Month', inplace=True)
      

      第一个更直接的创建可能性 图纸是:

      df.plot(style='-x');
      

      对于我得到的数据样本:

      第二种可能性是生成带有平滑线条的图片。 为此,您可以在单个 axex 中绘制两个图:

      • 第一个 - 平滑线,来自重新采样的 DataFrame,带有 插值,但没有标记,因为现在有更多的点,
      • 第二个 - 唯一标记,取自原始DataFrame
      • 两者都具有相同的颜色列表。

      代码如下:

      fig, ax = plt.subplots()
      color = ['blue', 'orange']
      df.resample('D').interpolate('quadratic').plot(ax=ax, color=color)
      df.plot(ax=ax, marker='x', linestyle='None', legend=False, color=color);
      

      这次的结果是:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-15
        • 1970-01-01
        • 1970-01-01
        • 2023-01-10
        • 1970-01-01
        • 2018-06-12
        • 1970-01-01
        相关资源
        最近更新 更多