Matplotlib: 在循环中绘制多个单独图表

0 投票
1 回答
2053 浏览
提问于 2025-04-18 17:03

我想把多个基准测试的结果画在不同的图上。下面是我的代码:

for benchmark in benchmarks:
   readFile = open(benchmark+'.txt')
   text = readFile.read()
   x = re.findall(r"(\d+)",text)
   x = [int(i) for i in liveRatio]
   pylab.plot(x)
   F = pylab.gcf()
   F.savefig('benchmark',dpi=200)

这段代码把所有的数据都画在了一张图上。但是,我想要每个基准测试都有自己单独的图。

1 个回答

2

在每次绘图之前,你需要先清空图形:

for benchmark in benchmarks:
   readFile = open(benchmark+'.txt')
   text = readFile.read()
   x = re.findall(r"(\d+)",text)
   x = [int(i) for i in liveRatio]

   #clear the figure
   pylab.clf()

   pylab.plot(x)
   F = pylab.gcf()
   F.savefig('benchmark',dpi=200)

另外,每次循环时图形都会被覆盖,所以我建议你可以这样做:

   F.savefig(benchmark+'.png',dpi=200)

撰写回答