
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display Matrix Value and Colormap in Matplotlib
To display the matrix value and colormap in Matplotlib, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create a figure and a set of subplots.
- Initialize max and min values for matrix.
- Plot the values of a 2D matrix or array as color-coded image.
- Iterate each cell of the color-code image and place value at the center.
- To display the figure, use show() method.
Example
import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() min_val, max_val = 0, 5 matrix = np.random.randint(0, 5, size=(max_val, max_val)) ax.matshow(matrix, cmap='ocean') for i in range(max_val): for j in range(max_val): c = matrix[j, i] ax.text(i, j, str(c), va='center', ha='center') plt.show()
Output
Advertisements