简体   繁体   English

避免覆盖列表的值-Python(Pandas)

[英]Avoid overwriting values of a list - Python (Pandas)

I have a list of values that I am plotting and every time I loop through the list, I create a plot. 我有一个要绘制的值的列表,每次遍历列表时,都会创建一个图。 However, the plots overwrite every time it goes through the loop. 但是,每次循环时,图都会覆盖。 This is what I tried for far that did not work. 这是我到目前为止一直没有尝试的方法。

myPath = "//my/absolute/path"

for i in list_val:
    i.plot('var1', 'var2')
    plt.savefig(os.path.join(myPath,''.join("figure{y}.png".format(y = i))))
    plt.show()

However, when I tried the following, it overwrites the images(which I knew it would happen), 但是,当我尝试以下操作时,它会覆盖图像(我知道会发生这种情况),

myPath = "//my/absolute/path"

for i in list_val:
    i.plot('var1', 'var2')
    plt.savefig(os.path.join(myPath,''.join("figure.png")))
    plt.show()

How can I modify my first snippet above to avoid overwriting images? 如何修改上面的第一个代码段以避免覆盖图像?

How about 怎么样

myPath = "//my/absolute/path"

for index,df in enumerate(list_val):
    df.plot('var1', 'var2')
    plt.savefig(os.path.join(myPath,''.join("figure{y}.png".format(y = index))))
    plt.show()

Try 尝试

myPath = "//my/absolute/path"

for i in range(1, len(list_val)):
    list_val[i].plot('var1', 'var2')
    plt.savefig(os.path.join(myPath,''.join("figure{y}.png".format(y = i+1))))
    plt.show()

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

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