Plot matrix with ticks corresponding to the data points
2 views (last 30 days)
Show older comments
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
Answers (2)
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';
EDIT — (11 Mar 2023 at 23:39)
Re-plotted ‘M’ using the recerntly-provided ‘x’ and ‘y’ vectors.
.
3 Comments
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
on 12 Mar 2023
1 Comment
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.
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!