필터 지우기
필터 지우기

Plot matrix with ticks corresponding to the data points

조회 수: 2 (최근 30일)
Michal Szkup
Michal Szkup 2023년 3월 11일
댓글: Star Strider 2023년 3월 12일
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일
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일
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
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일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by