使用csv文件中的matplotlib在python中绘制时间序列图 [英] plotting a timeseries graph in python using matplotlib from a csv file
本文介绍了使用csv文件中的matplotlib在python中绘制时间序列图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下格式的csv数据.
I have some csv data in the following format.
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
我想使用列(Ln
,Dr
,Tg
,Lab
)作为键并使用0:0n
字段作为时间序列图上的值来绘制时间序列图.
I want to plot a timeseries graph using the columns (Ln
, Dr
, Tg
,Lab
) as the keys and the 0:0n
field as values on a timeseries graph.
我有以下代码.
#!/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
列中的所有值,而无需工业界指定每个值.我希望所有时间序列都绘制在一个时间序列上?
How do I extract all the values in columns 0:0n
without induviduall specifying each one of them. I want all the timeseries to be plotted on a single timeseries?
推荐答案
我建议使用 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()
这篇关于使用csv文件中的matplotlib在python中绘制时间序列图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文