Как сохранить цифры вместо того, чтобы строить их - PullRequest
0 голосов
/ 23 апреля 2020

У меня есть эта часть кода:

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

Но я получаю сообщение об ошибке при попытке ее построить:

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.

Поэтому я хочу сохранить цифры вместо построить это, как я должен сделать?

Ответы [ 3 ]

0 голосов
/ 23 апреля 2020

Прежде всего, измените индекс вашего внутреннего l oop. В настоящее время он такой же, как внешний l oop, который вам не нужен. Например, установите j.

Что касается проблемы фигуры, добавьте следующее к своему внутреннему l oop.

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

РЕДАКТИРОВАТЬ:

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)
0 голосов
/ 23 апреля 2020
import matplotlib.pyplot as plt

plt.savefig('name.png')
0 голосов
/ 23 апреля 2020

Вы можете сохранить фигуру, используя plt.savefig("figure-name.png")

Кроме того, чтобы удалить пробелы на рисунке, используйте plt.savefig('figure-name.png', bbox_inches='tight')

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...