# Plotting images using matplotlib and for loop shows matrix in background

**URL:** <https://forum.opencv.org/t/plotting-images-using-matplotlib-and-for-loop-shows-matrix-in-background/3898>\
**Category:** Python\
**Created:** [June 17, 2021, 6:27pm UTC](https://forum.opencv.org/t/plotting-images-using-matplotlib-and-for-loop-shows-matrix-in-background/3898 "2021-06-17T18:27:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Apurva\_Sinha](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/apurva_sinha/32/673_2.png) [@Apurva\_Sinha](https://forum.opencv.org/u/Apurva_Sinha)\
**Post date:** [June 17, 2021, 6:27pm UTC](https://forum.opencv.org/t/plotting-images-using-matplotlib-and-for-loop-shows-matrix-in-background/3898/1 "2021-06-17T18:27:39Z")

</div>

I am getting the some matrix in the background (most likely the image matrix, not sure though). Have tried debugging and removing the for loop with 8 lines of the same code does not lead to this problem. Any leads, anyone? Code below:

Input:

```auto
import cv2 as cv
from matplotlib import pyplot as plt

img=cv.imread(r'C:\Users\sinha\OneDrive\Desktop\Camera\IMG_20181018_182302.jpg',cv.IMREAD_REDUCED_GRAYSCALE_8)
img=cv.medianBlur(img,5)

ret1,thresh1=cv.threshold(img,127,255,cv.THRESH_BINARY)
ret2,thresh2=cv.threshold(img,127,255,cv.THRESH_BINARY)
ret3,thresh3=cv.threshold(img,127,255,cv.THRESH_MASK)
ret4,thresh4=cv.threshold(img,50,255,cv.THRESH_TRUNC)
ret5,thresh5=cv.threshold(img,50,255,cv.THRESH_TOZERO)
ret6,thresh6=cv.threshold(img,50,255,cv.THRESH_MASK)
ret7,thresh7=cv.threshold(img,50,255,cv.THRESH_BINARY_INV)
ret8,thresh8=cv.threshold(img,50,255,cv.THRESH_TOZERO_INV)

images = [img, thresh1, thresh2, thresh3, thresh4, thresh5, thresh6, thresh7, thresh8]
title = [img, thresh1, thresh2, thresh3, thresh4, thresh5, thresh6, thresh7, thresh8]

for i in range(9):
    e1 = cv.getTickCount()
    plt.subplot(3,3,i+1),plt.imshow(images[i],'gray'),plt.title(title[i]), plt.xticks([]), plt.yticks([])
    e2 = cv.getTickCount()
    time=(e2-e1)/cv.getTickFrequency()
    print(i,": cv.getTickCount()= ",time,", cv.getTickFrequency()= ",cv.getTickFrequency())

# plt.subplot(3,3,1),plt.imshow(img,'gray'),plt.title("img"),plt.xticks([]), plt.yticks([])
# plt.subplot(3,3,2),plt.imshow(thresh1,'gray'),plt.title("thresh1"),plt.xticks([]), plt.yticks([])
# plt.subplot(3,3,3),plt.imshow(thresh2,'gray'),plt.title("thresh2"),plt.xticks([]), plt.yticks([])
# plt.subplot(3,3,4),plt.imshow(thresh3,'gray'),plt.title("thresh3"),plt.xticks([]), plt.yticks([])
# plt.subplot(3,3,5),plt.imshow(thresh4,'gray'),plt.title("thresh4"),plt.xticks([]), plt.yticks([])
# plt.subplot(3,3,6),plt.imshow(thresh5,'gray'),plt.title("thresh5"),plt.xticks([]), plt.yticks([])
# plt.subplot(3,3,7),plt.imshow(thresh6,'gray'),plt.title("thresh6"),plt.xticks([]), plt.yticks([])
# plt.subplot(3,3,8),plt.imshow(thresh7,'gray'),plt.title("thresh7"),plt.xticks([]), plt.yticks([])
# plt.subplot(3,3,9),plt.imshow(thresh8,'gray'),plt.title("thresh8"),plt.xticks([]), plt.yticks([])
plt.show()
print(cv.useOptimized())

```

Ooutput:

 ![Screenshot (21)](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/9/99c402159e63ddfa22ada83de329a62789d6880f.png)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [June 17, 2021, 6:37pm UTC](https://forum.opencv.org/t/plotting-images-using-matplotlib-and-for-loop-shows-matrix-in-background/3898/2 "2021-06-17T18:37:02Z")

</div>

this is the opencv help page, we cannot help with weird matplotlib behaviour  
(or any other arbitrray python problem)
