如何遍历 CSV 文件并将其绘制成箱线图,每列代表 Python 中的一秒?
How can I iterate through a CSV file and plot it to a boxplot by each column representing a second in Python?
假设我有一个这样的 csv 文件:
20 30 33 54 12 56
90 54 66 12 88 11
33 22 63 86 12 65
11 44 65 34 23 26
我想创建一个箱线图,其中每一列都是一秒钟,也是 x 轴。 y 上的实际数据。因此,20、90、33、11 将在 1 秒内出现在一个地块上,而 30、54、22、44 将在 2 秒内出现,依此类推。另外,csv 文件的数据比这个多,我不确定有多少数据集,所以我不能硬编码任何东西。
这是我目前拥有的:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('/user/Desktop/test.csv', header = None)
fig = plt.figure()
ax = fig.add_subplot()
plt.xlabel('Time (s)')
plt.ylabel('ms')
df.boxplot()
plt.show()
试试这个:
axes = df.groupby(df.columns//10, axis=1).boxplot(subplots=True,
figsize=(12,18))
plt.xlabel('Time (s)')
plt.ylabel('ms')
plt.show()
输出:
如果要设置 y
子图的限制:
for ax in axes.flatten():
ax.set_ylim(0,100)
假设我有一个这样的 csv 文件:
20 30 33 54 12 56
90 54 66 12 88 11
33 22 63 86 12 65
11 44 65 34 23 26
我想创建一个箱线图,其中每一列都是一秒钟,也是 x 轴。 y 上的实际数据。因此,20、90、33、11 将在 1 秒内出现在一个地块上,而 30、54、22、44 将在 2 秒内出现,依此类推。另外,csv 文件的数据比这个多,我不确定有多少数据集,所以我不能硬编码任何东西。
这是我目前拥有的:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('/user/Desktop/test.csv', header = None)
fig = plt.figure()
ax = fig.add_subplot()
plt.xlabel('Time (s)')
plt.ylabel('ms')
df.boxplot()
plt.show()
试试这个:
axes = df.groupby(df.columns//10, axis=1).boxplot(subplots=True,
figsize=(12,18))
plt.xlabel('Time (s)')
plt.ylabel('ms')
plt.show()
输出:
如果要设置 y
子图的限制:
for ax in axes.flatten():
ax.set_ylim(0,100)