Search code examples
matlabcolorsmatlab-figurepie-chart

Pie Chart Color


How can I create two different pie chart with different colors?
That it to say, I have to plot 2 different data:

  1. One of them has Monday, Tuesday, Wednesday... (all days)
  2. And the other one has Monday, Wednesday and Sunday.

I want that Monday in chart 1 and Monday in chart 2 to have the same color. The same thing for Wednesday, etc.
Tuesday and the other days which do not appear in the second plot in other color. Is it possible?

Using:

figure
X = rand(5, 1);
X = X/sum(X);
p = pie(X, {'M', 'T', 'W', 'TH', 'F'});
figure
X2 = rand(5, 1);
X2(2) = 0; % remove Tuesday from the plot
X2 = X2/sum(X2);
p = pie(X2, {'M', 'T', 'W', 'TH', 'F'});

gives:

image


Solution

  • A small hack is to set the values of the days that you want to show to a very small positive value and use an empty char array or a char array with space characters for their labels.

    The smallest value in MATLAB can be returned using realmin('double') or you can use eps or manually define a very small positive value.

    figure
    X = rand(7,1);
    X = X/sum(X);
    subplot(1,2,1);
    p = pie(X,{'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'});
    
    subplot(1,2,2);
    X2 = rand(7,1);
    X2([2,4,5,6]) = realmin('double');  %<----- Notice this (setting very small values)
    X2 = X2/sum(X2);                                
    p = pie(X2,{'Mon', '', 'Wed', '', '', '', 'Sun'});
    %Notice this -------^----------^---^---^   No label for Tue, Thur, Fri, Sat
    

    which gives:

    out