Matplotlib: plot multiple individual plots in a loop

纵饮孤独 提交于 2019-12-25 03:27:36

问题


I want to plot multiple benchmarks, each on a separate plot. Here's my code:

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)

The code plots all the data on the same plot. But, I want individual separate plots for each benchmark.


回答1:


You need to clear the figure before each plot call:

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)

On a second note each time you iterate the figure will be overwritten so I suggest something like this:

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


来源:https://stackoverflow.com/questions/25273365/matplotlib-plot-multiple-individual-plots-in-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!