It's easier to understand what np.vstack, np.hstack and np.dstack* do by looking at the .shape attribute of the output array.

Using your two example arrays:

print(a.shape, b.shape)
# (3, 2) (3, 2)
  • np.vstack concatenates along the first dimension...

    print(np.vstack((a, b)).shape)
    # (6, 2)
    
  • np.hstack concatenates along the second dimension...

    print(np.hstack((a, b)).shape)
    # (3, 4)
    
  • and np.dstack concatenates along the third dimension.

    print(np.dstack((a, b)).shape)
    # (3, 2, 2)
    

Since a and b are both two dimensional, np.dstack expands them by inserting a third dimension of size 1. This is equivalent to indexing them in the third dimension with np.newaxis (or alternatively, None) like this:

print(a[:, :, np.newaxis].shape)
# (3, 2, 1)

If c = np.dstack((a, b)), then c[:, :, 0] == a and c[:, :, 1] == b.

You could do the same operation more explicitly using np.concatenate like this:

print(np.concatenate((a[..., None], b[..., None]), axis=2).shape)
# (3, 2, 2)

* Importing the entire contents of a module into your global namespace using import * is considered bad practice for several reasons. The idiomatic way is to import numpy as np.

Answer from ali_m on Stack Overflow
🌐
w3resource
w3resource.com › numpy › manipulation › dstack.php
NumPy: numpy.dstack() function - w3resource
This function is particularly useful for working with image data, where each image is represented as a 2D array of pixel values, and a collection of images can be stacked depth-wise to create a 3D array of shape (height, width, number of images).
🌐
University of Texas at Austin
het.as.utexas.edu › HET › Software › Numpy › reference › generated › numpy.dstack.html
numpy.dstack — NumPy v1.9 Manual
This is a simple way to stack 2D arrays (images) into a single 3D array for processing. See also · vstack · Stack along first axis. hstack · Stack along second axis. concatenate · Join arrays. dsplit · Split array along third axis. Notes · Equivalent to np.concatenate(tup, axis=2). Examples ...
🌐
Programtalk
programtalk.com › python-examples › numpy.dstack
numpy.dstack Example - Program Talk
Examples -------- Load multiple images:: >>> from pylinac.core.image import load_multiples >>> paths = ['starshot1.tif', 'starshot2.tif'] >>> superimposed_img = load_multiples(paths) """ # load images img_list = [load(path, **kwargs) for path in image_file_list] first_img = img_list[0] # check that all images are the same size and stretch if need be for img in img_list: if img.shape != first_img.shape: raise ValueError("Images were not the same shape") if stretch: img.array = stretcharray(img.array, fill_dtype=first_img.array.dtype) # stack and combine arrays new_array = np.dstack(tuple(img.ar
🌐
SciPy
docs.scipy.org › doc › numpy-1.12.0 › reference › generated › numpy.dstack.html
numpy.dstack — NumPy v1.12 Manual
January 16, 2017 - Takes a sequence of arrays and ... dsplit. This is a simple way to stack 2D arrays (images) into a single 3D array for processing. This function continues to be supported for backward compatibility, but you should prefer np.concatenate or np.stack....
Top answer
1 of 4
91

It's easier to understand what np.vstack, np.hstack and np.dstack* do by looking at the .shape attribute of the output array.

Using your two example arrays:

print(a.shape, b.shape)
# (3, 2) (3, 2)
  • np.vstack concatenates along the first dimension...

    print(np.vstack((a, b)).shape)
    # (6, 2)
    
  • np.hstack concatenates along the second dimension...

    print(np.hstack((a, b)).shape)
    # (3, 4)
    
  • and np.dstack concatenates along the third dimension.

    print(np.dstack((a, b)).shape)
    # (3, 2, 2)
    

Since a and b are both two dimensional, np.dstack expands them by inserting a third dimension of size 1. This is equivalent to indexing them in the third dimension with np.newaxis (or alternatively, None) like this:

print(a[:, :, np.newaxis].shape)
# (3, 2, 1)

If c = np.dstack((a, b)), then c[:, :, 0] == a and c[:, :, 1] == b.

You could do the same operation more explicitly using np.concatenate like this:

print(np.concatenate((a[..., None], b[..., None]), axis=2).shape)
# (3, 2, 2)

* Importing the entire contents of a module into your global namespace using import * is considered bad practice for several reasons. The idiomatic way is to import numpy as np.

2 of 4
6

Let x == dstack([a, b]). Then x[:, :, 0] is identical to a, and x[:, :, 1] is identical to b. In general, when dstacking 2D arrays, dstack produces an output such that output[:, :, n] is identical to the nth input array.

If we stack 3D arrays rather than 2D:

x = numpy.zeros([2, 2, 3])
y = numpy.ones([2, 2, 4])
z = numpy.dstack([x, y])

then z[:, :, :3] would be identical to x, and z[:, :, 3:7] would be identical to y.

As you can see, we have to take slices along the third axis to recover the inputs to dstack. That's why dstack behaves the way it does.

🌐
KajoData
kajodata.com › main page › knowledge base – excel, sql, python, powerbi, tableau, statistics › knowledge base – python › how numpy dstack works in python? best example
How numpy dstack works in Python? Best example - KajoData
April 15, 2025 - import numpy as np # Simulating two 2D grayscale images image1 = np.array([[10, 20], [30, 40]]) image2 = np.array([[50, 60], [70, 80]]) # Stacking images into a multi-channel format merged = np.dstack((image1, image2)) print(merged.shape) # Output: (2, 2, 2) Now, the depth (third axis) indicates the number of channels, similar to color images in RGB format.
🌐
SciPy
docs.scipy.org › doc › numpy-1.10.1 › reference › generated › numpy.dstack.html
numpy.dstack — NumPy v1.10 Manual
January 16, 2017 - This is a simple way to stack 2D arrays (images) into a single 3D array for processing. See also · stack · Join a sequence of arrays along a new axis. vstack · Stack along first axis. hstack · Stack along second axis. concatenate · Join a sequence of arrays along an existing axis. dsplit ...
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › dstack()
Python Numpy dstack() - Stack Arrays Depthwise
November 18, 2024 - The numpy.dstack() function in Python offers a powerful approach to stacking arrays depth-wise along the third dimension. It works effectively for combining images, data frames, or any set of matrices where a third-dimensional aggregation is desired.
Find elsewhere
🌐
SciPy
docs.scipy.org › doc › › numpy-1.13.0 › reference › generated › numpy.dstack.html
numpy.dstack — NumPy v1.13 Manual
June 10, 2017 - Takes a sequence of arrays and ... dsplit. This is a simple way to stack 2D arrays (images) into a single 3D array for processing. This function continues to be supported for backward compatibility, but you should prefer np.concatenate or np.stack....
🌐
Codingtag
codingtag.com › numpy-dstack-method
Numpy dstack() method
The np.dstack() function stacks arrays in sequence depth-wise (along the third axis). This means it takes a sequence of 2D arrays and stacks them along a new third axis, creating a 3D array.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.dstack.html
numpy.dstack — NumPy v2.5 Manual
>>> import numpy as np >>> a = np.array((1,2,3)) >>> b = np.array((4,5,6)) >>> np.dstack((a,b)) array([[[1, 4], [2, 5], [3, 6]]]) >>> a = np.array([[1],[2],[3]]) >>> b = np.array([[4],[5],[6]]) >>> np.dstack((a,b)) array([[[1, 4]], [[2, 5]], [[3, 6]]]) Go BackOpen In Tab ·
🌐
Medium
medium.com › @andiksyldnata › understanding-numpy-dstack-in-python-eb40e4467c09
Understanding NumPy dstack in Python | by 99spaceidea | Medium
June 21, 2023 - Press enter or click to view image in full size · The dstack() function takes a sequence of arrays as input and returns a single array that is stacked along the third axis. The arrays in the sequence must have the same shape along all but the ...
🌐
pythontutorials
pythontutorials.net › blog › numpy-dstack
Mastering `numpy.dstack`: A Comprehensive Guide — pythontutorials.net
In this example, we create three 2D arrays representing the red, green, and blue color channels of an image. Then we use np.dstack to combine them into a single 3D array representing the complete image.
🌐
Towards Data Science
towardsdatascience.com › home › data science › np.stack() - how to stack two arrays in numpy and python
np.stack() - How To Stack two Arrays in Numpy And Python | Towards Data Science
January 10, 2023 - Image 1 - Horizontal stacking explained (image by author) Vertical stacking works just the opposite. One row of two vertically stacked arrays contains corresponding elements from both. For example, the first row of a vertically stacked array Z will contain the first elements of the input arrays X and Y. ... And with that out the way, let's go over the np stack function signature.
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › stack()
Python Numpy stack() - Stack Arrays
December 26, 2024 - In this scenario, the stacking of individual 2D arrays representing color channels forms a 3D array representing a full-color image. Utilize np.stack() to combine features from various sources into a structured form.
🌐
SciPy
docs.scipy.org › doc › numpy-1.11.0 › reference › generated › numpy.dstack.html
numpy.dstack — NumPy v1.11 Manual
May 29, 2016 - This is a simple way to stack 2D arrays (images) into a single 3D array for processing. ... Join a sequence of arrays along a new axis. ... Stack along first axis. ... Stack along second axis. ... Join a sequence of arrays along an existing axis. ... Split array along third axis.
🌐
SciPy
docs.scipy.org › doc › numpy-1.7.0 › reference › generated › numpy.dstack.html
numpy.dstack — NumPy v1.7 Manual (DRAFT)
February 12, 2013 - This is a simple way to stack 2D arrays (images) into a single 3D array for processing. See also · vstack · Stack along first axis. hstack · Stack along second axis. concatenate · Join arrays. dsplit · Split array along third axis. Notes · Equivalent to np.concatenate(tup, axis=2). Examples ...