0

I have this part of code:

import matplotlib.pyplot as plt

for i in range(1, 5, 1):
    x, y = valid_gen.__getitem__(i)
    result = model.predict(x)
    result = result > 0.4

    for i in range(len(result)):
        fig = plt.figure()
        fig.subplots_adjust(hspace=0.4, wspace=0.4)

        ax = fig.add_subplot(1, 2, 1)
        ax.imshow(np.reshape(y[i] * 255, (image_size, image_size)), cmap="gray")

        ax = fig.add_subplot(1, 2, 2)
        ax.imshow(np.reshape(result[i] * 255, (image_size, image_size)), cmap="gray")

But I am getting an error when I am trying to plot it:

RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory.

So I want to save figures instead to plot it, how should I do?

  • Does this answer your question? [Save plot to image file instead of displaying it using Matplotlib](https://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib) – Jongware Apr 23 '20 at 09:51
  • But what should I do with `ax.imshow`? –  Apr 23 '20 at 09:52
  • ```fig.savefig('path/name.png')``` should do it. Heres the corresponding documentation: https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.savefig.html – prog2de Apr 23 '20 at 09:57
  • But should I delete `ax.imshow(np.reshape(y[i] * 255, (image_size, image_size)), cmap="gray")` and `ax.imshow(np.reshape(result[i] * 255, (image_size, image_size)), cmap="gray")` ? How program knows what it should be saved? –  Apr 23 '20 at 10:01

3 Answers3

0

You can save a figure by using plt.savefig("figure-name.png")

Additionally to remove white spaces in the figure use plt.savefig('figure-name.png', bbox_inches='tight')

0

First of all, modify the index of your inner loop. It is currently the same as the outer loop, which you do not want. Set it to j for example.

Regarding your figure issue, append the following to your inner loop.

fig.savefig(f'Figure{i}_{j}.png')
plt.close(fig)

EDIT:

import matplotlib.pyplot as plt
import numpy as np

for i in range(1, 5, 1):
    x, y = valid_gen.__getitem__(i)
    result = model.predict(x)
    result = result > 0.4
    for j in range(len(result)):
        fig = plt.figure()
        fig.subplots_adjust(hspace=0.4, wspace=0.4)
        ax = fig.add_subplot(1, 2, 1)
        ax.imshow(np.reshape(y[j] * 255, (image_size, image_size)),
                  cmap='gray')
        ax = fig.add_subplot(1, 2, 2)
        ax.imshow(np.reshape(result[j] * 255, (image_size, image_size)),
                  cmap='gray')
        fig.savefig(f'Figure{i}_{j}.png')
        plt.close(fig)
Patol75
  • 4,342
  • 1
  • 17
  • 28
  • But should I delete `ax.imshow(np.reshape(y[i] * 255, (image_size, image_size)), cmap="gray")` and `ax.imshow(np.reshape(result[i] * 255, (image_size, image_size)), cmap="gray")` ? How program knows what it should be saved? –  Apr 23 '20 at 10:00
  • Do not delete those lines. Each subplot axis is created by the add_subplot method of your Figure object fig. As a consequence, calling imshow on each of these axes renders the data on the Figure object associated to those axes, which indeed is fig in your case. Simply save your figure at the end of the inner loop and close the figure to avoid memory filling. – Patol75 Apr 23 '20 at 10:04
  • Can you write your lines into my code please? I still don't understand. Sorry, I am new in python –  Apr 23 '20 at 10:27
0
import matplotlib.pyplot as plt

plt.savefig('name.png')