Colormap a plot based on value of (x,y)

5 views (last 30 days)
Matthew
Matthew on 16 Jun 2024
Answered: Manikanta Aditya on 3 Jul 2024
Hello,
I have a table T of dimensions x,y where each point has a value between -100 and 100.
I want to graph this data such that T(1,1) is point 1,1 on the graph and the color of that point is determined by the value of T(1,1)
I included a picture of something similar to what I want my plot to look like, along with some code that generates an example table for graphing
Thanks in advance for any help you can provide.
clear
clc
close all
%This will generate a table with example data.
x=100
y=50
data=zeros(y,x);
data(1,1)=50;
dchartdx=25/x;
dchartdy=25/y;
for ii=1:x
data(1,ii+1)=data(1,ii)+dchartdx;
end
for ii=1:x+1
for ij=1:y
data(ij+1,ii)=data(ij,ii)+dchartdy;
end
end
  1 Comment
DGM
DGM on 16 Jun 2024
Try imagesc(). If you want explicit control over the colormap limits, use clim().
imagesc(data) % display the data with the colormap ends corresponding to the data extrema
colorbar % add a colorbar
colormap(jet(256)) % specify a colormap

Sign in to comment.

Answers (1)

Manikanta Aditya
Manikanta Aditya on 3 Jul 2024
Hi,
The image you've provided shows a depth vs range plot, likely representing some kind of acoustic or seismic data.
To create a similar plot using MATLAB with your data, you can use the 'imagesc' function as mentioned by @DGM.
Here's the script with the changes:
clear
clc
close all
% Generate example data
x = 100;
y = 50;
data = zeros(y, x);
% Fill the data array (your existing code)
data(1,1) = 50;
dchartdx = 25/x;
dchartdy = 25/y;
for ii = 1:x
data(1,ii+1) = data(1,ii) + dchartdx;
end
for ii = 1:x+1
for ij = 1:y
data(ij+1,ii) = data(ij,ii) + dchartdy;
end
end
% Create the plot
figure;
imagesc(1:x, 1:y, data); % display the data with the colormap ends corresponding to the data extrema
colormap(flipud(hot)); % specify a colormap
colorbar; % add a colorbar
% Set axis labels and title
xlabel('Range (km)');
ylabel('Depth (m)');
title('Data Visualization');
% Invert the y-axis to have (1,1) at the top-left corner
set(gca, 'YDir', 'reverse');
% Adjust aspect ratio if needed
axis equal tight
I hope this helps!

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!