Single value in colors vector causes error in scatter plot
5 views (last 30 days)
Show older comments
Hi,
I have the following code that works perfectly, the NaN values in the colors vector are skipped as desired:
>> colors = [NaN, 121, NaN, 150]
colors =
NaN 121 NaN 150
>> scatter([1,2,3,4], [1,2,3,4], 50, colors, 'filled');
which produces the following graph:
However, when i replace a value in the colors array with NaN it all goes wrong:
>> colors = [NaN, 121, NaN, NaN]
colors =
NaN 121 NaN NaN
>> scatter([1,2,3,4], [1,2,3,4], 50, colors, 'filled');
Warning: Error updating Scatter.
The logical indices contain a true value outside of the
array bounds.
Which stops the scatter graph from being plotted, it seems to happen when the vector is all NaN but one value.
Why does simply changing one of the values to NaN cause this error?
It doesn't matter which value is changed, but as soon as there is a single value surrounded by NaN's it will break.
Thanks in advance for any suggestions
0 Comments
Accepted Answer
DGM
on 30 Nov 2021
The code you give works fine in the newest version that I have (R2019b), but not in R2021b. I'm not exactly sure what changed, so I can't do much to troubleshoot that, but I don't know that there's a need.
If you want to mask off points, it's probably better to simply omit the X and Y data directly. The input for the color property is subject to a bunch of conditional operations and scaling, so it kind of makes sense that it might explode if it only has one valid numeric value in the vector.
x = 1:4;
y = 1:4;
colors = [NaN, 121, NaN, NaN];
mk = ~isnan(colors);
scatter(x(mk), y(mk), 50, colors(mk), 'filled');
colorbar
caxis([1 256])
More Answers (0)
See Also
Categories
Find more on Colormaps 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!