0

I'm trying to make a grid image with pyplot--specifically one that I can use as a "gallery" of a 2d array containing some smaller images of uniform size. I've mostly succeeded, but the blank image I used to put the grid over gets random noise on it. This happens even when I go down to a 4x4 input image :(

When I run this I get the error message

C:\Users\[me]\anaconda3\lib\site-packages\matplotlib\image.py:491: 
RuntimeWarning: underflow encountered in true_divide
  vrange /= ((a_max - a_min) / frac)

From the relatively orderly noise I thought it was going to be an overflow error, but I guess not... What's going on here, and how can I fix it?

import numpy as np
import matplotlib.pyplot as plt

def grid_binim(a):
    bweight = 1
    bcolor = .5
    #shift = 0
    plt.figure()
    plt.axis("off")
    n = a.shape[0]
    size = n*n + 2*n*bweight
    out = np.ndarray((size,size))
    for i in range(n):
        pixeli = n*i+(i)*bweight*2
        out[pixeli,:] = bcolor
        out[pixeli+n+1,:] = bcolor
        for j in range(n):
            pixelj = n*j+(j)*bweight*2
            out[pixeli:pixeli+n+1,pixelj] = bcolor
            out[pixeli:pixeli+n+1,pixelj+n+1] = bcolor
            
    plt.imshow(out,cmap=plt.cm.gray,vmin=0.0,vmax=1.0)

blank = np.zeros((4,4,4,4))
grid_binim(blank)
  • 1
    You are computing a result image in `out`, and then plotting the original 4-dimensional `a`. – Tim Roberts May 01 '21 at 04:30
  • Thanks, that was actually a typo--I had a helper function doing the plotting that I didn't bother including. I just double checked and it does actually plot ```out```, so I've edited the original post :) – aimlesslegs May 01 '21 at 14:40
  • UPDATE: I just realized I wasn't initializing the values in the output array. So of course they were random stuff from memory. Still not sure what the underflow error is and would be curious, but the actual problem appears to be solved. – aimlesslegs May 01 '21 at 14:46
  • Memory in Python is never left uninitialized. It will always be set to something. "Underflow" means your division produced a result beyond the lowest range of floating point. So, if you had 2*10**-308 and you divided by 10, that's an underflow. – Tim Roberts May 02 '21 at 03:31

0 Answers0