【问题标题】:How to plot data from list?如何从列表中绘制数据?
【发布时间】:2021-09-27 09:08:12
【问题描述】:

如何使用 for 循环根据其索引绘制列表中的数据?

(我需要写点东西,因为我的帖子主要是代码,我还有点添加更多细节......)

这是代码示例:

import pandas as pd 
import os
import matplotlib.pyplot as plt
import numpy as np

path = r'D:\Experiments\20210924_SureliteOPO_beampointing\all_log'
filesnames = os.listdir(path)
filesnames = [f for f in filesnames if (f.startswith("2") and f.lower().endswith(".csv"))]

dfs = list() # a list of dataframes
for csvfile in filesnames:
    fpath = path + '/' + csvfile
    df = pd.read_csv(fpath, skiprows=26, skipfooter=5)
    dfs.append(df)
print(dfs)

输出如下:

[    Powermeter1 Start
0            0.001864
1            0.001756
2            0.001818
3            0.001837
4            0.001932
..                ...
95           0.001697
96           0.001950
97           0.001871
98           0.001757
99           0.001849

[100 rows x 1 columns],     Powermeter1 Start
0            0.001771
1            0.001863
2            0.001796
3            0.001885
4            0.001746
..                ...
95           0.001827
96           0.001678
97           0.001813
98           0.001776
99           0.001637

[100 rows x 1 columns]], ...
...
...

问候,

卡尔

【问题讨论】:

    标签: list for-loop plot


    【解决方案1】:

    找到了:

    for i in range(len(dfs)):    
        plt.plot(df.index,dfs[i])
    

    【讨论】:

      【解决方案2】:

      enumerate() 设计用于当您需要遍历可迭代的元素时 访问它们的索引:

      for i, elem in enumerate(dfs):
        ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-29
        • 1970-01-01
        • 1970-01-01
        • 2020-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多