How do I create a multi saved chart as .png?

Viewed 18

My goal is to save each plot after one pass of for loop. Here is what I've tried, but it shows the images overlapping. Below is my code:

import matplotlib.pyplot as plt

for i in range(10):
    y = arr_df[i].flatten()
    plt.title('Nitrat Graph')
    plt.xlabel('X Axis')
    plt.ylabel('Y Axis')
    plt.plot(x_axis, y, color='blue')
    plt.savefig(f'NO_{i}.png')
1 Answers

As long as y is a one-dimensional array, all you need to do is add plt.show() in the loop and it'll display after each iteration. Here is an example replacing your data with random data:

import matplotlib.pyplot as plt
import numpy as np
for i in range(10):
    y = np.random.rand(10,1)
    plt.title('Nitrat Graph')
    plt.xlabel('X Axis')
    plt.ylabel('Y Axis')
    plt.plot(y, color='blue')
    plt.savefig(f'NO_{i}.png')
    plt.show()
Related