How To Use The NumPy hstack Function

In NumPy hstack is one of the stacking functions. The main difference is that it stacks arrays horizontally. If you want to understand more about this functionality, we have a detailed guide below.

Table of Contents

NumPy hstack Function

The numpy.hstack() function works column-wise. For multi-dimensional arrays, this concatenation happens along the second axis. On the other hand, the function stacks 1D arrays along their first axis.

Like most other stacking functions, it doesn’t accept any optional arguments. What it needs is a tuple that contains all the arrays you want to stack with this function.

Here is a simple example of using numpy.hstack() on 1D arrays. We create two original arrays with the numpy.array() function first, then pass them into numpy.stack().

import numpy as np
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
c = np.hstack([a, b])
c.shape # (6,)
array([ 1, 2, 3, 10, 20, 30])

Make sure you load the NumPy library first – your Python program will produce errors like “name ‘array’ is not defined” otherwise. Also, put all your input arrays between a pair of square brackets to create a tuple.

The result here is a 1D array that contains all the elements of both input arrays. The numpy.hstack() doesn’t do much more than simply join them side by side.

Unlike some other stacking functions, which require the provided arrays to have the same shape, you can stack arrays of different shapes with numpy.hstack().

The stacking operation takes place pretty much the same as the function doesn’t create another dimension on the returned array. Here is what numpy.hstack() produces when you stack two 1D arrays of different lengths:

a = np.array([1, 2, 3])
b = np.array([10, 20])
np.hstack([a, b])
array([ 1, 2, 3, 10, 20])

2D and 3D arrays are where you can see the most useful applications of numpy.hstack().

Here, the arrays a and b have the same shape (2, 3). The numpy.hstack() function can easily join them into a single (2, 6) array:

a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[10, 20, 30], [40, 50, 60]])
np.hstack([a, b])
array([[ 1, 2, 3, 10, 20, 30],
       [ 4, 5, 6, 40, 50, 60]])  

The above example demonstrates more clearly how this function works along the second axis (column). The number of rows doesn’t change, but the columns are added side by side to create a single array.

Like 1D arrays, you can stack 2D arrays of different shapes. The only requirement is that they must share the same element in the concatenation axis, which is here the dimension 0 (the number of rows).

We have here an array (a) with 2 rows and 3 columns, while b has 2 rows and 6 columns. If you want to combine these two arrays into a single array, you can use the numpy.hstack() function as follows:

a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18]])
np.hstack([a, b])
array([[ 1, 2, 3, 7, 8, 9, 10, 11, 12],
       [ 4, 5, 6, 13, 14, 15, 16, 17, 18]])

The result of this stacking operation is a (2, 9) array. What numpy.stack() does is just to put b on the right of a. It doesn’t care about the difference in the number of columns as long as those arrays have the same number of rows.


The NumPy hstack function stacks multiple arrays along the second axis. The only exception is 1D arrays, where the concatenation happens along the first axis. Make sure your input arrays match the requirement when it comes to their shapes to avoid errors.

Leave a Reply

Your email address will not be published. Required fields are marked *