1

I have some problems with a scatter plot.

I am plotting a matrix containing grades per assignment for students e.g. [assignments x grades], but if more than one student gets the same grade in the same assignment, the points will be on top of each other. I want to add a small random number (between -0.1 and 0.1) to the x- and y-coordinates of each dot.

On the x-axis it should be number of assignments and on the y-axis it should be all the grades.

the grades matrix is defined as a 12x4 matrix

My code looks like this:

n_assignments = size(grades,2);  % Total number of assignments.
n_students = size(grades,1);    % Total number of student.
hold on;                    % Retain current plot when adding new plots.
for i = 1:n_assignments     % Loop through every assignment.
% Scatter plot of assignment vs grades for that assignment.
% One assignment on every iteration.
scatter(i*ones(1, n_students), grades(i, :), 'jitter', 'on', 'jitterAmount', 0.1);
end
hold off;                   % Set the hold state to off.
set(gca, 'XTick', 1:n_assignments); % Display only integer values in x-axis.
xlabel('assignment');       % Label for x-axis.
ylabel('grades');           % Label for y-axis.
grid on;                    % Display grid lines.

But I keep getting the error message:

X and Y must be vectors of the same length.
Ryan
  • 53
  • 6

1 Answers1

3

Please note that the scatter plot jitter is an undocumented feature. You can also have semi-transparent markers in line and scatter plots, which could be another alternative to solve your current problem.

I will cover the scatter 'jitter' feature in this answer.

Note that 'jitter' only affects the x-axis but not the y-axis (more info on Undocumented Matlab).

Have a look at this example I made based on your description:

Suppose you have a class with 20 students and they have completed 5 assignments. The grades for the assignments are stored in a matrix (grades) where the rows are the assignments and the columns are the students.

Then I simply generate a scatter plot of the data in the grades matrix, one row at a time, in a for loop and using hold on to keep all the graphics on the same figure.

n_assignments = 5;  % Total number of assignments.
n_students = 20;    % Total number of students.

grades = randi(10, n_assignments, n_students);  % Random matrix of grades.

hold on;                    % Retain current plot when adding new plots.
for i = 1:n_assignments     % Loop through every assignment.
    % Scatter plot of assignment vs grades for that assignment.
    % One assignment on every iteration.
    scatter(i*ones(1, n_students), grades(i, :), 'jitter', 'on', 'jitterAmount', 0.1);
end
hold off;                   % Set the hold state to off.

set(gca, 'XTick', 1:n_assignments); % Display only integer values in x-axis.
xlabel('assignment');       % Label for x-axis.
ylabel('grades');           % Label for y-axis.
grid on;                    % Display grid lines.

This is the result:

Scatter plot with jitter


If you still want to add jitter in the y-axis, you would have to do that manually by adding random noise to your grades data, which is something I personally wouldn't recommend, because the grades in the scatter plot could get mixed, thus rendering the plot completely unreliable.

Graham
  • 7,431
  • 18
  • 59
  • 84
codeaviator
  • 2,545
  • 17
  • 42
  • It's exactly what I wanted to, but, when I try to write so its more generel I can't get it to work properly, it doesn't plot all the grades. I have made following changes in the code: `grades is difined as a 12x4 matrix n_assignments = size(grades,2); n_students = size(grades,1); hold on; for i = 1:n_assignments scatter(i*ones(1, n_students), grades(i, :), 'jitter', 'on', 'jitterAmount', 0.1); end hold off; set(gca, 'XTick', 1:n_assignments);` But it gives me the error message: Error using scatter (line 61) X and Y must be vectors of the same length. – Ryan May 04 '17 at 13:01
  • @Ryan Please, can you edit your question and add the new piece of code underneath (without deleting what you already wrote). Comments are generally not the ideal place to share code. Thanks. – codeaviator May 04 '17 at 13:05