How to create a graph from a matrix showing its values.

Hi,
I have two matrices A and B. Both of them 19 x19 matrix. I want to create a graph like one below using both matrices, where matrix A is the one displying its values on the graph and matrix B is displayed by countours.
How can I plot it? Many thanks!
matrix.png

 Accepted Answer

This example should help to get you started. Let me know how I can help further. You'll use contour() to draw the contour lines (link). To plot the text lables, I used the FEX function labelpoints() which can be found here: https://www.mathworks.com/matlabcentral/fileexchange/46891-labelpoints
% Produce fake data and contour matrix
x = -2:0.2:2;
y = -2:0.2:3;
[X,Y] = meshgrid(x,y);
B = X.*exp(-X.^2-Y.^2)*100;
contour(X,Y,B, 'LineWidth', 2) %this produces the contour plot
colorbar %add color bar
% Produce label matrix
A = round(B(:));
lh = labelpoints(X(:), Y(:), A, 'Center', 'color', [.5 .5 .5], 'FontSize', 8);
uistack(lh, 'bottom'); %put lables under lines
% Expand the axis limits by 2%
ylim(ylim + ([-0.02,0.02]*range(ylim)))
xlim(xlim + ([-0.02,0.02]*range(xlim)))
The following plot is produced:
190123 104924-Figure 1.jpg
Note that it might be cleaner just to label the contours themselves by using 'ShowText', 'on'. Heres' how:
contour(X,Y,B,'ShowText','on', 'LineWidth', 2)
190123 105127-Figure 1.jpg

More Answers (0)

Categories

Asked:

S_P
on 23 Jan 2019

Commented:

S_P
on 24 Jan 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!