2

I have created a figure using matplotlib and saved it using savefig function. following is the code i used,

import matplotlib.pyplot as plt

x = [0, 2, 4, 6]
y = [1, 3, 4, 8]

plt.plot(x,y)
plt.xticks([]), plt.yticks([])
plt.box(False)
plt.savefig('plot.png')
plt.show()

When i run the following code in google colabs it generates an image with the size of 432 x 288 When i run the same code in my local machine it will generates an image with the size of 640 x 480

so i tried to resize the image which i generated in local machine by adding the following changes

plt.figure(figsize=(4.32, 2.88))

Using the below code to verify that both images are exactly the same, but they are not identical.

import PIL.Image, PIL.ImageChops
im1 = PIL.Image.open(colab_image.png') 
im2 = PIL.Image.open(local_image.png')
if list(im1.getdata()) == list(im2.getdata()): 
    print("Identical");
else:
    print("Different");

Both colab and my local machine environment has -> python 3.7.0 64bit, matplotlib == 3.2.2

Please please help me to figure this issue.Because, I have no idea why this is happening. Your hep will be highly appreciated.

Here is the link for the colab notebook

Thanks and best regards.

  • 1
    what you get if you run following in your local? `from matplotlib.pyplot import figure fig = plt.figure() size = fig.get_size_inches()*fig.dpi # size in pixels print(size)` , graph looks okay there, for size may set at os level. for me mac gave `[432. 288.]
    `
    – simpleApp May 07 '21 at 22:26
  • Hi @simpleApp ,Thanks for the quick response. I am working on windows 10 OS, After run the code you have mentioned in my local machine I've got [640. 480.]. In colabs i've got [432. 288.] . – Naveen Fernando May 08 '21 at 00:59
  • @simpleApp graph plotted okay but the issue is in the resolution of the image. The resolution of the image that generates in the colabs and my local machine are different. If i reshape the local machine generated image from 640 x 480 to the 432 x 288 using the code i mentioned in my question, colab image and the reshaped images are looks same but not identical. i want to create the exact identical image in my local machine that i created in the colabs. – Naveen Fernando May 08 '21 at 01:12
  • 1
    so it is os playing a role here, what you can try is to have these two lines of code `from matplotlib.pyplot import figure figure(figsize=(20, 20), dpi=10)` set the size and dpi, before 'plt.plot(x,y)' – simpleApp May 08 '21 at 01:19
  • @simpleApp I add the code line. Now both local machine and colab generates images 200 x 200 . I compared them still they are not identical. Plus the size of the colab image is 2.33KB, and the size of the local machine image is 2.56KB. – Naveen Fernando May 08 '21 at 02:10
  • I think, it needs more research and it mainly on, efficiently determine the difference between two images. this is a good resource on it [diff on two images](https://stackoverflow.com/questions/189943/how-can-i-quantify-difference-between-two-images) – simpleApp May 08 '21 at 02:38
  • You've already checked the following method in your matplotlib configuration. `import matplotlib.pyplot as plt;plt.rcParams['figure.figsize'];plt.rcParams['figure.dpi']` – r-beginners May 08 '21 at 04:54
  • @simpleApp , Since there is a role playing by the OS, will i able to solve this issue by using the **docker-colab-local**. which i found [here](https://hub.docker.com/r/sorokine/docker-colab-local). – Naveen Fernando May 08 '21 at 09:24
  • @r-beginners, Thanks for reply my question. It made no change to the saved figure. It saved both the file in the default size. image generated in colabs = 432 x 288, image generated in Windows 10 = 640 x 480. – Naveen Fernando May 08 '21 at 09:29
  • I thought that the cause of the different image sizes might be in the preferences, so I meant that you should try running the code described in the previous comment in your environment. – r-beginners May 08 '21 at 11:38

1 Answers1

2

I think there might be an Operating System compatibility issue. Please check the code with windows and Linux/Mac environments separately.

himesh-sv
  • 36
  • 4
  • I run the code in mac and Linux environment. However, both the times i got same 640 x 480 image.I was surprised that @simpleApp was able to generate a 432x288 size image in his mac environment but i didn't :| – Naveen Fernando May 08 '21 at 19:44