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 件のコメント
Anitha Limann
2022 年 5 月 23 日
Thank you for your answer. This is a great help.
Anitha Limann
2022 年 5 月 23 日
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
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 件)
カテゴリ
ヘルプ センター および File Exchange で Subplots についてさらに検索
製品
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
