Identifying individual points on scatter plot
Show older comments
Hello,
I have the following problem: I need to crossplot two vectors (x and y) and am using scatter for it. Then I need to be able to identify the index of the point on which the user clicked. So, in short the code looks like this:
---------------------------
h=scatter(x,y,S,C); points=get(h,'Children');
for i=1:numel(children)
set(points(i),'HitTest','on','ButtonDownFcn',{'myFunction',i};
end
---------------------------
I was hoping that the order of children is the same as order of elements in x and y. Looks like it is actually reversed. I could live with this, provided that Matlab guarantees that it will be always this way, i.e. that the relationship between the order of children and the order of x,y elements is fixed now and forever. x and y are not odrered, and searching by their values, which can be obtained from the src argument of the callback, is not very palatable: the vectors may be pretty large.
I'd appreciate any ideas.
Thank you,
Naum Derzhi
1 Comment
per isakson
on 4 Jun 2012
Markup, please!
Accepted Answer
More Answers (1)
Walter Roberson
on 4 Jun 2012
0 votes
scatter() creates one child per point, and creates them in order of the elements of the inputs. MATLAB always renders children from last to first, so the first in the list is the top-most for renderers that are sensitive to order of operations (OpenGL has its own rules about what will show on top.) You can count on this for all versions from at least 6.5 until R2012a.
However, there is no promise at all that this will not change in future releases. You can be pretty much certain that it will change, eventually, if MATLAB survives as a product long enough.
Mathworks has changed the mechanisms for drawing some objects. For example bar plots used to generate patches as their immediate output, but now bar plots generate barseries objects instead; barseries objects are members of the hggroup class and they have patches as their children (in current versions anyhow.) Patches that are inside a barseries object act differently than direct patches. Anyhow, this is just one of the examples of Mathworks changing how graphics objects are implemented.
If you need guarantees that MATLAB will have some particular behavior "now and forever" then you will probably have to purchase Mathworks (the entire company!) and impose your restriction -- and if you do you should expect that at some point the product line will fail because it has to maintain that behavior when it no longer makes sense to do so in the context of the rest of the product and the rest of the industry. (Mathworks the company is a privately-held company that has never been for sale, but you could probably get an appointment to discuss the matter if you put down a non-refundadable $3.141 billion deposit. )
4 Comments
Naum Derzhi
on 4 Jun 2012
per isakson
on 5 Jun 2012
One child per point or not? I've added a little test of that to my answer.
Naum Derzhi
on 5 Jun 2012
per isakson
on 5 Jun 2012
I'm confused regarding "one child per point". I use R2012a. Have you executed the test with the seamount data, that I added to my answer? If so, do you get the same result? Do I draw the wrong conclusion from the results of the test? Or does the test miss the point?
Categories
Find more on Graphics Performance in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!