python - 使用 csv 文件中的 matplotlib 在 python 中绘制时间序列图

标签 python numpy matplotlib time-series data-analysis

我有一些以下格式的 csv 数据。

Ln    Dr    Tag Lab    0:01    0:02    0:03    0:04    0:05    0:06    0:07    0:08   0:09
L0   St     vT  4R       0       0       0       0       0      0        0       0      0
L2   Tx     st  4R       8       8       8       8       8      8        8       8      8
L2   Tx     ss  4R       1       1       9       6       1      0        0       6      7

我想使用列(LnDrTgLab)绘制时间序列图作为时间序列图上的键和 0:0n 字段作为值。

我有以下代码。

#!/usr/bin/env python

import matplotlib.pyplot as plt
import datetime
import numpy as np
import csv
import sys


with open("test.csv", 'r', newline='') as fin:
    reader = csv.DictReader(fin)
    for row in reader:
          key = (row['Ln'], row['Dr'], row['Tg'],row['Lab'])
          #code to extract the values and plot a timeseries.

如何提取列 0:0n 中的所有值,而不单独指定其中的每一个值。我希望将所有时间序列绘制在单个时间序列上?

最佳答案

我建议使用pandas :

import pandas as pd
a=pd.read_csv('yourfile.txt',delim_whitespace=True)
for x in a.iterrows():
    x[1][4:].plot(label=str(x[1][0])+str(x[1][1])+str(x[1][2])+str(x[1][3]))

plt.ylim(-1,10)
plt.legend()

enter image description here

关于python - 使用 csv 文件中的 matplotlib 在 python 中绘制时间序列图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33681911/

相关文章:

python - 比较两个字典并检查有多少(键,值)对是相等的

python - 如何修复 pylint E1134 : Non-mapping value X is used in a mapping context (not-a-mapping)

python - 在 Python 中比较矩阵并存储结果

python - 添加两个带有 2D 掩码的 3D numpy 数组

python:散点图对数刻度

python - Matplotlib 3d 散点图缺少颜色图

matplotlib - 颜色之间带有刻度标签的离散颜色条

python - Nosetests 给出 "Improperly Configured"错误

python - numpy 中的逻辑索引抛出异常

python - 在python中将单值字典转换为多值字典