'Plotting a matrix on a heatmap using Python

I have two matrices. I want to highlight b on a heatmap generated with matrix a. Is there straightforward way to do it?

a = np.array([[0.40926238, 0.66471655, 0.05410414, 0.92418819, 0.3743909 ],
       [0.97651759, 0.18147079, 0.03769747, 0.12720863, 0.11],
       [0.84298074, 0.72379236, 0.63551217, 0.12, 0.63157138],
       [0.1490922 , 0.71312848, 0.29995778, 0.50983848, 0.80253496],
       [0.33389957, 0.79653182, 0.89042807, 0.41, 0.97443408]])

b = np.array([0.05410414, 0.03769747, 0.12720863, 0.11      , 0.12      ,
       0.63157138, 0.12      , 0.50983848, 0.29995778, 0.41      ,
       0.71312848, 0.1490922 , 0.33389957, 0.79653182])


Solution 1:[1]

The arrays a and b have different dimensionality. They can be visualized e.g. by

import numpy as np
from matplotlib import pyplot as plt

a = np.array([[0.40926238, 0.66471655, 0.05410414, 0.92418819, 0.3743909 ],
       [0.97651759, 0.18147079, 0.03769747, 0.12720863, 0.11],
       [0.84298074, 0.72379236, 0.63551217, 0.12, 0.63157138],
       [0.1490922 , 0.71312848, 0.29995778, 0.50983848, 0.80253496],
       [0.33389957, 0.79653182, 0.89042807, 0.41, 0.97443408]])

b = np.array([0.05410414, 0.03769747, 0.12720863, 0.11      , 0.12      ,
       0.63157138, 0.12      , 0.50983848, 0.29995778, 0.41      ,
       0.71312848, 0.1490922 , 0.33389957, 0.79653182])


fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5));
im = ax1.imshow(a)
ax1.set_title("value of a over indices")
cax = plt.axes([0.46, 0.1, 0.02, 0.8])
plt.colorbar(im, cax=cax)
ax2.plot(b)
ax2.set_title("value of b over index")
plt.show()

output of imshow and plot

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cknoll