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

5 次查看(过去 30 天)
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 个评论
DGM
DGM 2024-6-16
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

请先登录,再进行评论。

回答(1 个)

Manikanta Aditya
Manikanta Aditya 2024-7-3
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!

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by