首页 > 解决方案 > 如何在同一标签下绘制整个 Pandas DataFrame?

问题描述

我正在尝试编写一个带有 2 个参数的绘图函数 - 一个 DataFrame 和一个 label (df1 and label1)。如果我有更多数据组要绘制,我的函数还有可选参数来添加更多 DataFrame:

def REE_GroupPlot(df1, label1,
                  df2=None, label2=None, 
                  df3=None, label3=None, 
                  df4=None, label4=None,
                  df5=None, label5=None):

我正在尝试将单个 DataFrame 中的所有列绘制在相同的键标签和相同的颜色下,但我不太确定如何。我已经调查过了pd.groupby,但我似乎无法让它做我想做的事。

我将不胜感激任何建议 - 谢谢!

标签: pythonpandasplot

解决方案


如果你传入一个数据框列表,比如

def REE_Group_Plot(dfs, label):
      # combine all dataframes into 1
      all_dfs = pd.concat(dfs)

     # the rest of the code to plot the graph



REE_Group_Plot(dfs=[df, df2, df3, df4], label = “ABC”)

推荐阅读