从Matplotlib的Csv文件中读取特定列

时间:2016-09-08 11:24:27

标签: python csv matplotlib

我有一个包含41年数据集的csv文件,而csv文件有10列,分别是日期,年,月,日,pcp1,pcp2,pcp3,pcp4,pcp5,pcp6.l想通过Matplotlib模块绘制这些数据但我从Csv文件中获取数据时遇到问题。

这里是Csvfile的示例数据集:

date    day month   year    pcp1    pcp2    pcp3    pcp4    pcp5    pcp6
1.01.1979   1   1   1979    0.431   2.167   9.375   0.431   2.167   9.375
2.01.1979   2   1   1979    1.216   2.583   9.162   1.216   2.583   9.162
3.01.1979   3   1   1979    4.041   9.373   23.169  4.041   9.373   23.169
4.01.1979   4   1   1979    1.799   3.866   8.286   1.799   3.866   8.286
5.01.1979   5   1   1979    0.003   0.051   0.342   0.003   0.051   0.342
6.01.1979   6   1   1979    2.345   3.777   7.483   2.345   3.777   7.483
7.01.1979   7   1   1979    0.017   0.031   0.173   0.017   0.031   0.173
8.01.1979   8   1   1979    5.061   5.189   43.313  5.061   5.189   43.313

这是我的代码:

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



with open('output8b.csv', 'r') as csvfile:

    # get number of columns
    for line in csvfile.readlines():
        array = line.split(',')
        first_item = array[0]

    num_columns = len(array)
    csvfile.seek(0)

    reader = csv.reader(csvfile, delimiter=';')
    included_col1 = [1]
    included_col6=[6]
    x=[]
    y=[]

    for row in reader:
            content = list(row[i] for i in included_col1)
            x.append(content)
    for row in reader:
            content2 = list(row[i] for i in included_col6)
            y.append(content2)


    plt.figure(1)  # the first figure
    plt.subplot(211)
    plt.plot(x,y)
    plt.show()

当我运行代码时,我变空了#34; []" pcp的列。当我将col1=[1]更改为[1,5,6,7]时,我得到了这个:['2014', '0.003', '0', '0.003']。如何通过Matplot绘制特定列?

reader = csv.reader(csvfile, delimiter=';')
    included_col1 = [1,5,6,7]
    x=[]
    for row in reader:
            content = list(row[i] for i in included_col1)
            x.append(content)
    print (content)

0 个答案:

没有答案
相关问题