'Plot a 2D array with axes labelled with the array values of a 1D array
I have a 1D array arr1d = ['A', 'B', 'C', 'D'] and I have a 2D array
arr2d =[[1, 2, 5, 3], [2, 1, 2, 5], [5, 3, 4, 4], [5, 5, 3, 4]] (say)
I wish to plot the array using matplotlib (or any other library) in such a way that the output is in the following manner.
I want the x and y axis to get labelled as per the 1D array as shown in the picture.
How to achieve that?
Solution 1:[1]
You could achieve a result close to what you need with matshow in matplotlib.
import matplotlib.pyplot as plt
arr1d = ['A', 'B', 'C', 'D']
arr2d =[[1, 2, 5, 3], [2, 1, 2, 5], [5, 3, 4, 4], [5, 5, 3, 4]]
plt.matshow(arr2d)
plt.xticks([0,1,2,3], arr1d)
plt.yticks([0,1,2,3], arr1d)
plt.show()
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 | DNy |


