How to plot points on map axes with varying colors using scatterm?
조회 수: 18 (최근 30일)
이전 댓글 표시
I have three vectors: lat, lon, depth. They are of equal length. When I run the code below, I get an error saying: Error using scatter
Color must be one RGB triplet, an m-by-3 matrix of RGB triplets with one color per scatter point, or an m-by-1 vector with one value per scatter point.". I don't understand, because my depth vector, which is being used for color, is the same size as my latitude vector.
%Load in data
data = readmatrix("Lin14_cores.csv","NumHeaderLines",1);
lon = data(:,1);
lat = data(:,2);
depth = data(:,4);
%Plot datapoints on map axes with colors showing depth
load coastlines
ax = axesm("MapProjection","mollweid","MapLatLimit",[-60 60], "MapLonLimit",[-180 180], 'Frame', 'on');
plotm(coastlat,coastlon)
scatterm(ax, lat,lon, ones(length(lat))*50, depth);
colormap("autumn")
Can anyone help me fix this.
댓글 수: 0
채택된 답변
Malay Agarwal
2023년 6월 16일
Hi Samuel,
I think the issue is with the way the depth array is being passed. When you're passing colors as a m-by-1 vector, each value can be one of the supported values specified here: Project scatter plot on axesm-based map - MATLAB scatterm. You cannot use the m-by-1 format with RGB values. If you want to do that, you should be passing in a m-by-3 matrix of RGB triplets where each value in the triplet is between 0 and 1. For example, the code below will create the attached plot:
data = readmatrix("Lin14_cores.csv","NumHeaderLines",1);
[m, ~] = size(data);
lon = data(:,1);
lat = data(:,2);
% Red color
color = [1 0 0];
% Repeat the vector m times to get an m-by-3 matrix
colors = repmat(color, m, 1);
load coastlines
ax = axesm("MapProjection","mollweid","MapLatLimit",[-60 60], "MapLonLimit",[-180 180], 'Frame', 'on');
plotm(coastlat,coastlon)
% Notice how the last argument is colors instead of depth
scatterm(ax, lat,lon, ones(length(lat))*50, colors);
colormap("autumn")
In your case, you'll have to figure out a way to convert the depth values to appropriate triplets.
댓글 수: 0
추가 답변 (1개)
Manas
2023년 6월 16일
Hi Samuel,
Can you provide the coastlines file?
Also, you can checkout this answer and try out if it works for you
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Scatter Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!