- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to display the 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
- Related Articles
- How to make the Parula colormap in Matplotlib?
- How to create a matplotlib colormap that treats one value specially?
- How to redefine a color for a specific value in a Matplotlib colormap?
- How to draw node colormap in NetworkX/Matplotlib?
- How to set a default colormap in Matplotlib?
- How to extract a subset of a colormap as a new colormap in Matplotlib?
- How to increase colormap/linewidth quality in streamplot Matplotlib?
- Extract Matplotlib colormap in hex-format
- How to plot data into imshow() with custom colormap in Matplotlib?
- Defining the midpoint of a colormap in Matplotlib
- Add alpha to an existing Matplotlib colormap
- How to shade points in a scatter based on colormap in Matplotlib?
- 3D scatterplots in Python Matplotlib with hue colormap and legend
- Set a colormap of an image in Matplotlib
- Defining a discrete colormap for imshow in Matplotlib

Advertisements