imagesc color map for gridded value
4 views (last 30 days)
Show older comments
Hi all,
I have a set of three variables of a big data set in a given grid (see below) and I would like to plot Z variables as color scale using imagesc. Could you help me how I can plot?
X = 1:01:5
Y =5:01:8
Z = 1,5,0,10,......
length(X)=length(Y)=length(Z).
0 Comments
Answers (1)
Ayush
on 11 Jul 2024
Hi,
To plot the Z variable as a colour scale using "imagesc" in MATLAB, you need to ensure that Z is in a matrix form that corresponds to the grid defined by X and Y. If Z is a vector, you need to reshape it into a matrix that matches the dimensions of the grid defined by X and Y. Refer to an example code below for better understanding:
% Define the variables
X = 1:1:5;
Y = 5:1:8;
Z = [1, 5, 0, 10, 2, 3, 4, 7, 6, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19];
% Ensure Z is reshaped into a matrix form
% Here, assuming Z corresponds to a 5x4 grid
Z_matrix = reshape(Z, [length(Y), length(X)]);
% Plot using imagesc
imagesc(X, Y, Z_matrix);
% Set axis properties
set(gca, 'YDir', 'normal'); % To have Y-axis in the correct direction
colorbar; % Display color scale
xlabel('X-axis');
ylabel('Y-axis');
title('Z variable color scale plot');
For more information on the "imagesc" function, refer to the below documentation:
1 Comment
DGM
on 11 Jul 2024
This isn't what the question was asking -- at least not directly. Preparing the data is the core of the problem.
As per the question,
length(X) = length(Y) = length(Z)
So all inputs are equal-length vectors specifying scattered data. Simply reshaping Z doesn't work. The data needs to be interpolated onto a grid using griddata() or scatteredInterpolant().
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!