One of my blog readers, Henrik Toft, reported an undocumented behavior of the well-known scatter plot function: scatter returns a handle to an hggroup-type object that contains child handles of the data points (patch-type objects). This enables easy manipulation of specific points (for example, changing the color or marker of interesting data points).
The undocumented behavior is that when the scatter plot contains more than 100 points, the returned hggroup object only has a single child – a handle to a unified patch object that contains all the data points. This prevents customization of specific points: color can be customized via the cdata property, but this is not trivial; the marker cannot be customized at all.
Both Henrik and I initially thought this was a simple bug. But when Henrik reported this issue to MathWorks support, he got the response that this is indeed the expected behavior (i.e., not a bug but a feature), which is not documented. MathWorks did not say whether this was because they didn’t think users need more than 100 child handles, or perhaps an issue with memory/performance. Whatever the reason, I feel that this behavior/feature should at least be documented:
>> hggroup = scatter(rand(99,1),rand(99,1)); >> size(get(hggroup,'Children')) ans = 99 1 >> hggroup = scatter(rand(100,1),rand(100,1)); >> size(get(hggroup,'Children')) ans = 100 1 >> hggroup = scatter(rand(101,1),rand(101,1)); >> size(get(hggroup,'Children')) ans = 1 1 |
Workaround: use the ‘v6’ option. Unfortunately, this option is deprecated and marked as obsolete by Matlab, with the warning that it will be removed in a future Matlab version. I hope this will not be done until a better workaround for the above-reported issue is implemented in the scatter function:
>> hPatches = scatter('v6',rand(101,1),rand(101,1)); Warning: The 'v6' argument to SCATTER is deprecated, and will no longer be supported in a future release. > In usev6plotapi>warnv6args at 84 In usev6plotapi at 40 In scatter at 39 >> size(hPatches) ans = 101 1 |
If you have detected any other undocumented behavior, function or feature in Matlab, please report them in the comments section below, or directly by email to me: altmany at gmail dot com.
Interesting. I use scatter with thousands of points all the time. Perhaps I am misusing it then? I don’t really need the ability to access each data marker but scatter is just an easy function to work with.
Naor – Perhaps I should have been clearer: scatter works correctly even with 101+ data points, as you correctly said. However, the returned value in this case is simply a single patch handle rather than hundreds or thousands of separate data-point handles. If you don’t need to customize specific data points then this issue doesn’t really matter; but if you do need this ability, you need to use the obsolete ‘v6’ option.
Hi Yair,
In my GUI, I have to add/delete/adjust lot of graphs. In matlab compiler documentation, PLOTTOOLS is stated under List of Unsupported Functions.
Is there any way to get PLOTTOOLS working with the compiled code of standalone EXE?
Venkat – Interesting question. Unfortunately, I do not know if it can be done or how.
I do not like the method of using deprecated functions – they might just disappear the next release.
I suggest using something like this:
By default, it is plotted in different colours, but that can be easily overcome.
Sebastiaan
[…] Charting Matlab’s unsupported hidden underbelly « Undocumented scatter plot behavior […]
Hi
This article has been very useful for me! I struggled few hours with this before I found this. Since I need to manipulate the individual points in a specific order it is worth noting that the order in, which the handle is returned with the ‘v6’ option is exactly the opposite of what the ‘Children’ property returns meaning that the first object is on top instead of on the bottom. I hope this helps to others and I have to say it is very disappointing Matlab decided to do this in such manner…
Boyan
Thanks a lot! It took me quite a while to figure out why my functions suddenly stopped working.
Again, thanks! you made my day.
[…] You may also be interested in the article I posted a few years ago about another undocumented scatterplot behavior […]