Skip to content
Advertisement

Read datasets one by one and put them next to the previous plot

I have some datasets which are too big and I want to plot these datasets with matplotlib‘s imshow() function.

I need to plot the datasets concatenated with matplotlib, but since the datasets are quite large, when I try to concatenate it causes my computer to overheat (I use the NumPy library to concatenate). Is it possible for me to read these datasets one by one and put them next to the previous plot, as I have given below as a gif? Since I read all datasets one by one, it will not cause heating.

enter image description here

Advertisement

Answer

Let’s assume this input:

array = np.diag(np.ones(3))
arrays = [array,array*2,array*3]
[array([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]]),
 array([[2., 0., 0.],
        [0., 2., 0.],
        [0., 0., 2.]]),
 array([[3., 0., 0.],
        [0., 3., 0.],
        [0., 0., 3.]])]

looking like:

plt.imshow(np.concatenate(arrays, axis=1))

original data

Now we want to plot block by block. For this we loop over the blocks and use the extent option of imshow

ax = plt.subplot()

vmin = min(a.min() for a in arrays)  ## if you know the values in advance you
vmax = max(a.max() for a in arrays)  ## can input them instead of calculating

for i,a in enumerate(arrays):
    h,w = a.shape
    ax.imshow(a, vmin=vmin, vmax=vmax,
              extent=[(w*i)-0.5, (w*(i+1))-0.5, h-0.5, -0.5])
ax.set_xlim(-0.5, (w*(i+1))-0.5)

imshow block by block

You need to know in advance the min/max values of your whole dataset and specify it to imshow with the vmin/vmax parameters, otherwise each dataset will be scaled independently.

Now, if the arrays have different widths (arrays = [array,(array*2)[:, :2],array*3]), you can use the following:

ax = plt.subplot()

vmin = min(a.min() for a in arrays)
vmax = max(a.max() for a in arrays)

W = 0
for i,a in enumerate(arrays):
    h,w = a.shape
    ax.imshow(a, vmin=vmin, vmax=vmax,
              extent=[W-0.5, (W+w)-0.5, h-0.5, -0.5])
    W+=w
ax.set_xlim(-0.5, W-0.5)

imshow blocks of different width

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement