Why am I getting this underflow error while slicing numpy ndarrays?

177 Views Asked by At

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)
0

There are 0 best solutions below