How to add a border around a NumPy array? Last Updated : 01 Oct, 2020 Comments Improve Suggest changes Like Article Like Report Sometimes we need to add a border around a NumPy matrix. Numpy provides a function known as 'numpy.pad()' to construct the border. The below examples show how to construct a border of '0' around the identity matrix. Syntax : numpy.pad(array, pad_width, mode='constant', **kwargs) Example 1: Construct a border of 0s around 2D identity matrix Python3 # importing Numpy package import numpy as np # Creating a 2X2 Numpy matrix array = np.ones((2, 2)) print("Original array") print(array) print("\n0 on the border and 1 inside the array") # constructing border of 0 around 2D identity matrix # using np.pad() array = np.pad(array, pad_width=1, mode='constant', constant_values=0) print(array) Output: In the above examples, we construct a border of 0s around the 2-D NumPy matrix. Example 2: Construct a border of 0s around 3D identity matrix Python3 # importing Numpy package import numpy as np # Creating a 3X3 Numpy matrix array = np.ones((3, 3)) print("Original array") print(array) print("\n0 on the border and 1 inside the array") # constructing border of 0 around 3D identity matrix # using np.pad() array = np.pad(array, pad_width=1, mode='constant', constant_values=0) print(array) Output: In the above examples, we construct a border of 0s around the 3-D NumPy matrix. Example 3: Construct a border of 0s around 4D identity matrix Python3 # importing Numpy package import numpy as np # Creating a 4X4 Numpy matrix array = np.ones((4, 4)) print("Original array") print(array) print("\n0 on the border and 1 inside the array") # constructing border of 0 around 4D identity matrix # using np.pad() array = np.pad(array, pad_width=1, mode='constant', constant_values=0) print(array) Output: In the above examples, we construct a border of 0s around the 4-D NumPy matrix. Comment More infoAdvertise with us Next Article How to create an empty and a full NumPy array? V vanshgaur14866 Follow Improve Article Tags : Python Python-numpy Python numpy-arrayManipulation Practice Tags : python Similar Reads Add a border around histogram bars in Matplotlib Prerequisites: Matplotlib In this article, we will see how can we can add a border around histogram bars in our graph using matplotlib, Here we will take two different examples to showcase our graph. Approach: Import required module.Create data.Add border around histogram bars.Normally plot the data 2 min read How to create an empty and a full NumPy array? Creating arrays is a basic operation in NumPy. Empty array: This array isnât initialized with any specific values. Itâs like a blank page, ready to be filled with data later. However, it will contain random leftover values in memory until you update it.Full array: This is an array where all the elem 2 min read How to create an empty and a full NumPy array? Creating arrays is a basic operation in NumPy. Empty array: This array isnât initialized with any specific values. Itâs like a blank page, ready to be filled with data later. However, it will contain random leftover values in memory until you update it.Full array: This is an array where all the elem 2 min read Convert a NumPy array to an image Converting a NumPy array to an image is a simple way to turn numbers into pictures. A NumPy array holds pixel values, which are just numbers that represent colors. Images, like PNG or JPEG, store these pixel values in a format we can see. In this process, the NumPy array turns into an image, with ea 3 min read Convert a NumPy array to an image Converting a NumPy array to an image is a simple way to turn numbers into pictures. A NumPy array holds pixel values, which are just numbers that represent colors. Images, like PNG or JPEG, store these pixel values in a format we can see. In this process, the NumPy array turns into an image, with ea 3 min read How to append two NumPy Arrays? Prerequisites: Numpy Two arrays in python can be appended in multiple ways and all possible ones are discussed below. Method 1: Using append() method This method is used to Append values to the end of an array. Syntax : numpy.append(array, values, axis = None) Parameters : array: [array_like]Input a 4 min read Like