Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (20.3k points)

Using Matplotlib, I want to plot a 2D heat map. My data is an n-by-n Numpy array, each with a value between 0 and 1. So for the (i, j) element of this array, I want to plot a square at the (i, j) coordinate in my heat map, whose color is proportional to the element's value in the array.

How can I do this?

2 Answers

0 votes
by (40.7k points)

The imshow() function with parameters interpolation='nearest' and cmap='hot' should do what you want.

import matplotlib.pyplot as plt

import numpy as np

a = np.random.random((16, 16))

plt.imshow(a, cmap='hot', interpolation='nearest')

plt.show()

0 votes
by (106k points)

You can use seaborn which takes care of a lot of the manual work and automatically plots a gradient at the side of the chart etc. See the code below:-

image

Browse Categories

...