Plot matrix with ticks corresponding to the data points

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 件のコメント

Dyuman Joshi
Dyuman Joshi 2023 年 3 月 11 日
What values do you want to show on x and y ticks? Values of 1st row and 1st column respectively?
If other, then please specify.
Michal Szkup
Michal Szkup 2023 年 3 月 11 日
Values of x and y, respectively so that where matrix changes from 0 to 1 corresponds to actual values of x and y, not indices in the matrix.
Dyuman Joshi
Dyuman Joshi 2023 年 3 月 11 日
Can you attach the data for x and y?
Michal Szkup
Michal Szkup 2023 年 3 月 11 日
I just attached the data for x and y.

サインインしてコメントする。

回答 (2 件)

Star Strider
Star Strider 2023 年 3 月 11 日
編集済み: Star Strider 2023 年 3 月 11 日

0 投票

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 件のコメント

Michal Szkup
Michal Szkup 2023 年 3 月 11 日
Thank you, but that's not exactly what I was looking for. I figured out a solution to my problem, though, and will post it a bit later when I have time. Basically, I construct a new matrix N uniform grid, where I evaluate whether the outcome is 0, 1, or 2, based on my non-uniform grid (in x and y) and matrix of outcomes M. And then I plot N using imagesc.
I attach the result to show the difference because it seems it was non clear what the issue I was trying to solve is. As you can see compare imagesc based on M and on the new matrix N, in the latter the regions are shifted (and this reflect the true size of those regions).
Star Strider
Star Strider 2023 年 3 月 12 日
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 2023 年 3 月 12 日
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.

サインインしてコメントする。

Michal Szkup
Michal Szkup 2023 年 3 月 12 日

0 投票

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 件のコメント

Star Strider
Star Strider 2023 年 3 月 12 日
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.

サインインしてコメントする。

カテゴリ

製品

リリース

R2019b

タグ

質問済み:

2023 年 3 月 11 日

コメント済み:

2023 年 3 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by