0
import numpy as np
import os.path

from skimage.io import imread
from skimage import data_dir

img = imread(os.path.join(data_dir, 'checker_bilevel.png'))

import matplotlib.pyplot as plt
#plt.imshow(img, cmap='Blues')
#plt.show()

imgT = img.T
plt.figure(1)
plt.imshow(imgT,cmap='Greys')
#plt.show()

imgR = img.reshape(20,5)
plt.figure(2)
plt.imshow(imgR,cmap='Blues')

plt.show(1)

I read that plt.figure() will create or assign the image a new ID if not explicitly given one. So here, I have given the two figures, ID 1 & 2 respectively. Now I wish to see only one one of the image. I tried plt.show(1) epecting ONLY the first image will be displayed but both of them are. What should I write to get only one?

Khalil Al Hooti
  • 4,207
  • 5
  • 23
  • 40
user10089194
  • 339
  • 1
  • 5
  • 14

2 Answers2

1

plt.clf() will clear the figure

import matplotlib.pyplot as plt


plt.plot(range(10), 'r')

plt.clf()


plt.plot(range(12), 'g--')
plt.show()
Khalil Al Hooti
  • 4,207
  • 5
  • 23
  • 40
0

plt.show will show all the figures created. The argument you forces the figure to be shown in a non-blocking way. If you only want to show a particular figure you can write a wrapper function.

import matplotlib.pyplot as plt

figures = [plt.subplots() for i in range(5)]

def show(figNum, figures):
    if plt.fignum_exists(figNum):
        fig = [f[0] for f in figures if f[0].number == figNum][0]
        fig.show()
    else:
        print('figure not found')
cvanelteren
  • 1,633
  • 9
  • 16