The Numpy library in Python comes with a number of useful functions to work with and manipulate the data in arrays. In this tutorial, we will look at how to create a diagonal matrix using Numpy with the help of some examples.
How to create a diagonal matrix with Numpy?

You can use the numpy built-in numpy.diag() function to create a diagonal matrix. Pass the 1d array of the diagonal elements. 
The following is the syntax –
numpy.diag(v, k)
To create a diagonal matrix you can use the following parameters –
v– The 1d array containing the diagonal elements.k– The diagonal on which the passed elements (elements of the 1d array, v) are to be placed. By default, k is 0 which refers to the main diagonal. Diagonals above the main diagonal are positive and the ones below it are negative (see the examples below).
It returns a 2d array with the passed elements placed on the kth diagonal.
Examples
Let’s now look at examples of using the above syntax to get create a diagonal matrix using the Numppy library.
Example 1 – Diagonal matrix from 1d array placed on the default diagonal in Numpy
Let’s now use the numpy.diag() function to create a diagonal matrix from a 1d array. For example, we’ll only pass the 1d array and use the default diagonal.
import numpy as np # create a 1d array of diagonal elements ar = np.array([1, 2, 3]) # create a diagonal matrix res = np.diag(ar) # display the returned matrix print(res)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
 - Standford University Data Science: Introduction to Machine Learning
 - UC Davis Data Science: Learn SQL Basics for Data Science
 - IBM Data Science: Professional Certificate in Data Science
 - IBM Data Analysis: Professional Certificate in Data Analytics
 - Google Data Analysis: Professional Certificate in Data Analytics
 - IBM Data Science: Professional Certificate in Python Data Science
 - IBM Data Engineering Fundamentals: Python Basics for Data Science
 
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
 - Harvard University Computer Science Courses: Using Python for Research
 - IBM Python Data Science: Visualizing Data with Python
 - DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
 
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
 - UC San Diego Data Science: Probability and Statistics in Data Science using Python
 - Google Data Analysis: Professional Certificate in Advanced Data Analytics
 - MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
 - MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
 
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
[[1 0 0] [0 2 0] [0 0 3]]
We get a 2d numpy array which is a diagonal matrix. All the elements in the matrix are zero except the diagonal elements. You can see that the passed elements are placed on the main diagonal (k=0).
Example 2 – Diagonal matrix from 1d array placed on a custom diagonal in Numpy
In the above example, we placed the elements from the 1d array of the main diagonal.
The numpy.diag() function comes with an optional parameter, k that you can use to specify the diagonal you want to use to create the diagonal matrix. 
The below image better illustrates the different values of k (representing different diagonals) for a 3×3 matrix.

k is 0 by default. The diagonals below the main diagonal have k < 0 and the diagonals above it have k > 0.
Let’s now use the numpy.diag() function to create a diagonal matrix by placing the passed elements on the k=-1 diagonal.
# create a 1d array of diagonal elements ar = np.array([1, 2, 3]) # create a diagonal matrix with elements on digonal, k=-1 res = np.diag(ar, k=-1) # display the returned matrix print(res)
Output:
[[0 0 0 0] [1 0 0 0] [0 2 0 0] [0 0 3 0]]
The resulting diagonal matrix has the passed elements on the k = -1 diagonal. Here, the resulting matrix is 4×4 because all the elements in the passed array cannot be accommodated on the k=-1 diagonal of a 3×3 matrix, hence the added dimensions.
Alternative usage of the numpy.diag() function
In the above examples, we used the numpy.diag() function to create a diagonal matrix by passing a 1d array and placing its elements on the kth diagonal.
You can also use the numpy.diag() function to extract the diagonal elements from a 2d array.
For example, if you pass a 2d array to the numpy.diag() function, it will return its diagonal elements on the kth diagonal (which is 0 by default). 
# create a 2D numpy array
arr = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
# get the diagonal elements
res = np.diag(arr)
# display the diagonal elements
print(res)
Output:
[1 5 9]
We get the elements on the main diagonal as a 1d array.
Summary
In this tutorial, we looked at how to create a diagonal matrix using a 1d array in Numpy. The following are the key takeaways from this tutorial.
- Use the 
numpy.diag()function to create a diagonal matrix. Pass the diagonal elements as a 1d array. - You can specify the diagonal to place the elements in the passed array on using the optional parameter 
k. By default, it represents the main diagonal,k = 0. 
You might also be interested in –
- Extract Diagonal Elements From Numpy Array
 - Numpy – Get the Lower Triangular Matrix (With Examples)
 - Get the First N Rows of a 2D Numpy Array
 - Numpy – Remove Duplicates From Array
 
Subscribe to our newsletter for more informative guides and tutorials. 
We do not spam and you can opt out any time.
