vstack/hstack
vstack stacks arrays on top of each other and hstack places them side by side.
In this page:
Syntax
np.vstack((arr1, arr2))
np.hstack((arr1, arr2))
vstack/hstack
vstack joins vertically (more rows) and hstack joins horizontally (more columns). They are convenient shorthands over concatenate that also handle 1-D inputs sensibly. np.column_stack turns 1-D arrays into columns.
Note:
vstack of two 1-D arrays gives a 2-row matrix.
Example: vstack/hstack
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.vstack((a, b)))
print(np.hstack((a, b)))
# Output:
# [[1 2 3]
# [4 5 6]]
# [1 2 3 4 5 6]
Related Topics
Common Mistakes
- Using hstack when you meant vstack
- Passing mismatched lengths
- Forgetting to pass a tuple or list
Chapter Summary
- vstack adds rows
- hstack adds columns
- Both take a tuple of arrays
- Handy for 1-D inputs
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: