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

조회 수: 5 (최근 30일)
Matthew
Matthew 2024년 6월 16일
답변: Manikanta Aditya 2024년 7월 3일
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