"1D" scatter plot without x-axis

71 views (last 30 days)
Daniele Gerosa
Daniele Gerosa on 25 May 2020
Commented: Daniele Gerosa on 26 May 2020
Hi all,
I would like to plot groups of scattered dots like in the pictures attached below. I specify the y coordinates, while the x coordinates can be arbitrary as long as the grouped structure is highlighted (and they also should be different, at least within the same group of points). The groups should be placed in the same "column" like in the first picture or in "separate columns" like in the second (maybe with some dashed line that vertically separates one "column" from the other). It would be nice to see code for both options.
Thanks.
  1 Comment
Christophe
Christophe on 25 May 2020
You can hide XTick by assigning an empty element.
figure;
h = axes;
h.XTick = [];

Sign in to comment.

Accepted Answer

Tommy
Tommy on 26 May 2020
If you only have one-dimensional data but you want to display it on two axes, you'll have to tell MATLAB where (i.e. if you have the y data, you need to pick corresponding x data). Also, if you want clusters to be scattered in a circular shape, rather than along vertical lines, you could jitter the x data to add some variation along the x axis.
Here is one example which centers each cluster of data at its mean y value:
% random data
data{1} = 16 + randn(10,1);
data{2} = 10 + randn(10,1);
data{3} = 4 + randn(10,1);
c = {'k','r','g'}; % colors
% separated
ax = axes(figure, 'NextPlot', 'add', 'XColor', 'none');
for i = 1:numel(data)
y = data{i};
x = mean(y) + std(y)*randn(numel(y),1); % x data have same mean and standard deviation as y data
scatter(ax, x, y, c{i}, 'MarkerFaceColor', c{i})
end
Here is another example which stacks the clusters. The main difference is that each cluster here will be centered around x = 0, rather than x = mean(y).
% random data
data{1} = 16 + randn(10,1);
data{2} = 10 + randn(10,1);
data{3} = 4 + randn(10,1);
c = {'k','r','g'}; % colors
% stacked
ax = axes(figure, 'NextPlot', 'add', 'XColor', 'none');
for i = 1:numel(data)
y = data{i};
x = std(y)*randn(numel(y),1); % x data have same standard deviation as y data but mean of 0
scatter(ax, x, y, c{i}, 'MarkerFaceColor', c{i})
end
xlim(ax, ax.XLim + [-0.1 0.9]*range(ax.XLim));
Of course, you could determine the x values any way you want. They don't need to be drawn from a normal distribution. Both of these examples would plot two clusters on top of each other if the clusters happen to have a similar mean.
  1 Comment
Daniele Gerosa
Daniele Gerosa on 26 May 2020
Hi Tommy, it seems like the code is producing the exact output I had in mind. Thank you very much!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!