# How to Plot data groups with different colors?

28 views (last 30 days)
Anitha Limann on 23 May 2022
Commented: Voss on 24 May 2022
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?
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 on 23 May 2022
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')
Voss on 24 May 2022
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).

### Categories

Find more on Colormaps 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!

Translated by