重新绘制旧数据(对于以前的绘图),同时绘制新的p

2024-04-25 07:34:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有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.pngtrain2.png

正如我们所看到的,第二个图也包含了第一个图的值。我不明白为什么会发生这种情况,即使我已经删除了名单后阴谋。我犯了什么错误。你知道吗

谢谢你的帮助。你知道吗


Tags: and文件csvfileimportidplotmoreas
1条回答
网友
1楼 · 发布于 2024-04-25 07:34:33

需要关闭或清除循环之间的绘图:

例如plt.close()

cla()清除一个轴,即当前图形中当前活动的轴。它使其他轴保持不变。你知道吗

clf()清除当前图形及其所有轴,但保持窗口打开,以便可以将其重新用于其他绘图。你知道吗

close()关闭一个窗口,如果没有另外指定,它将是当前窗口。你知道吗

相关问题 更多 >