필터 지우기
필터 지우기

How to Plot data groups with different colors?

조회 수: 31 (최근 30일)
Anitha Limann
Anitha Limann 2022년 5월 23일
댓글: Voss 2022년 5월 24일
I have latitude, longitude and depth data. which I plotted them as below;
These depth data range between 0 -1000km.
I want each 'circle' (data point) to have different Markerface colours based on the depth group; such as 0-100 --> yellow; 101-300 --> blue; 301-1000 --> red. Could someone please help me with how to do this?
Data=load('Depth.txt','-ascii');
lon=Data(:,1); lon=360+lon;
lat=Data(:,2);
depth=Data(:,3);n=length(depth);
figure(1);clf;axis equal;hold on;
plot(lon,lat,'o');
ylabel('latitude (deg)');xlabel('longitude (deg)')
grid minor;
title('Depth distribution')

채택된 답변

Voss
Voss 2022년 5월 23일
One way is to plot one line per depth interval, each with a different Color/MarkerFaceColor:
% first, I make up some random data since I don't have your text file:
Data = [-360*rand(100,1) 180*rand(100,1)-90 1000*rand(100,1)];
% define the depth levels and colors, for plotting data in different
% depth ranges with different colors:
depth_levels = [100 300];
colors = [ ...
1 1 0; ... % yellow
0 0 1; ... % blue
1 0 0]; % red
lon=Data(:,1); lon=360+lon;
lat=Data(:,2);
depth=Data(:,3);n=length(depth);
figure(1);clf;axis equal;hold on;
% put -Inf and Inf around depth_levels, to avoid edge effects
depth_levels = [-Inf depth_levels Inf];
% for each depth range ...
for ii = 1:numel(depth_levels)-1
% get a logical index of the data points in that range
idx = depth > depth_levels(ii) & depth <= depth_levels(ii+1);
% and plot them with the corresponding color
plot(lon(idx),lat(idx), ...
'LineStyle','none', ...
'Color',colors(ii,:), ...
'Marker','o', ...
'MarkerFaceColor',colors(ii,:));
end
ylabel('latitude (deg)');xlabel('longitude (deg)')
grid minor;
title('Depth distribution')
Alternatively, you can use scatter with the optional color input:
figure(2);clf;axis equal;hold on;
% initialize a matrix of colors to be used in scatter
c = zeros(size(Data,1),3);
% for each depth range ...
for ii = 1:numel(depth_levels)-1
% get a logical index of the data points in that range
idx = depth > depth_levels(ii) & depth <= depth_levels(ii+1);
% set those rows of c to the corresponding color
c(idx,:) = colors(ii*ones(nnz(idx),1),:);
end
scatter(lon,lat,[],c,'filled')
ylabel('latitude (deg)');xlabel('longitude (deg)')
grid minor;
title('Depth distribution')
  댓글 수: 8
Anitha Limann
Anitha Limann 2022년 5월 24일
I am so sorry, When I tried with clim it says,
Unrecognized function or variable
'clim'.
Error in Untitled (line 33)
clim([0 1000]);
I also do not understand 'repmat' function and how to use it. Please help me if you could.
Thank you
Voss
Voss 2022년 5월 24일
Apparently in some versions of MATLAB clim is called caxis, so try
caxis([1 10000]);
Regarding repmat, its purpose in general is to replicate a matrix (or repeat a matrix) in a specified way. In this case, I'm using repmat to build the colormap, which needs to have red repeated 7 times (since red covers depths fro 300 to 1000) and blue repeated 2 times (since blue covers depths from 100 to 300).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by