在Python中,如何遍历CSV文件并按代表秒的每列将其打印到boxplot?

2024-04-18 00:28:10 发布

您现在位置: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()

Tags: 文件csv数据import绘图pandas编码df
1条回答
网友
1楼 · 发布于 2024-04-18 00:28:10

试试这个:

axes = df.groupby(df.columns//10, axis=1).boxplot(subplots=True, 
                                           figsize=(12,18))

plt.xlabel('Time (s)')
plt.ylabel('ms')
plt.show()

输出:

enter image description here

如果要设置子时隙的y限制:

for ax in axes.flatten():
    ax.set_ylim(0,100)

相关问题 更多 >

    热门问题