Identify exact jittered point from swarmchart plot

7 次查看(过去 30 天)
I created a horizontal swarmchart in app designer. The input data have numeric values and grouping tags (intGroups) associated with each point. Each point's color is associated with its group. I also defined a ButtonDownFcn to get the point that the user clicks on (using code from a previous Matlab Answer).
The issue is that when I click a point, swarmchart's XData and YData give the undithered coordinates (i.e., the y-values are all the same). So if I click a point that is far from center (due to a lot of points having that same value), the code below may or may not identify that point I clicked, so I may not get the correct group.
Is there a way to ensure that I get the exact point I clicked (or its index in the vector), not others that are in the same vicinity?
for zz = 1:length(intVarsUnique)
hPlot = swarmchart(axPlot, xData(:, zz), intVars(:, zz), [], categorical(strGroupColors), 'filled', 'o', ...
'YJitter','density', ...
'HitTest', 'on', 'ButtonDownFcn', @(src, eventData) fcnPlot_ButtonDown(app, src, eventData));
if ~ishold(axPlot)
hold(axPlot, 'on');
end
end % zz
hold(axPlot, 'off');
function fcnPlot_ButtonDown(app, hPlot, eventData)
% Based on code from Yair Altman in https://www.mathworks.com/matlabcentral/answers/1903190-get-data-point-that-was-clicked-on-in-graph
% Get the click coordinates from the click event data
x = eventData.IntersectionPoint(1);
y = eventData.IntersectionPoint(2);
line_xs = hPlot.XData;
line_ys = hPlot.YData;
dist2 = (x-line_xs).^2 + (y-line_ys).^2;
[~,min_idx] = min(dist2);
closest_x = line_xs(min_idx);
closest_y = line_ys(min_idx);
msgbox(sprintf('Clicked on: (%g,%g)\nClosest pt: (%g,%g)', x, y, closest_x, closest_y));
% Code to locate point in vector and ID group...
end
  5 个评论
Yair Altman
Yair Altman 2025-9-9
编辑:Yair Altman 2025-9-9
Undocumented properties are often protected or private, not just hidden, and in such cases you can't access them directly using the base object. Instead, you must use the struct function, i.e.
struct(hSW(1)).XYZJittered
dpb
dpb 2025-9-9
编辑:dpb 2025-9-10
AHA! I had overlooked could do that, thanks for pointing it out. Glad that I persisted... <g>
However, another case of so many where Mathworks seems to have a penchant for hiding things from the end user that clearly would/could be of use to the user. Having to go to such lengths to discover such is very counter-productive in terms of the end-user effort.

请先登录,再进行评论。

采纳的回答

Yair Altman
Yair Altman 2025-9-9
The answer lies in the pair of undocumented properties XYZJitter and XYZJittered. You can see these properties if you inspect the hPlot handle using my getundoc utility (File Exchange download; technical description) or via the built-in struct function, as shown below:
hStruct = struct(hPlot(1)); % uses built-in struct function, displays a nasty warning message
xyzData = hStruct.XYZJittered;
hProps = getundoc(hPlot(1)); % uses getundoc utility, no warning message
xyzData = hProps.XYZJittered;
  • hPlot(1).XYZJitter provides the jitter amount in each of the 3 axes (a 500x3 numeric matrix in your case)
  • hPlot(1).XYZJittered provides the jittered result in each of the 3 axes (a 500x3 numeric matrix in your case). This is the same as [hPlot(1).XData', hPlot(1).YData', hPlot(1).ZData'] + hPlot(1).XYZJitter (when ZData is empty you need to make the appropriate corrections of course).
If you use the XYZJittered property instead of XData,YData, your code should work as expected without any additional changes.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

产品


版本

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by