简体   繁体   中英

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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