"1D" scatter plot without x-axis
43 次查看(过去 30 天)
显示 更早的评论
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/300873/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/300878/image.png)
1 个评论
采纳的回答
Tommy
2020-5-26
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!