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')
채택된 답변
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
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';

Anitha Limann
2022년 5월 24일
편집: Anitha Limann
2022년 5월 24일
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?
Anitha Limann
2022년 5월 24일
편집: Anitha Limann
2022년 5월 24일
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)
