1

次のようなエントリを含むCSVファイルがあります

id, A, B    #first line.
1, 1 2 3 4 5 6 7 8(and many more), 0 9 8 7 6 5 4 3 2(and many more) #line 1
...
...
(many more lines like this)

これは、グラフを各行にプロットしてファイルに保存するスクリプトです。たとえば、プロットされるペアは {1,0};{2,9};{3,8};... になります。

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

def plot( fileName, x, y):
    fileNameWithExt=fileName
    fileNameWithExt+='.png'
    print fileNameWithExt
    plt.plot( x, y )
    plt.xlabel( "X values" )
    plt.ylabel( "Y values" )
    plt.savefig(fileNameWithExt)

if __name__=="__main__":
    with open('test', 'r')  as csvfile:
        next(csvfile)
        spamreader = csv.reader(csvfile, delimiter=',')
        for row in spamreader:
            print (row[1].strip()).split(" ")
            x = map(int, (row[1].strip()).split(" "))
            y = map(int, (row[2].strip()).split(" "))
            plot(row[0], x, y)

ファイル( test)の例は次のとおりです。

id, A, B
train1, 1 2 3 4 5 6 7 8, 2 9 8 7 6 5 4 3
train2, 8 5 6 9 3 2 0 1, 2 4 6 8 6 9 1 6

train1.png train2.png

ご覧のとおり、2 番目のプロットには最初のプロットの値も含まれています。プロット後にリストを削除しても、なぜこれが起こっているのかわかりません。私が犯している間違い。

どんな助けでも感謝します。

4

1 に答える 1