I am trying to generate a heatmap of a 10x10 matrix. All values in the matrix are probabilities; sum of all elements equal to 1.0. I decided to use the matshow plot type (it seemed easy to use), however I cannot generate the output I'd like to have so far.
1.Visually it looks kinda ugly. Would you recommend a fitting color map for use in a heatmap?
2.Is there a way to assign predefined bins to the color map when using matshow? E.g. take a gradient of 1000 colors, always use the same colors for the corresponding probabilities. In default behavior, I think matshow checks the minimum and maximum values, assigned the first and last colors in the gradient to those values, then colorizes the values in between by interpolation.
Sometimes I have very similar probabilities in the matrix, and other times the range of probabilities may be great. Due to the default behavior I tried to explain above, I get similar plots, which makes comparisons harder.
My code for generating the said heat maps (and an example plot) is below by the way.
Thanks!
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
def pickcoord():
i = np.random.randint(0,10)
j = np.random.randint(0,10)
return [i,j]
board = np.zeros((10,10))
for i in range(1000000):
try:
direction = np.random.randint(0,2)
new_board = np.zeros((10,10))
coords = pickcoord()
if direction == 1:
for k in range(2):
new_board[coords[0]][coords[1]+k] = 1
else:
for k in range(2):
new_board[coords[0]+k][coords[1]] = 1
except IndexError:
new_board = np.zeros((10,10))
board = board + new_board
board_prob = board/np.sum(board)
plt.figure(figsize=(6,6))
plt.matshow(board_prob, cmap=matplotlib.cm.Spectral_r, interpolation='none')
plt.xticks(np.arange(0.5,10.5), [])
plt.yticks(np.arange(0.5,10.5), [])
plt.grid()
Your second problem can be solved using the vmin
and vmax
arguments of the matshow
function:
matshow(board_prob, cmap=cm.Spectral_r, interpolation='none', vmin=0, vmax=1)
Considering your first problem, it depends on what you want to emphasize or display. Choose a fitting colormap from the default colormaps of matplotlib.