Matplotlib的选择性数据

时间:2016-09-09 21:31:42

标签: python csv matplotlib

我有41年的数据集,包括10列和l m试图用Matplotlib绘制这些数据,并且能够无误地绘制列。但是,我希望生成不同类型的图形,如年平均pcp,月平均pcp和pcp等年度总和。

示例Csv文件:

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
例如,要绘制年度总和pcp数据,我需要从1979年获得数据总和并且这样做了41年,需要花费太多精力。

这是我的代码,我仍然在学习,并且由于缺乏我的代码知识而使其变得复杂。这就是为什么我希望我可以学习简单易行的方法。(从1979年到1987年,每个pcp列,我计算每年的pcp值总和)

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



with open('output813b.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=',')
    next(reader)
    included_col1 = [1]
    included_col6=[4]
    included_col7=[5]
    included_col8=[6]
    included_col9=[7]
    included_col10=[8]
    included_col11=[9]
    x=[]
    y=[]
    y1=[]
    y2=[]
    y3=[]
    y4=[]
    y5=[]


    for row in reader:

            content = list(row[i] for i in included_col1)
            content2= list(row[i] for i in included_col6)
            content3= list(row[i] for i in included_col7)
            content4= list(row[i] for i in included_col8)
            content5= list(row[i] for i in included_col9)
            content6= list(row[i] for i in included_col10)
            content7= list(row[i] for i in included_col11)
            x.append(content)
            y.append(content2)
            y1.append(content3)
            y2.append(content4)
            y3.append(content5)
            y4.append(content6)
            y5.append(content7)



    #x1        
    klm1=tuple(x[i] for i in range(1,len(x)) if x[i]==["1979"])
    klm2=tuple(x[i] for i in range(1,len(x)) if x[i]==["1980"])
    klm3=tuple(x[i] for i in range(1,len(x)) if x[i]==["1981"])
    klm4=tuple(x[i] for i in range(1,len(x)) if x[i]==["1982"])
    klm5=tuple(x[i] for i in range(1,len(x)) if x[i]==["1983"])
    klm6=tuple(x[i] for i in range(1,len(x)) if x[i]==["1984"])
    klm7=tuple(x[i] for i in range(1,len(x)) if x[i]==["1985"])
    klm8=tuple(x[i] for i in range(1,len(x)) if x[i]==["1986"])
    klm9=tuple(x[i] for i in range(1,len(x)) if x[i]==["1987"])
    #y
    b1=len(klm1)
    b2=b1+len(klm2)
    b3=b2+len(klm3)
    b4=b3+len(klm4)
    b5=b4+len(klm5)
    b6=b5+len(klm6)
    b7=b6+len(klm7)
    b8=b7+len(klm8)
    b9=b8+len(klm9)
    s1=0;s11=0;s12=0;s13=0;s14=0;s15=0
    s2=0;s21=0;s22=0;s23=0;s24=0;s25=0
    s3=0;s31=0;s32=0;s33=0;s34=0;s35=0
    s4=0;s41=0;s42=0;s43=0;s44=0;s45=0
    s5=0;s51=0;s52=0;s53=0;s54=0;s55=0
    s6=0;s61=0;s62=0;s63=0;s64=0;s65=0
    s7=0;s71=0;s72=0;s73=0;s74=0;s75=0
    s8=0;s81=0;s82=0;s83=0;s84=0;s85=0
    s9=0;s91=0;s92=0;s93=0;s94=0;s95=0
    for i in range(1,b1):
        s1+=float(y[i][0])#### error (for +=: 'int' and 'list')"""
        s11+=float(y1[i][0])
        s12+=float(y2[i][0])
        s13+=float(y3[i][0])
        s14+=float(y4[i][0])
        s15+=float(y5[i][0])
    for i in range(b1,b2):
        s2+=float(y[i][0])
        s21+=float(y1[i][0])
        s22+=float(y2[i][0])
        s23+=float(y3[i][0])
        s24+=float(y4[i][0])
        s25+=float(y5[i][0])

    for i in range(b2,b3):
        s3+=float(y[i][0])
        s31+=float(y1[i][0])
        s32+=float(y2[i][0])
        s33+=float(y3[i][0])
        s34+=float(y4[i][0])
        s35+=float(y5[i][0])

    for i in range(b3,b4):
        s4+=float(y[i][0])
        s41+=float(y1[i][0])
        s42+=float(y2[i][0])
        s43+=float(y3[i][0])
        s44+=float(y4[i][0])
        s45+=float(y5[i][0])

    for i in range(b4,b5):
        s5+=float(y[i][0])
        s51+=float(y1[i][0])
        s52+=float(y2[i][0])
        s53+=float(y3[i][0])
        s54+=float(y4[i][0])
        s55+=float(y5[i][0])

    for i in range(b5,b6):
        s6+=float(y[i][0])
        s61+=float(y1[i][0])
        s62+=float(y2[i][0])
        s63+=float(y3[i][0])
        s64+=float(y4[i][0])
        s65+=float(y5[i][0])

    for i in range(b6,b7):
        s7+=float(y[i][0])
        s71+=float(y1[i][0])
        s72+=float(y2[i][0])
        s73+=float(y3[i][0])
        s74+=float(y4[i][0])
        s75+=float(y5[i][0])

    for i in range(b7,b8):
        s8+=float(y[i][0])
        s81+=float(y1[i][0])
        s82+=float(y2[i][0])
        s83+=float(y3[i][0])
        s84+=float(y4[i][0])
        s85+=float(y5[i][0])

    for i in range(b8,b9):
        s9+=float(y[i][0])
        s91+=float(y1[i][0])
        s92+=float(y2[i][0])
        s93+=float(y3[i][0])
        s94+=float(y4[i][0])
        s95+=float(y5[i][0]



    xa1=[1979,1980,1981,1982,1983,1984,1985,1986,1987]
    ya=[s1,s2,s3,s4,s5,s6,s7,s8,s9]
    y1a=[s11,s21,s31,s41,s51,s61,s71,s81,s91]
    y2a=[s12,s22,s32,s42,s52,s62,s72,s82,s92]
    y3a=[s13,s23,s33,s43,s53,s63,s73,s83,s93]
    y4a=[s14,s24,s34,s44,s54,s64,s74,s84,s94]
    y5a=[s15,s25,s35,s45,s55,s65,s75,s85,s95]

    plt.subplot(111)
    plt.plot(xa1,ya,xa1,y1a,xa1,y2a,xa1,y3a,xa1,y4a,xa1,y5a)
    plt.axis([1978,1987,0,2000])
    plt.title("Yearly Average Precip.")
    plt.xlabel("Years")
    plt.ylabel("PCP(mm)")


    plt.show()

enter image description here

0 个答案:

没有答案