Pandas 在单个图形上打印多个按分组的列

Pandas 在单个图形上打印多个按分组的列,pandas,matplotlib,plot,group-by,seaborn,Pandas,Matplotlib,Plot,Group By,Seaborn,对于上述熊猫数据帧pd我想根据以下条件进行绘图: 单线图 对于每个算法,x轴=num\u incress和y轴=[“总流”,“成功流”,“丢弃流”]。因此,对于每个算法,绘图上必须有3条线 y轴的标签必须是算法名称+列,例如A-总流量,B-总流量等 我尝试了matplotlib的groupby,但后来我得到了多个组,每个组只能绘制一个图。不是一个绘图上的所有线条。我也尝试了seaborn,但基于groupby也无法实现 如有任何帮助,我们将不胜感激。创建您的数据帧,请确保使用按算法和数值入口

对于上述熊猫数据帧
pd
我想根据以下条件进行绘图:

  • 单线图
  • 对于每个算法,x轴=
    num\u incress
    和y轴=
    [“总流”,“成功流”,“丢弃流”]
    。因此,对于每个算法,绘图上必须有3条线
  • y轴的标签必须是算法名称+列,例如
    A-总流量
    B-总流量
我尝试了
matplotlib
groupby
,但后来我得到了多个组,每个组只能绘制一个图。不是一个绘图上的所有线条。我也尝试了
seaborn
,但基于
groupby
也无法实现


如有任何帮助,我们将不胜感激。

创建您的数据帧,请确保使用按算法和数值入口排序

import pandas as pd

algorithm = ['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B']
num_ingress = [4, 2, 1, 3, 5, 4, 2, 1, 3, 5]
total_flow = [8000, 4000, 2000, 6000, 10000, 8000, 4000, 2000, 6000, 10000]
successful_flows = [5985, 3994, 1997, 5991, 1994, 5975, 3988, 1996, 5974, 5087]
dropped_flows = [2000, 0, 0, 0, 7991, 2005, 0, 0, 9, 4889]

df = pd.DataFrame({'algorithm': algorithm,
      'num_ingress': num_ingress,
      'total_flow': total_flow,
      'successful_flows': successful_flows,
      'dropped_flows': dropped_flows
     })

df.sort_values(['algorithm', 'num_ingress'], inplace=True)
df
然后策划

import matplotlib.pyplot as plt
import numpy as np

for i, algorithm in enumerate(df.groupby('algorithm')):
    algorithm_df = pd.DataFrame(algorithm[1])
    plt.subplot(1, 2, i+1)
    plt.plot(algorithm_df['num_ingress'], algorithm_df[['total_flow', 'successful_flows', 'dropped_flows']])
    plt.title("Algorithm {}".format(algorithm_df['algorithm'].values[0]))

plt.tight_layout()
plt.show()