简体   繁体   English

CSV中的Python Matplotlib绘图

[英]Python Matplotlib Plotting from csv

I am able to plot from a simple csv like this using matplot: 我可以使用matplot从像这样的简单csv进行绘制:

1  2
2  4
3  8
4  16
5  32
.
.

The values are seperated by tab. 这些值由制表符分隔。 Now I need to read the data from a csv which looks like this: 现在,我需要从csv中读取数据,如下所示:

    #  Name Test Number1                                      \\Name of the csv
#Sometimes there is a comment which has one line
# or even more

    Category1  Number2  Test  Temperature  Voltage            \\Labels for the plot
    # [1]  [1/min]  [s]  [°C]  [mV]                           \\Units
    MinMax  2.3  5  9  48  22                                 \\Data starts here
    MinMax  9.87  6.01  8  9  3
    MinMax  1  2  3  4  5
    MinMax  99.52  5  8  6.66  0

How can I get the data from my csv and the Labels for the plots? 我如何从csv和图的标签中获取数据? For example if I want to plot Test and Temperature? 例如,如果要绘制测试和温度? Thereis a huge amount of rows and columns. 有大量的行和列。

Thank you! 谢谢!

import csv

with open('path\to\sample.txt', 'r') as csvfile:

csvreader = csv.reader(csvfile, delimiter='\t')
foundheader=False
for row in csvreader:
    if row[0].startswith(' '):
        foundheader=True
    if foundheader:
        print row

sample data for testing 用于测试的样本数据

#Name Test Number1 
#Sometimes there is a comment which has one line
#or even more
# Name Test Number1 \\Name of the csv
#Sometimes there is a comment which has one line
# or even more
 Category1  Number2 Test    Temperature Voltage 
 #[1]   [1/min] [s] [°C]    [mV]    
 MinMax 2.3 5   9   48  22  
 MinMax 9.87    6.01    8   9   3
 MinMax 1   2   3   4   5
 MinMax 99.52   5   8   6.66    0

output 输出

[' Category1', 'Number2', 'Test', 'Temperature', 'Voltage', '']
[' #[1]', '[1/min]', '[s]', '[\xc2\xb0C]', '[mV]', '']
[' MinMax', '2.3', '5', '9', '48', '22', '']
[' MinMax', '9.87', '6.01', '8', '9', '3']
[' MinMax', '1', '2', '3', '4', '5']
[' MinMax', '99.52', '5', '8', '6.66', '0']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM