How to Plot data groups with different colors?
显示 更早的评论
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')
采纳的回答
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 个评论
Thank you for your answer. This is a great help.
Could you please tell me if it is possible to get a colorbar for this?
You're welcome!
Yes, you can make a colorbar.
% 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 = 0:100:1000;
colors = [ ...
1 1 0; ... % yellow
repmat([0 0 1],2,1); ... % blue
repmat([1 0 0],7,1)]; % red
lon=Data(:,1); lon=360+lon;
lat=Data(:,2);
depth=Data(:,3);n=length(depth);
figure(1);clf;axis equal;hold on;
depth_levels_centers = (depth_levels(1:end-1)+depth_levels(2:end))/2;
c = zeros(size(Data,1),1);
for ii = 1:numel(depth_levels)-1
idx = depth > depth_levels(ii) & depth <= depth_levels(ii+1);
c(idx,:) = depth_levels_centers(ii);
end
scatter(lon,lat,[],c,'filled')
ylabel('latitude (deg)');xlabel('longitude (deg)')
grid minor;
title('Depth distribution')
set(gca(),'Colormap',colors);
cb = colorbar();
clim([0 1000]);
cb.YLabel.String = 'Depth';

My colorbar gives me depth values between 0 and 1. I check my depth values. they all are between 0 and 1000. could you please check this error?
My colorbar gives me depth values between 0 and 1. I checked my depth values. they all are between 0 and 1000. could you please check this error?
I used the first method you posted.
Data=load('Depth.txt','-ascii');
lon=Data(:,1); lon=360+lon;
lat=Data(:,2);
depth=Data(:,3);n=length(depth);
dgroup=[100 300 600];
dgroup=[-inf dgroup inf];
colors=[0.8500 0.3250 0.0980;1 1 0;0 1 0;0 0 1];
gray=[0.5 0.5 0.5];
% for each depth range ...
for ii = 1:numel(dgroup)-1
% get a logical index of the data points in that range
idx = depth > dgroup(ii) & depth <= dgroup(ii+1);
% and plot them with the corresponding color
plot(lon(idx),lat(idx),'o','MarkerSize',5,'MarkerFaceColor',colors(ii,:),'MarkerEdgeColor',gray);
end
ylabel('latitude (deg)');xlabel('longitude (deg)')
grid minor;
title('Depth distribution')
set(gca(),'Colormap',colors);
cb = colorbar();
cb.YLabel.String = 'depth';
Don't forget this line, toward the end:
clim([0 1000]);
Also, I changed some other stuff, to get the colorbar to work, like setting the ColorMap of the axes.
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
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 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Subplots 的更多信息
产品
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
