Single value in colors vector causes error in scatter plot

조회 수: 6 (최근 30일)
Fin Cottle
Fin Cottle 2021년 11월 29일
댓글: Fin Cottle 2021년 12월 1일
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

채택된 답변

DGM
DGM 2021년 11월 30일
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])

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by