python - Heatmap with matplotlib using matshow -
i trying generate heatmap of 10x10 matrix. values in matrix probabilities; sum of elements equal 1.0. decided use matshow plot type (it seemed easy use), cannot generate output i'd have far.
1.visually looks kinda ugly. recommend fitting color map use in heatmap?
2.is there way assign predefined bins color map when using matshow? e.g. take gradient of 1000 colors, use same colors corresponding probabilities. in default behavior, think matshow checks minimum , maximum values, assigned first , last colors in gradient values, colorizes values in between interpolation.
sometimes have similar probabilities in matrix, , other times range of probabilities may great. due default behavior tried explain above, similar plots, makes comparisons harder.
my code generating said heat maps (and example plot) below way.
thanks!
import bumpy np def pickcoord(): = np.random.randint(0,10) j = np.random.randint(0,10) return [i,j] board = np.zeros((10,10)) in range(1000000): try: direction = np.random.randint(0,2) new_board = np.zeros((10,10)) coords = pickcoord() if direction == 1: k in range(2): new_board[coords[0]][coords[1]+k] = 1 else: 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) figure(figsize(6,6)) matshow(board_prob, cmap=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 solved using vmin , vmax arguments of matshow function:
matshow(board_prob, cmap=cm.spectral_r, interpolation='none', vmin=0, vmax=1) considering first problem, depends on want emphasize or display. choose fitting colormap default colormaps of matplotlib.
Comments
Post a Comment