Updated June 15, 2023

## Introduction to NumPy hstack

When we have many arrays and want to concatenate them sequentially or stack them horizontally to make a single array, we utilize the stack function in NumPy. In NumPy, the stack function accepts a tuple of input arrays and returns a new horizontally stacked array. When using the stack function, it is important to ensure that the input arrays have compatible shapes. This means that the dimensions of the input arrays should match or be compatible with stacking horizontally. Otherwise, an error may occur.

**Syntax:**

`numpy.hstack(tuple)`

where tuple represents the input arrays that are to stacked, resulting in a single array.

### Working of NumPy hstack

The working of NumPy hstack is as follows:

- Whenever there we have more than one array, and we wish to display the values present in those arrays together sequentially or stack them horizontally one after the other in a single array, we make use of a function called stack in NumPy.
- The input arrays passed to this stack function in NumPy must be of a similar shape.

### Examples of NumPy hstack

The following are some examples:

#### Example #1

Python program to demonstrate NumPyhstack function to horizontally stack the given two input arrays into a single array and display the resulting array as the output on the screen:

**Code:**

```
#importing the package numpy
import numpy as num
#Creating an array by making use of array function in NumPy and storing it in a variable called firstarray
firstarray = num.array([[1,2,3],[4,5,6]])
#Displaying the elements of firstarray followed by one line space by making use of \n
print 'The elements of the first array are:'
print firstarray
print '\n'
#Creating an array by making use of array function in NumPy and storing it in a variable called secondarray
secondarray = num.array([[7,8,9],[10,11,12]])
#displaying the elements of second array followed by one line space by making use of \n
print 'The elements of the second array are:'
print secondarray
print '\n'
print 'The elements of the array after stacking the given two arrays horizontally by making use of hstack function are:'
#creating an array by horizontally stacking the elements of firstarray and secondarray by making use of hstack function and storing it in a variable called resultingarray
resultingarray = num.hstack((firstarray,secondarray))
#displaying the elements of the resultingarray followed by one line space by making use of \n
print resultingarray
print '\n'
```

**Output:**

The stack function combines the arrays horizontally, appending the secondary elements next to the elements of first_array along a new axis.

#### Example #2

Python program to demonstrate NumPyhstack function to horizontally stack the given two input arrays into a single array and display the resulting array as the output on the screen:

**Code:**

```
#importing the package numpy
import numpy as num
#Creating an array by making use of array function in NumPy and storing it in a variable called firstarray
firstarray = num.array([[1,2,3,13],[4,5,6,14]])
#Displaying the elements of firstarray followed by one line space by making use of \n
print 'The elements of the first array are:'
print firstarray
print '\n'
#Creating an array by making use of array function in NumPy and storing it in a variable called secondarray
secondarray = num.array([[7,8,9,15],[10,11,12,16]])
#displaying the elements of second array followed by one line space by making use of \n
print 'The elements of the second array are:'
print secondarray
print '\n'
print 'The elements of the array after stacking the given two arrays horizontally by making use of hstack function are:'
#creating an array by horizontally stacking the elements of firstarray and secondarray by making use of hstack function and storing it in a variable called resultingarray
resultingarray = num.hstack((firstarray,secondarray))
#displaying the elements of the resultingarray followed by one line space by making use of \n
print resultingarray
print '\n'
```

**Output:**

The stack function combines the arrays horizontally, meaning it appends the elements of secondary next to the elements of first_array along a new axis.

### Conclusion

In this tutorial, we understand the concept of the NumPyhstack function in Python through its definition, the syntax of the NumPy hstack function, and the working of the NumPyhstack function in Python through programming examples and their outputs.

### Recommend ed Articles

We hope that this EDUCBA information on “NumPy hstack” was beneficial to you. You can view EDUCBA’s recommended articles for more information.