简体   繁体   English

为什么当pylab.show()没有时,pylab.savefig()会添加多个图,我如何使其停止?

[英]Why does pylab.savefig() add multiple plots when pylab.show() doesn't and how can I make it stop?

My program computes a number of diagrams, each with multiple plots. 我的程序计算许多图,每个图都有多个图。 When executed using pylab.show() , it shows the first diagram in a window and when I close that window it shows the second diagram and so on, just fine. 使用pylab.show()执行时,它在一个窗口中显示第一个图,当我关闭该窗口时,它显示第二个图,依此类推。

When using pylab.savefig() , the second diagram contains both the plots from the first and the second diagram. 使用pylab.savefig() ,第二张图包含第一张图和第二张图的图。

How do I keep savefig() from accumulating the plots? 如何savefig()累积图?

This is the code I'm using to plot the diagrams: 这是我用来绘制图表的代码:

# generate multiple plots for the first diagram
label = 4
for i in nylonLengthLists:  # take a list of lists to generate multiple plots
    pylab.plot(windSpeeds,i, label=str(label) + ' m')
    label += 1

pylab.title('Leinenlaengen nach Tiefen')
pylab.ylabel('Leinenlaenge (m)')
pylab.xlabel('Windgeschwindigkeit (kn)')
pylab.legend(loc='upper left', title='Tiefen')
pylab.xticks()

pylab.grid()
pylab.savefig('diagram1.png')

# generate the plots for the second diagram

nylonLengthLists = []    # reset and regenerate the list of lists
for i in range(12,25,2):
    nylonLengthLists.append(nylonLengthList(i, chainLength, boatLength)) 

label = 12
for i in nylonLengthLists:    # generate multiple plots
    pylab.plot(windSpeeds,i, label=str(label) + ' m')
    label += 2

pylab.title('Leinenlaengen nach Tiefen')
pylab.ylabel('Leinenlaenge (m)')
pylab.xlabel('Windgeschwindigkeit (kn)')
pylab.legend(loc='upper left', title='Tiefen')
pylab.xticks()

pylab.grid()
pylab.savefig('diagram2.png')

Closing a matplotlib window after a pylab.show destroys the figure instance. pylab.show之后关闭matplotlib窗口会破坏图形实例。 So a new one will be created at the next plotting command. 因此,将在下一个绘图命令中创建一个新的。

To get multiple figures without show , call pylab.figure each time you want to start a new plot. 要获取多个没有show图形,请pylab.figure每次要开始新绘图时调用pylab.figure

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM