从具有 pandas 或类似内容的单个长格式文件创建具有多个系列的多个图表

Create multiple charts, with multiple series, from single long format file with pandas or similar

我正在尝试将 python 和 pandas 用于此数据集:

LocCode Sampled_Date-Time ChemName Conc_num
Well 1 2021-03-18 Sulfate 660
Well 1 2021-06-23 Sulfate 780
Well 1 2021-09-14 Sulfate 920
Well 1 2021-12-30 Sulfate 920
Well 1 2021-03-18 Chloride 158
Well 1 2021-06-23 Chloride 194
Well 1 2021-09-14 Chloride 240
Well 1 2021-12-30 Chloride 168
Well 2 2021-03-18 Sulfate 990
Well 2 2021-06-23 Sulfate 1170
Well 2 2021-09-14 Sulfate 1380
Well 2 2021-12-30 Sulfate 1380
Well 2 2021-03-18 Chloride 237
Well 2 2021-06-23 Chloride 291
Well 2 2021-09-14 Chloride 360
Well 2 2021-12-30 Chloride 252

要创建多个图表,如下所示:

Chart 1 - Sulfate Chart 2 - Chloride

所以我有:

我一直在尝试学习 python 和 pandas 模块来实现这个结果,但我被卡住了。不幸的是,我在编写任何代码方面一无所获,因为到目前为止我看过或读过的所有内容都是在单个索引上以旋转格式处理输入数据。

到目前为止,我的想法是代码需要类似于:

  1. 创建将 ChemName 和 LocCode 分组的索引
  2. 迭代此索引,创建 x 值 = Conc_num 和 y 值 = Sampled_Date-Time
  3. 的图

任何 advice/guidance/suggestions 都将不胜感激,或者指向显示如何执行此操作的教程的指针也很棒。

谢谢,

为了让您深入了解,您可以尝试以下代码:

import pandas as pd
import matplotlib.pyplot as plt
plt.style.use("ggplot")
df = pd.read_csv("test.txt", sep="\t") # You do not need this line. Just use your dataframe variable without implementing this line.
df["Sampled_Date-Time"] = pd.to_datetime(data["Sampled_Date-Time"])
fig, ax =plt.subplots(len(data["ChemName"].unique()), 1, figsize=(12,8))
for index,ChemName in enumerate(data["ChemName"].unique()):
  tempDf = df[df["ChemName"] == ChemName]
  for LocCode in tempDf["LocCode"].unique():
    tempDf2 = tempDf[tempDf["LocCode"] == LocCode]
    ax[index].plot(tempDf2["Sampled_Date-Time"], tempDf2["Conc_num"])
    ax[index].scatter(tempDf2["Sampled_Date-Time"], tempDf2["Conc_num"])
  ax[index].set_title(ChemName)
  ax[index].set_xlabel("date")
  ax[index].set_ylabel("Concentration")
fig.subplots_adjust(hspace=0.5)

输出