21

I am using google colab for my project. I am getting grid lines on images even I am not writing them.

from matplotlib import pyplot as plt
%matplotlib inline
import cv2

img = cv2.imread('k15.jpg')

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

plt.imshow(img)

enter image description here

for code like above, I am getting grid lines which is not the case when I run the same code in my python shell.

2
  • The code you show is probably not the one responsible for the gridlines. Do they still appear if you start with a fresh kernel? Commented May 27, 2018 at 18:58
  • yes.I think its something related to google colab. it's weird.... Commented May 28, 2018 at 15:59

4 Answers 4

29
plt.imshow(myImage)
plt.grid(None)   <---- this should remove that white grid
2
  • 3
    Actually, plt.grid() will also remove the grid, so dont have to even type 'None' Commented Nov 1, 2020 at 19:10
  • This worked very well, until I tried adding a twin axis at the top of my plot with ax.twiny(). Now none of the methods here work anymore :( Commented Feb 27, 2023 at 21:30
21

Apparently something in the background changes the style. I have no experience whatsoever with google colab ti judge whether this can be responsible for the observed difference in displayed image.

In any case it should be possible to manually turn the grid lines off on a per notebook basis.

%matplotlib inline
from matplotlib import pyplot as plt
plt.rcParams["axes.grid"] = False

# rest of code
1
  • 1
    That's finally solved my problem. I was looking for somehow to stop this but couldn't find any. Thanks!! Commented May 29, 2018 at 4:12
0

If you don't mind using a different packet, you can pretty much do it easily with PIL or Pillow

from PIL import Image
img = Image.open('C:\...\k15.jpg')
img.show()
2
  • When you open it in PIL you need to provide the entirety of the image's path Commented May 26, 2018 at 16:56
  • there is no path as I am using it on google colab. also, same script is working fine on local python shell. Commented May 26, 2018 at 18:13
0

The above answer didn't work for me in an Jupyter Notebook. Here is an alternative solution - after every imshow you need to disable the grid like this:

...
plt.imshow(image)
plt.grid(False)
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.