问题描述
我有几个电子表格包含以逗号分隔(.csv)文件格式保存的数据,格式如下:第一行包含列标签作为字符串('Time','Parameter_1'...)。第一列数据是时间,每个后续列包含相应的参数数据,作为浮点数或整数。
I have several spreadsheets containing data saved as comma delimited (.csv) files in the following format: The first row contains column labels as strings ('Time', 'Parameter_1'...). The first column of data is Time and each subsequent column contains the corresponding parameter data, as a float or integer.
我想在同一个图上绘制每个参数与时间的关系,其中参数图例直接从.csv文件的第一行派生。
I want to plot each parameter against Time on the same plot, with parameter legends which are derived directly from the first row of the .csv file.
我的电子表格具有不同数量的(列的)参数对时间绘制;所以我想找到一个通用的解决方案,它也将直接从.csv文件导出列数。
My spreadsheets have different numbers of (columns of) parameters to be plotted against Time; so I'd like to find a generic solution which will also derive the number of columns directly from the .csv file.
附加的最小工作示例显示了我试图实现使用np.loadtxt(减去图例);但我找不到一种方法从.csv文件导入列标签以使用此方法来制作图例。
The attached minimal working example shows what I'm trying to achieve using np.loadtxt (minus the legend); but I can't find a way to import the column labels from the .csv file to make the legends using this approach.
np.genfromtext提供更多功能,但我不熟悉这一点,并且正在努力找到一种使用它来做上述事情的方法。
np.genfromtext offers more functionality, but I'm not familiar with this and am struggling to find a way of using it to do the above.
以.csv文件格式绘制此样式中的数据必须是常见问题,但我无法在网上找到解决方案。我非常感谢你的帮助&建议。
Plotting data in this style from .csv files must be a common problem, but I've been unable to find a solution on the web. I'd be very grateful for your help & suggestions.
非常感谢
"""
Example data: Data.csv:
Time,Parameter_1,Parameter_2,Parameter_3
0,10,0,10
1,20,30,10
2,40,20,20
3,20,10,30
"""
import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt('Data.csv', skiprows=1, delimiter=',') # skip the column labels
cols = data.shape[1] # get the number of columns in the array
for n in range (1,cols):
plt.plot(data[:,0],data[:,n]) # plot each parameter against time
plt.xlabel('Time',fontsize=14)
plt.ylabel('Parameter values',fontsize=14)
plt.show()
推荐答案
函数 numpy.genfromtxt
更适用于缺少值而不是您尝试做。你可以做的只是打开该文件,然后交给 numpy.loadtxt
并读取第一行。然后你甚至不需要跳过它。以下是您在上面所阅读的标签和传说的编辑版本:
The function numpy.genfromtxt
is more for broken tables with missing values rather than what you're trying to do. What you can do is simply open the file before handing it to numpy.loadtxt
and read the first line. Then you don't even need to skip it. Here is an edited version of what you have here above that reads the labels and makes the legend:
"""
Example data: Data.csv:
Time,Parameter_1,Parameter_2,Parameter_3
0,10,0,10
1,20,30,10
2,40,20,20
3,20,10,30
"""
import numpy as np
import matplotlib.pyplot as plt
#open the file
with open('Data.csv') as f:
#read the names of the colums first
names = f.readline().strip().split(',')
#np.loadtxt can also handle already open files
data = np.loadtxt(f, delimiter=',') # no skip needed anymore
cols = data.shape[1]
for n in range (1,cols):
#labels go in here
plt.plot(data[:,0],data[:,n],label=names[n])
plt.xlabel('Time',fontsize=14)
plt.ylabel('Parameter values',fontsize=14)
#And finally the legend is made
plt.legend()
plt.show()
这篇关于Matplotlib:直接从.csv导入和绘制多个时间序列与图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!