Bug when plotting 3 points in scatter MATLAB R2022a

16 views (last 30 days)
I have found a bug in the scatter function that I'm not sure how to tackle. If I have a scatter plot with only three elements I get the error: Invalid RGB triplet. Specify a three-element vector of values between 0 and 1. This is because the program thinks I am trying to input an RGB triplet, when I want them to fit within a broader context.
colormap("jet");
X = 1:7;
Y = X;
color_map = X;
scatter(X,Y,45,color_map,"o","filled")
X = 1:3;
Y = X;
color_map = X;
scatter(X,Y,45,color_map,"o","filled")
Warning: Error updating Scatter.

Invalid RGB triplet. Specify a three-element vector of values between 0 and 1.
I can work arround the problem, by plotting each point twice.
X = 1:3;
Y = X;
color_map = X;
if length(X)==3
X = [X,X];
Y = [Y,Y];
color_map = [color_map,color_map];
end
scatter(X,Y,45,color_map,"o","filled")

Accepted Answer

Walter Roberson
Walter Roberson on 15 Sep 2022
Because an RGB triple is permitted at that place, MATLAB needs to have some code to decide whether you are providing RGB or you are providing a vector with one entry per coordinate. The test for the vector length being 3 (RGB) is done first. But [1 2 3] is not valid RGB because the entries for RGB have to be in the range [0 1]
I think it would be even more confusing if MATLAB looked at the range of values and treated the row vector of length 3 differently depending on whether the values were all in the range [0 1] or not.
Historically this situation did not happen because scatter() required that x and y be column vectors, and was explicit that c had to be a column vector if it was one value per coordinate pair.
X = 1:3;
Y = X;
color_map = X(:);
scatter(X,Y,45,color_map,"o","filled")

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!