简体   繁体   English

如何在 python plt.savefig() python matplotlib 2020 中使用相同的变量保存图形

[英]How to save figure with a variable in the same in python plt.savefig() python matplotlib 2020

I have seen similar questions and answer but they are very old and do not work.我见过类似的问题和答案,但它们很旧,不起作用。

for i in range (0,7)

    plt.figure()
    plt.plot(energia-ef,dos[a-3],linewidth=0.6, color='orange',label='Central')
    plt.plot(energia-ef,dos[a-2],linewidth=0.6, color ='orange')
    plt.plot(energia-ef,dos[a-1],linewidth=0.6, color ='red',label='Layer')
    plt.plot(energia-ef,dos[a],linewidth=0.6, color ='red')
    plt.plot((0, 0), (-15, 15), 'k-', linewidth=0.5)
    plt.ylabel("DOS (number of states/eV)")
    plt.legend()
    plt.savefig('/Users/andreaangeletti/Dropbox/data/anand/surface/scan/7/relaxed/dos/p/i.png')

where i should be equal the value of the for.其中 i 应该等于 for 的值。

Actually I will first have to create the first set of images but then I would run again the code and rewrite over the old ones.实际上,我首先必须创建第一组图像,然后我会再次运行代码并重写旧的。

How it would be possible to use a string instead to specify the entire path name as savefig() argument, except for i variable part.除了 i 变量部分之外,如何使用字符串来指定整个路径名作为 savefig() 参数。

If you want to write it i different time you need to do;如果你想在不同的时间写它,你需要做;

plt.savefig('/Users/andreaangeletti/Dropbox/data/anand/surface/scan/7/relaxed/dos/p/'+str(i)+'.png',bbox_inches='tight')

then after for, close it.然后在for之后,关闭它。

plt.close()

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

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