Search code examples
matlabmatlab-figure

How i plot probability distribution of each observation as a colour plot?


Lets say I have following eleven values:

>> values=[100:10:200]

values =

   100   110   120   130   140   150   160   170   180   190   200

Each of 11 values are random distributions, each of which can take probablity between 0 to 300.

For example, first value 100 is taken from a probablity distribution dist as follows:

>> dist=[0 0.1;50 0.3; 90 0.3; 150 0.2 ;160 0.1]

dist =

         0    0.1000
   50.0000    0.3000
   90.0000    0.3000
  150.0000    0.2000
  160.0000    0.1000

This means that value 100 could have taken value 0 with probability of 0.1, value of 50 with probability 0.3, and so on...

Now a simple plot of indexes(1-11) on x-axis and range of values which can possibly take place is plotted on y-axis(0-300) as follows:

plot(1:11,values);ylim([0 300])

Resultant figure is as follows:

enter image description here

Lets say I want to plot probabilities of each value along Y axis with shade of colour blue. Higher intensity blue means higher value.

In my example, there will be 5 dots of blue for value=100. On plot, points (1,0), (1,50), (1,90), (1,150),(1,160) will be a filled blue circle. Point (1,0) should have ligher shade of blue than Point(1,50) as it has less probability of occuring. Point(1,50) and point(1,90) should have same shade of blue as they have same probability.

My idea is represented in a MSPAINT edited picture as follows:

enter image description here

How do I generate above required plot in Matlab?


Solution

  • With some understanding on the scatter function and on creating custom colormaps, here's a straight-forward approach:

    % Distributions
    dist{1} = [0 0.1; 50 0.3; 90 0.3; 150 0.2; 160 0.1];
    dist{2} = [0 0.01; 100 0.7; 150 0.29];
    dist{3} = [160 1.0];
    
    figure(1);
    hold on;
    for ii = 1:numel(dist)
    
      % Number of distribution values
      n = size(dist{ii}, 1);
    
      % Scatter plot: 
      %   x = iterating index 
      %   y = distribution value
      %   c = probability value
      scatter(ii .* ones(n, 1), dist{ii}(:, 1), 151, dist{ii}(:, 2), 'filled', 'MarkerEdgeColor', 'k');
    
    end
    hold off;
    xlim([0 4]);
    ylim([-50 300]);
    
    % Colormap dark blue -> light blue
    cm = [(0:0.01:1).' (0:0.01:1).' ones(101, 1)]
    colormap(cm);
    colorbar();
    

    We get the following output:

    Output

    As you can see, for your example (first column) with nearby probability values (0.1 to 0.3), it's hard to distinguish between the colors using the chosen colormap. Nevertheless, you see, it's working (second and third column). So, maybe play around with the colormap to find the one which meets your needs best.

    Hope that helps!