How to plot points on map axes with varying colors using scatterm?
11 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Malay Agarwal
on 16 Jun 2023
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 Comments
More Answers (1)
Manas
on 16 Jun 2023
Hi Samuel,
Can you provide the coastlines file?
Also, you can checkout this answer and try out if it works for you
0 Comments
See Also
Categories
Find more on Scatter Plots 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!