【问题标题】:How to plot mulitple coulmns of a dataframe in matplotlib如何在 matplotlib 中绘制数据框的多列
【发布时间】:2021-03-31 18:36:33
【问题描述】:

我已经多次问过这个问题,但不知何故我的问题根本没有意见。我希望这次我的问题结构正确。 我有一个如下的数据框:


  indeces      Zeitstempel     Ergebnis
0   382    16.04.2020 16:12:07  PASS
1   383    16.04.2020 16:13:07  PASS
2   392    16.04.2020 16:13:20  FAIL
3   382    16.04.2020 16:13:22  PASS
4   383    16.04.2020 16:14:22  PASS

数据框有三列。我正在尝试构建一个图表,其中 x 轴是 Zeitstempel(日期/时间的组合),y 轴是 indeces,我还想指定 Ergebnis 列的值(颜色编码绿色表示通过,红色表示失败和灰色表示阻塞) 该图应表示在什么 Zeitstempel 值处哪些 indeces 值通过、失败或阻塞。实际的数据框有 1172 行 × 3 列的值,但在上面我只提到了几个。 我正在尝试的代码如下,但不知何故我无法弄清楚如何根据需要绘制所有 3 个。

# times = pd.date_range('2020-04-16 16:12 AM', '2020-04-16 11:00 PM', freq='1H')
# df["Zeitstempel"] = pd.to_datetime(df['Zeitstempel'],errors='coerce')
groups = df.groupby("Ergebnis")
marker = ["x","x","*"]
size = [15,5,2]
i = 0
#plt.subplots(311)
for name,group in groups:
  #plt.subplot(3,1,i+1)

  plt.plot(df["Zeitstempel"][:2],group["indeces"][:2],marker=marker[i],markersize = size[i],label=name)
  i = i + 1

plt.legend()

这给了我一个图表,但它是准确的。我提到 [:2] 的原因是因为它不断抛出尺寸错误。这是一个折线图。我也尝试了条形图,但没有运气。 谁能建议我在这里实际需要做什么才能得到实际的图表。我被困在这几天,无法弄清楚必须做什么。我也尝试了 numpy 数组,但因为这是数据框 我不能使用 numpy 数组。

【问题讨论】:

    标签: python-3.x pandas dataframe matplotlib


    【解决方案1】:

    您不应在循环中使用未分组的数据框 df。试试这个:

    dict_color = {
        'PASS': 'green',
        'FAIL': 'red',
        'BLOCKED': 'grey'
    }
    dict_marker = {
        'PASS': "x",
        'FAIL': "x",
        'BLOCKED': "*"
    }
    for ergebnis, df_small in df.groupby(['Ergebnis']):
        plt.scatter(
            x=df_small['Zeitstempel'], 
            y=df_small['indeces'],
            color=dict_color[ergebnis],
            marker=dict_marker[ergebnis],
            label=ergebnis
        )
    plt.legend()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      • 2022-01-20
      • 2017-08-29
      • 2017-08-04
      • 2020-07-28
      相关资源
      最近更新 更多