Plotting a categorized dot (scatter?) plot

10 views (last 30 days)
Sam
Sam on 12 Jan 2014
Answered: Samayochita on 26 Mar 2025
I am trying to plot the values in a matrix M such that each column in M is plotted along a distinct vertical line on the x axis. Ideally, I could name each cluster of values with a string (much like a bar or boxplot)
Unfortunately I don't know the exact name of the plot I am trying to recapitulate but it seems like the closest alternative matlab has is the boxplot. If anyone knows the name of the plot I am trying to create, a function capable of doing this or an effective work around, I would be highly appreciative.

Answers (1)

Samayochita
Samayochita on 26 Mar 2025
Hi Sam,
I think the type of plot you are looking for here is the "strip plot" or "dot plot." A strip plot helps us visualize the distribution of data points within each category.
In MATLAB, we can achieve this using a combination of the “scatter function and some adjustments to position the points correctly. Here is an example of how you could create such a plot:
1) Define the matrix M that contains the data. Each column in M represents a different category.
M = randn(100, 3);
2) Create a cell array containing the category names. These names will be used as labels on the x-axis.
categories = {'Category 1', 'Category 2', 'Category 3'};
% Number of categories
numCategories = size(M, 2);
3) Plot each category by using a for loop to iterate over each column of the matrix M.
  1. Use “jitterfunction to avoid overlapping points, generate a small random jitter for the x-axis positions.
  2. Use the “scatter function to plot the data points.
for i = 1:numCategories
jitter = (rand(size(M, 1), 1) - 0.5) * 0.2;
scatter(i + jitter, M(:, i), 'filled');
hold on;
end
4) Set X-ticks and X-tick labels.
% Set x-ticks to be at the center of each category
set(gca, 'XTick', 1:numCategories, 'XTickLabel', categories);
xlabel('Categories');
ylabel('Values');
title('Categorized Dot Plot');
% Adjust axis limits for better visualization
xlim([0.5, numCategories + 0.5]);
For more information on the “jitter” and “scatter” functions, please refer to the following documentation:
I hope this answer was helpful.

Community Treasure Hunt

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

Start Hunting!