Search code examples
matlabplotjitter

How to draw a jitter plot using 100x11 array in Matlab?


I'm able to draw a plot for out with size 100x11 using below code

x=0:0.1:1; plot(x, out(:,(x*10)+1),'ko');

But I'm unable to figure out how to draw jitter plot for that data.

Below is my complete Matlab code that I'm using to save data into the output variable and trying to plot the data of output using jitter plot.

clc;
close all;
clear all;

heads = 0;
flip_chance = 0.0;
weight = 0.0;
c = 2;
output = zeros(100,11);
for p = 0.1:0.1:1.0

    for i = 1:1:100

        heads = 0;
        flip_chance = 0.0;

        for j = 1:1:10
           weight = rand();
           flip_chance = (p*10)+weight;
           if flip_chance >= 0.5 && weight <= p
              heads = heads + 1; 
           end

        end
        %fprintf ("p : %f , heads : %d\n ", c, heads);
        output(i, c) = heads;

    end
     c = c+1;
end
x=0:0.1:1;
plot(x, output(:,(x*10)+1),'ko');

When I run with the plot I'm getting below output

enter image description here

I trying to get the plot in the below format

enter image description here


Solution

  • A jitter plot is imply a scatter plot like you make but with some noise added to the location of each dot. You can for example use the following code:

    sz = size(output);
    x = repmat(linspace(0,1,sz(2)),sz(1),1);
    plot(x+0.06*rand(sz),output+0.6*rand(sz),'k.')
    

    I'm using x+0.06*rand(sz) because x has increments of 0.1, so this gives a dot spread of 60% of the dot spacing along the x axis. You can change the 0.06 to suit your tastes. The same with output+0.6*rand(sz): 0.6 is 60% of the spacing.

    EDIT

    The "goal" graph you posted uses jitter only in the y axis. You'd leave out the random value added to x to accomplish this. It also seems to use a lot of jitter, merging visually the points for different y-groups. You'd set the random multiplier to at least 1 for such a graph, but I don't recommend it.