Plot matrix with ticks corresponding to the data points

7 views (last 30 days)
I have a matrix, call it M (attached), that contains 0s, 1s, and 2s, that I would like to plot. This matrix is generated using vectors x and y, where i-the element of x and j-th element y are used to generate {i,j}-th entry of M. The vectors x and y are attached, too.
Question: Can I plot M such that on x-axis I have actual values of x and on y-axis actual values of y?
What I tried: If I do imagesc(M) then this displays color-coded entry of the matrix but the ticks correspond to indices of M. I know I can set the limits for x-axis and y-axis but still the ticks do not correspond to the values of the grid.
As I show in the attached figure, the figure generated in that way shows that the teal area (which corresponds to 1s) appears at around 0.6 value on y-axis, but according to the grid it should be around 0.8, because the grid is not uniformly distributed.
Is there any way to change the behavior of image/imagesc or is there any other function that I could use?
  4 Comments

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 11 Mar 2023
Edited: Star Strider on 11 Mar 2023
Try something like this —
LD = load(websave('M','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1321115/M.mat'));
M = LD.M;
LD1 = load(websave('x_vec','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1321610/x_vec.mat'));
LD2 = load(websave('y_vec','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1321615/y_vec.mat'));
x = LD1.x;
y = LD2.y;
figure
imagesc(x,y,M)
Ax = gca;
Ax.YDir = 'normal';
I am not certain how you originally plotted that, so I’m using imagesc here.
EDIT — (11 Mar 2023 at 23:39)
Re-plotted ‘M’ using the recerntly-provided ‘x’ and ‘y’ vectors.
.
  3 Comments
Star Strider
Star Strider on 12 Mar 2023
O.K.
I have no idea how you calculated that, and it¹s certainly not obvious from the ‘M¹ matrix originally posted. My original post does correspond to the original .pdf file image.
Always feel free to change the rules without telling us! That’s what makes Answers so much fun!
Michal Szkup
Michal Szkup on 12 Mar 2023
Not sure if you are being sarcastic or not.
Maybe the question was not clear. But my point was that I have generated matrix M using some function F(x,y) for various values of x and y and wanted to plot the outcome using the actual grid values x and y in imagesc. Instead, the function imagesc seems to assume that M was generated using unfirom grid.
So what I did, I considered a uniform grid values (say x2 and y2) and mapped the outcome of F (which I only can comppute on the original grid) simply trying to find the closest value F(x,y) that would have corresponded to F(x2,y2) (which I cannot compute directly). Anyway, I posted details of my solution in my answer.

Sign in to comment.


Michal Szkup
Michal Szkup on 12 Mar 2023
Given the data I have, I initially tried:
figure;
imagesc(M, 'XData', x, 'YData', y);set(gca,'YDir','normal')
This delivered the figure I attched to the question. The issue is that x-axis and y-axis ticks are not "correct" in the sense that if I look at my data then M switched from 0 to 1 at value of around 0.8 on the vertical axis, but the figure suggest this occurs at value around 0.57.
So I did the following.
% Data:
% x - grid on 1st dimension
% y - grid on 2nd dimension
% M - matrix of values generated evaluated F(x,y) for all combinations of x
% and y
% Obtain data on uniform grid
fig.n1 = 101;
fig.n2 = 101;
fig.x_new = linspace(0,1,fig.n1);
fig.y_new = linspace(0,1,fig.n2);
N = zeros(fig.n1,fig.n2);
for ii = 1:fig.n1
for jj = 1:fig.n2
xx = fig.x(ii);
ind_x = sum(xx>=x);
yy = fig.y(jj);
ind_y = sum(yy>=y);
N(ii,jj) = M(ind_x,ind_y);
end
end
The outcome is
Here the imagesc now shows correct threshold for switch from 0 to 1.
  1 Comment
Star Strider
Star Strider on 12 Mar 2023
I’m having problems following the code and the problems with it. (I don’t understand what you’re doing.)
Also, are ‘fig.x’ and ‘fig.x_new’ not the same?
Is there a specific reason for using structure representations rather than simply vectors?
There appear to be differences between this figire and the earlier figure. I don’t understand where the differences arose, and the reason for preferring one over the other.
It might help to describe the problem and the preferred solution.

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!