Adding a random value (-0.1 : 0.1) to scatterplot data points to prevent exact overlapping.

조회 수: 3 (최근 30일)
Hello.
Ive been tasked with generating a scatterplot for grading students' assignments. The variable 'grades' is loaded from a .csv file, which ill be attaching to this post.
I've managed to make the loop work, generating the proper scatterplot, but I am supposed to apply a random value between -0.1 : 0.1 to the datapoints, IF there are multiple plotted in the same spot. The random value in this interval MUST be added to both the x- and y-value for each datapoint that overlaps. I simply do not know how to do that.
grades = readtable(grades);
G = zeros(height(grades), width(grades)-2);
for i = 3 : width(grades)
G(:,(-2+i)) = table2array(grades(:,i));
end
AS = [1: size(G,2)];
figure()
hold on
for i = 1 : size(G,1)
Gd = zeros(1, size(G,1));
Gd = G(i,:);
scatter(AS, Gd);
end
xlim([0 size(grades,2)-1]), xticks([1:1:size(grades,2)-2]),
ylim([-5 14]), yticks(Scale),
title('Students grading on assignments'), xlabel('Assignment'),
ylabel('Grade');
hold off
Is there any way to quickly and/or easily implement that change in my code? The attached code is part of a function script (more specifically, the 2nd section of the script). In case it is needed in order to assist me, the full script is attached as well.
function gradesPlot(grades)
close all
Scale = [-3 0 2 4 7 10 12];
G = computeFinalGrades(grades);
A = [sum(G(:) == -3) sum(G(:) == 0) sum(G(:) == 2) sum(G(:) == 4) ...
sum(G(:) == 7) sum(G(:) == 10) sum(G(:) == 12)];
figure()
hold on
bar(Scale, A);
ylim([0 max(A)+1]), yticks(0:1:max(A)+1), xlim([-5 14]),
xticks(Scale), title('Students grading'), xlabel('Grade'),
ylabel('Amount of students');
hold off
% -------------------------------------------------------------------------
grades = readtable(grades);
G = zeros(height(grades), width(grades)-2);
for i = 3 : width(grades)
G(:,(-2+i)) = table2array(grades(:,i));
end
AS = [1: size(G,2)];
figure()
hold on
for i = 1 : size(G,1)
Gd = zeros(1, size(G,1));
Gd = G(i,:);
scatter(AS, Gd);
end
xlim([0 size(grades,2)-1]), xticks([1:1:size(grades,2)-2]),
ylim([-5 14]), yticks(Scale),
title('Students grading on assignments'), xlabel('Assignment'),
ylabel('Grade');
hold off
% -------------------------------------------------------------------------
Gm = zeros(1, size(G,2));
for i = 1 : size(G,2)
Gm(:,i) = mean(G(:,i));
end
figure()
hold on
plot(AS,Gm)
xlim([0 size(grades,2)-1]), xticks([1:1:size(grades,2)-2]),
ylim([-5 14]), yticks(Scale),
title('Average grading on assignments'), xlabel('Assignment'),
ylabel('Average grade');
hold off

답변 (1개)

OCDER
OCDER 2018년 6월 14일
Try this example to see how to adjust X and Y values by a random value between -0.1 to 0.1 only if (x,y) are duplicates.
X = [1 1 2 2 3 3 4 4]';
Y = [1 1 3 4 5 5 6 7]'; %(x,y) = (1,1) and (3,5) have overlapping points
Xadj = zeros(size(X)); %store values to adjust x
Yadj = zeros(size(Y)); %store values to adjust y
[~, ~, Idx] = unique([X Y], 'rows'); %find unique (x,y) coordinates
for j = 1:length(Idx)
Loc = (Idx == j);
Copies = sum(Loc);
if Copies > 1
Xadj(Loc) = 0.2*rand(Copies, 1) - 0.1; %add values from -0.1 to 0.1
Yadj(Loc) = 0.2*rand(Copies, 1) - 0.1; %add values from -0.1 to 0.1
end
end
scatter(X+Xadj, Y+Yadj) %Plot X and Y, adjusting values if they overlap.
%NOTE: Adjustment could still be too small that overlaps happen.
You would need to set a minimum adjustment value then.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by