问题
I have a matrix that looks like this:
0.06 -0.22 -0.10 0.68 NaN -0.33
0.04 -0.07 0.12 0.23 NaN -0.47
NaN NaN NaN NaN NaN 0.28
0.37 0.36 0.14 0.58 -0.14 -0.15
NaN 0.11 0.24 0.71 -0.13 NaN
0.57 0.53 0.41 0.65 -0.43 0.03
I want to color in each value based on a colormap. In Python, I know I can use imshow to assign a color to each box. How can I do it in MATLAB?
回答1:
You could use imshow as well, but every pixel would have the size of a pixel of your screen. So you may rather use imagesc.
A = [...
0.06 -0.22 -0.10 0.68 NaN -0.33;
0.04 -0.07 0.12 0.23 NaN -0.47;
NaN NaN NaN NaN NaN 0.28;
0.37 0.36 0.14 0.58 -0.14 -0.15;
NaN 0.11 0.24 0.71 -0.13 NaN;
0.57 0.53 0.41 0.65 -0.43 0.03 ]
imagesc(A)
And then you can apply any colormap you want or create your own one.
colormap(jet)
colorbar
If you don't like how imagesc handles your NaNs consider using pcolor
pcolor(A)
colormap(jet)
colorbar
with shading flat you can get rid of the grid lines.
来源:https://stackoverflow.com/questions/29152793/visualize-matrix-with-colormap-in-grid