使用 csv 文件中的 matplotlib 在 python 中绘制时间序列图
plotting a timeseries graph in python using matplotlib from a csv file
我有一些以下格式的 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
我想使用列 (Ln
, Dr
, Tg
,Lab
) 作为键和 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
中的所有值而不单独指定每个值。我希望所有时间序列都绘制在一个时间序列上?
我不太确定你到底想做什么,但 np.loadtxt
是去这里的方法。确保为您的文件正确设置分隔符
data = np.loadtxt(fname="test.csv",delimiter=',',skiprows=1)
现在 data
的第 n 列是文件的第 n 列,行也是如此。
您可以按行访问数据:data[n]
或按列访问数据:data[:,n]
我建议使用 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 数据。
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
来绘制时间序列图字段作为时间序列图上的值。
我有以下代码。
#!/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
中的所有值而不单独指定每个值。我希望所有时间序列都绘制在一个时间序列上?
我不太确定你到底想做什么,但 np.loadtxt
是去这里的方法。确保为您的文件正确设置分隔符
data = np.loadtxt(fname="test.csv",delimiter=',',skiprows=1)
现在 data
的第 n 列是文件的第 n 列,行也是如此。
您可以按行访问数据:data[n]
或按列访问数据:data[:,n]
我建议使用 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()