When creating a swarmchart from categorical X and double Y values, is there a way to extract the resulting jittered X values?

6 views (last 30 days)
s = random_swarms(2^10);
jitteredXvals = [] % WHAT GOES HERE?
jitteredXvals = []
function [s, x, y] = random_swarms(n)
% Input:
% n: number of data points
% Outputs:
% s: handle to the swarmchart
% x: categorical variable ('A' or 'B')
% y: random numbers
% Generate random categorized values
categories = {'A', 'B'};
x = categorical(categories(randi(2, n, 1)));
y = randn(n, 1);
% Create swarmchart
s = swarmchart(x, y);
% Set title and labels
title('Random Swarmchart')
xlabel('Category')
ylabel('Random Value')
end
  5 Comments
Jim
Jim on 10 Oct 2024
One straightforward use case is to move those values into another program, such as Excel, where generating a nicely jittered graph is a PITA.
The one I am actually experimenting with involves drawing a "cutoff" at different levels of the "cloud" and having it expand and shrink as it moves up and down—purely aesthetic.
Additionally, what if I want to identify a specific point or set of points in that graph for some reason and highlight it somehow? I would need to know where the points of interest are actually plotted to do so.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 9 Oct 2024
Edited: Walter Roberson on 9 Oct 2024
ss = struct(s);
ss.XYZJittered
Note: the default setting for swarmchart is Jitter = 'off'
  1 Comment
Jim
Jim on 9 Oct 2024
I love the warning it gives:
Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should thus be avoided. Use DISP or DISPLAY to see the visible public details of an object. See 'help struct' for more information.
It's like...don't do this! But yup, that works!
Thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!