如何将 pandas 的每一行绘制为线图

seh*_*h33 6 python matplotlib pandas

我有一个 pandas 数据框,其中列名称是以 1 Hz 为步长的频率,每行是一个参与者id,值是参与者在每个相应频率下的振幅 ^2 值。

我正在尝试绘制数据的时间序列,其中 x 轴是频率,y 轴是幅度^2值,采用“意大利面条图”样式,即为我的数据帧的每一行绘制一条线:

这是我的数据的一小段:

data = [['1', 9.45e-09, 9.85e-09, 8.33e-09, 6.06e-09, 4.80e-09, 4.08e-09],
        ['2', 1.30e-08, 1.25e-08, 8.99e-09, 6.25e-09, 4.44e-09, 3.45e-09],
        ['3', 9.32e-09, 8.60e-09, 5.67e-09, 3.68e-09, 2.53e-09, 1.75e-09]]

fft_df = df = pd.DataFrame(data, columns = ['id', '1','2','3','4','5','6']).set_index('id')

# display(fft_df)
               1             2             3             4             5             6
id                                                                                    
1   9.450000e-09  9.850000e-09  8.330000e-09  6.060000e-09  4.800000e-09  4.080000e-09
2   1.300000e-08  1.250000e-08  8.990000e-09  6.250000e-09  4.440000e-09  3.450000e-09
3   9.320000e-09  8.600000e-09  5.670000e-09  3.680000e-09  2.530000e-09  1.750000e-09
Run Code Online (Sandbox Code Playgroud)

使用 matplotlib,如果我使用fft_df列名称作为 x 参数,并将fft_df列平均值作为 y 参数,matplotlib 将返回一个线图。但是,如果我从 y 输入中删除 ,.mean()它将返回错误。我似乎无法弄清楚如何为中的每一行绘制一条线fft_df

plt.figure(figsize=(10, 10))
plt.ylabel('Absolute Power (log)',fontsize=12)
plt.xlabel('Frequencies',fontsize=12)
plt.plot(fft_df.columns,fft_df.mean())
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Tre*_*ney 7

  • 如另一个答案所示,没有必要使用 pandas,但是,数据据说已经在 pandas 中,并且使用 pandas 可以更轻松地绘制并执行进一步的分析,例如提取摘要统计数据。大多数函数都是向量化的,并且numpy是依赖项。
    • df.T.describe()或者df.describe()
  • 转置数据框.T
  • 用 绘图pandas.DataFrame.plot,返回一个axes
  • 测试于python 3.8.11,pandas 1.3.1matplotlib 3.4.2
import pandas as pd
import matplotlib.pyplot as plt

# create dataframe
data = [['1', 9.45e-09, 9.85e-09, 8.33e-09, 6.06e-09, 4.80e-09, 4.08e-09], ['2', 1.30e-08, 1.25e-08, 8.99e-09, 6.25e-09, 4.44e-09, 3.45e-09], ['3', 9.32e-09, 8.60e-09, 5.67e-09, 3.68e-09, 2.53e-09, 1.75e-09]]
df = pd.DataFrame(data, columns = ['id', '1', '2', '3', '4', '5', '6']).set_index('id')

# transpose and plot
ax = df.T.plot(figsize=(7, 6))
ax.set_ylabel('Absolute Power (log)', fontsize=12)
ax.set_xlabel('Frequencies', fontsize=12)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


小智 1

我认为,最简单的解决方案是转置 DataFrame,然后使用 pandas 的绘图方法。这在某种程度上是基于这个答案。代码如下所示:

import pandas as pd
import matplotlib.pyplot as plt

data = [['1', 9.45e-09, 9.85e-09, 8.33e-09, 6.06e-09, 4.80e-09, 4.08e-09],
        ['2', 1.30e-08, 1.25e-08, 8.99e-09, 6.25e-09, 4.44e-09, 3.45e-09],
        ['3', 9.32e-09, 8.60e-09, 5.67e-09, 3.68e-09, 2.53e-09, 1.75e-09]]

df = pd.DataFrame(data, columns = ['id', '1','2','3','4','5','6']).set_index('id')

# create figure and axis
fig, ax = plt.subplots(figsize=(10, 10))

# setting the axis' labels
ax.set_ylabel('Absolute Power (log)',fontsize=12)
ax.set_xlabel('Frequencies',fontsize=12)

# transposing (switchung rows and columns) of DataFrame df and
# plot a line for each column on the axis ax, which was created previously
df.T.plot(ax=ax)
Run Code Online (Sandbox Code Playgroud)

结果如下: 阴谋