【问题标题】:How can I iterate through a CSV file and plot it to a boxplot by each column representing a second in Python?如何遍历 CSV 文件并将其绘制到箱线图中,每列代表 Python 中的一秒?
【发布时间】:2019-10-04 02:27:36
【问题描述】:

假设我有一个这样的 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()

【问题讨论】:

  • 你想要每一秒一个单独的图,还是一个图上的所有秒数?
  • @MasonCaiby 每张图 10,因为有很多数据集。所以每 10 秒在不同的图表上

标签: python pandas csv matplotlib boxplot


【解决方案1】:

试试这个:

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)

【讨论】:

  • 这样我得到“ValueError: Grouper and axis must be the same length”
  • 上线:df.groupby(df.columns // 10).boxplot(ax = ax)
  • 太棒了,谢谢。两个问题。 “轴 = 1”有什么作用?你知道我如何限制图上的 y 轴吗?我试过 plt.ylim=(0, 100) 但没用
  • 这不起作用,我做错了什么?轴 = df.groupby(ylim = (0, 100))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-28
  • 2017-10-02
  • 2019-01-17
  • 2019-11-24
  • 1970-01-01
  • 1970-01-01
  • 2018-02-07
相关资源
最近更新 更多