MATLAB Answers

Identifying individual points on scatter plot

123 ビュー (過去 30 日間)
Naum Derzhi
Naum Derzhi 2012 年 6 月 4 日
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)
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 件のコメント
per isakson
per isakson 2012 年 6 月 4 日
Markup, please!



per isakson
per isakson 2012 年 6 月 4 日
I was about to write: See datatip, but found that it is obsolete.
It is replaces by
Data Cursor Text Can Now Be Programmatically Modified
You can now easily customize the text of datatips. The datacursormode function lets you specify the contents and formatting of text displayed by the data cursor tool. When the data cursor tool is active, you can use its context (right-click) menu to edit or specify the text update function that MATLAB executes to display datatips. For more information, see Data Cursor — Displaying Data Values Interactively in the MATLAB Graphics documentation and datacursormode in the MATLAB Function Reference documentation.
Don't know if that is any good in your scenario.
Figure and Axes have the property CurrentPoint
two-element vector: [x-coordinate, y-coordinate]
Location of last button click in this figure. MATLAB sets this property to the location of the pointer at the time of the most recent mouse button press. MATLAB updates this property whenever you press the mouse button while the pointer is in the figure window.
All the markers in a scatter-plot is one patch - it seems.
With the axes property CurrentPoint it should be possible. There is no snap AFAIK. There might be problems with points close to each other.
--- @Walter ---
This is my refresh Matlab graphic course :-) I use R2012a and I'm not up to speed with the new stuff that has been added during the last few years. And the old stuff I'm about to forget:-) Thank, you for keeping an eye on my answers.
I was convinced that each point in scatter is a separate object. That was certainly(?) the case once. However, I did the test below, which I think shows that all points are parts of one patch. Scatter did have problems with performance.
load seamount % copy&paste from help
scatter(x,y,5,z) % copy&paste from help
h1 = get( gcf, 'Child');
h2 = get( h1, 'Child' );
h3 = get( h2, 'Child' );
disp( [ 'h1 is a ', get( h1, 'Type' ) ] )
disp( [ 'h2 is a ', get( h2, 'Type' ) ] )
disp( [ 'h3 is a ', get( h3, 'Type' ) ] )
whos h*
h1 is a axes
h2 is a hggroup
h3 is a patch
Name Size Bytes Class Attributes
h1 1x1 8 double
h2 1x1 8 double
h3 1x1 8 double
>> get( h3, {'Xdata','YData'} )
ans =
[294x1 double] [294x1 double]
>> whos x y z
Name Size Bytes Class Attributes
x 294x1 2352 double
y 294x1 2352 double
z 294x1 2352 double
  2 件のコメント
Walter Roberson
Walter Roberson 2012 年 6 月 5 日
Interesting, looks like Mathworks has _already_ redesigned scatter()


その他の回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 6 月 4 日
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 件のコメント
per isakson
per isakson 2012 年 6 月 5 日
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?


Community Treasure Hunt

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

Start Hunting!

Translated by