Custom Colours of a Pie Chart Sections

Hello!
I wonder if it is possible to create a pie chart in such a way so as to have 6 equally sized sections - each of which is coloured in a specific shade of green or red -depending on the percentage input - 100% being the brightest red or green and 10% being very pale green or red. Thanks! Dima

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 11 Jan. 2012

5 Stimmen

It was necessary to get rid of the "clc" to get it to work.
Note: the sectors go counter-clockwise in a "pie" chart.
% Program to apply red and green tinted colors to pie segments
% depending on the size of the pie segment.
function test1()
fontSize = 24;
X = [0 0.5 -0.2 0.3 0.8 -0.7];
fig = figure;
ax = axes('Parent', fig);
numberOfSegments = length(X);
rgbmatrix = [1+(X(:) < 0).*X(:), 1-(X(:) > 0).*X(:), 1-abs(X(:))];
hPieComponentHandles = pie(ax, ones(1,numberOfSegments));
title('Pie Chart with Custom Colors', 'Parent', ax, 'fontSize', fontSize);
% Enlarge figure to full screen.
set(fig, 'units', 'normalized', 'outerposition', [0 0 1 1]);
set(fig, 'name', 'Demo by ImageAnalyst & Tanuki', 'numbertitle', 'off')
% Assign custom colors.
for k = 1 : numberOfSegments
% Create a color for this sector of the pie
pieColorMap = rgbmatrix(k,:); % Color for this segment.
% Apply the colors we just generated to the pie chart.
set(hPieComponentHandles(k*2-1), 'FaceColor', pieColorMap);
set(hPieComponentHandles(k*2), 'String', num2str(X(k)), 'FontSize', fontSize );
end

12 Kommentare

Dima
Dima am 10 Jan. 2012
Verschoben: Adam Danz am 8 Mai 2025
Hello Walter and Image Analyst,
I have made the following changes to the code and it now operates without asking for user input. But colours all the segments in one bright red colour. I need each segment to be coloured in its own specific shade of green or red based on this vector
[0 0.5 -0.2 0.3 0.8 -0.7]
where minus values describe the place on the NO COLOUR to bright RED spectrum and positive values describe the place on the NO COLOUR to the bright GREEN colour.....this function -
rgbmatrix = [(X(:) > 0).*X(:), zeros(length(X),1), -(X(:) < 0).*X(:)];
does this?
the updated code:
% Program to apply red tinted colors to pie segments
% depending on the size of the pie segment.
function test1()
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 24;
X = [1 1 1 1 1 1 ] ;
hPieComponentHandles = pie(X);
rgbmatrix = [(X(:) > 0).*X(:), zeros(length(X),1), -(X(:) < 0).*X(:)];
title('Pie Chart with Custom Colors', 'fontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Assign custom colors.
numberOfSegments = length(X)
for k = 1 : numberOfSegments
% Create a color for this sector of the pie
fractionOfPie = sum(X(1:k)) / sum(X)
thisColor = [1 1-fractionOfPie 1-fractionOfPie] % Display in command window.
pieColorMap(k,:) = rgbmatrix(k,:); % Color for this segment.
% Apply the colors we just generated to the pie chart.
SetPieChartColors(hPieComponentHandles, pieColorMap);
end
%=====================================================================
% If you apply a colormap, MATLAB has a "feature" where it applies the
% colormap to ALL the axes on the figure, not just the current axes. So if
% you apply a colormap to the current axes (your pie chart) thinking it
% will affect only your pie chart, you will be surprised to find it affects
% all other charts and images on the dialog box. To get around that, use
% this function which the colors of the pie segments and does not affect
% any other objects in the dialog box. You need to pass in
% hPieComponentHandles which you get when you create the pie chart:
% hPieComponentHandles = pie([Value1, Value2, Value3],{'Label 1','Label 2','Label 3'});
% Then make up your color map like this:
% pieColorMap(1,:) = [.22 .71 .29]; % Color for segment 1.
% pieColorMap(2,:) = [.25 .55 .79]; % Color for segment 2.
% pieColorMap(3,:) = [.93 .11 .14]; % Color for segment 3.
% and finally, call this function
% SetPieChartColors(hPieComponentHandles, pieColorMap);
function SetPieChartColors(hPieComponentHandles, PieSegmentColors)
try
numberOfSegments = min([size(PieSegmentColors, 1) length(hPieComponentHandles)])
for s = 1 : numberOfSegments
set(hPieComponentHandles((s-1)*2+1),'FaceColor', PieSegmentColors(s,:));
end
catch ME
errorMessage = sprintf('Error in function SetPieChartColors.\n\nError Message:\n%s', ME.message);
fprintf(1,'%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
return; % from SetPieChartColors
Thank you for your help!)))
Dima
Dima am 10 Jan. 2012
Verschoben: Adam Danz am 8 Mai 2025
I replaced pieColorMap with rgbmatrix and it still colours all in one bright shade of RED:
% Program to apply red tinted colors to pie segments
% depending on the size of the pie segment.
function test1()
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 24;
X = [1 1 1 1 1 1 ] ;
hPieComponentHandles = pie(X);
rgbmatrix = [(X(:) > 0).*X(:), zeros(length(X),1), -(X(:) < 0).*X(:)];
title('Pie Chart with Custom Colors', 'fontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Assign custom colors.
numberOfSegments = length(X)
for k = 1 : numberOfSegments
% Create a color for this sector of the pie
fractionOfPie = sum(X(1:k)) / sum(X)
thisColor = [1 1-fractionOfPie 1-fractionOfPie] % Display in command window.
pieColorMap(k,:) = rgbmatrix(k,:); % Color for this segment.
% Apply the colors we just generated to the pie chart.
SetPieChartColors(hPieComponentHandles, pieColorMap);
end
%=====================================================================
% If you apply a colormap, MATLAB has a "feature" where it applies the
% colormap to ALL the axes on the figure, not just the current axes. So if
% you apply a colormap to the current axes (your pie chart) thinking it
% will affect only your pie chart, you will be surprised to find it affects
% all other charts and images on the dialog box. To get around that, use
% this function which the colors of the pie segments and does not affect
% any other objects in the dialog box. You need to pass in
% hPieComponentHandles which you get when you create the pie chart:
% hPieComponentHandles = pie([Value1, Value2, Value3],{'Label 1','Label 2','Label 3'});
% Then make up your color map like this:
% pieColorMap(1,:) = [.22 .71 .29]; % Color for segment 1.
% pieColorMap(2,:) = [.25 .55 .79]; % Color for segment 2.
% pieColorMap(3,:) = [.93 .11 .14]; % Color for segment 3.
% and finally, call this function
% SetPieChartColors(hPieComponentHandles, pieColorMap);
function SetPieChartColors(hPieComponentHandles, rgbmatrix)
try
numberOfSegments = min([size(rgbmatrix, 1) length(hPieComponentHandles)])
for s = 1 : numberOfSegments
set(hPieComponentHandles((s-1)*2+1),'FaceColor', rgbmatrix(s,:));
end
catch ME
errorMessage = sprintf('Error in function SetPieChartColors.\n\nError Message:\n%s', ME.message);
fprintf(1,'%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
return; % from SetPieChartColors
Image Analyst
Image Analyst am 10 Jan. 2012
Verschoben: Adam Danz am 8 Mai 2025
See new (shorter answer).
Dima
Dima am 11 Jan. 2012
Verschoben: Adam Danz am 8 Mai 2025
Hello again!)
I was wondering if the following code can be modified in such a way that is colours each of the sections in each specific shade of green or red based on his vector [0 0.5 -0.2 0.3 0.8 -0.7] :
% Create sample data and plot it.
X = [1 1 1 1 1 1 ] ;
numberOfSegments = length(X)
hPieComponentHandles = pie(X);
% Create custom colormap: 0=pure red, 1 = white.
ramp = [0 : 1/(numberOfSegments-1) : 1]'
pieColorMap = [ones(numberOfSegments, 1), ramp, ramp]
% Note: use flipud(pieColorMap) if you want it
% the other way: 0=white, 1 = pure red.
% pieColorMap = flipud(pieColorMap);
% Apply the colors we just generated to the pie chart.
SetPieChartColors(hPieComponentHandles, pieColorMap);
title('Pie Chart with Custom Colors', 'fontSize', fontSize);
Can you please help me finalize this project?
Thanks a lot!)
Dima
Dima am 11 Jan. 2012
Verschoben: Adam Danz am 8 Mai 2025
and yes, the following picture shows approximate version of the pie to be drawn with this code:
http://imageshack.us/content_round.php?page=done&l=img36/707/pieh.png&via=mupload&newlp=1
Dima
Dima am 11 Jan. 2012
thanks so much for your help and for this elegant script!)) and of course to Image Analyst who provided the original version) I was wondering if there is a way to explicitly set the starting and the ending colours of the spectrum (which are now RED TO GREEN) from the RGB colour code? I have the following two colours I might wish to use instead of the RED [1,0.4,0.6] and GREEN [0,0.8,1]...thanks for your fabulous help!
Walter Roberson
Walter Roberson am 11 Jan. 2012
Bearbeitet: Walter Roberson am 27 Aug. 2020
posrgb = [1,0.4,0.6];
negrgb = [0,0.8,1];
rgbmatrix = [ ...
interp1([-1 0 1], [negrgb(1), 1, posrgb(1)], X(:)), ...
interp1([-1 0 1], [negrgb(2), 1, posrgb(2)], X(:)), ...
interp1([-1 0 1], [negrgb(3), 1, posrgb(3)], X(:)) ];
I note, by the way, that [0,.8,1] is not very green at all.
Dima
Dima am 11 Jan. 2012
yes you are right....the green and the red are the prototype colours....I need to adjust this wonderful pie))) sounds almost like an apple pie)))) to suit the colour scheme of a chart on which I will need to place this pie....
thank you so much for you awesome help...I am so far from this level of programming expertise and happily learn from you!
Dima
Dima am 11 Jan. 2012
I just tested it and it works superb!!!! thanks again for your generous help1)
Walter Roberson
Walter Roberson am 12 Jan. 2012
I think the interp1 form used at the end is easier to understand than the conditions multiplied by the values and getting the signs right and the other tricks. Some days it doesn't pay to be clever ;-)
Dima
Dima am 12 Jan. 2012
yes exactly!))) thanks again for clarifying for me to see the logic of this scrip...works superb!))) I also had two questions:
1) you think this pie can be placed over another chart at a specified location based on the axes of the other chart?
2) and one more question - you think the size of this pie can be controlled to place say two different sized pies one over another on the above chart at the exact same location?
thank you very much for your generous assistance!)
Mathieu
Mathieu am 27 Aug. 2020
Thank you for this example, very helpfull !

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (4)

Steven Lord
Steven Lord am 9 Mai 2025
Bearbeitet: Steven Lord am 28 Mai 2025

3 Stimmen

This wasn't an option when the question was originally asked, but if you're using release R2023b or later you could use the piechart function instead of pie in conjunction with the colororder function.
piechart([1 2 3 4])
colororder([1 0 0; 0 1 0; 0 0 1; 0 0 0])
While I created the list of colors manually for the example above, you can construct a color matrix in a more automated fashion either using vector operations or by passing a named color palette to the colororder function.
Here I'm generating a matrix of six RGB values, each of which has the red component set to 1 with increasing values of the green and blue components.
GBcomponents = linspace(0, 0.9, 6).';
Rcomponent = ones(6, 1);
RGB = [Rcomponent, GBcomponents, GBcomponents]
RGB = 6×3
1.0000 0 0 1.0000 0.1800 0.1800 1.0000 0.3600 0.3600 1.0000 0.5400 0.5400 1.0000 0.7200 0.7200 1.0000 0.9000 0.9000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
After creating a piechart with six wedges, I can use that matrix to set the colors.
piechart([1 2 3 4 5 6])
colororder(RGB)
Or if I like the way some of the color palettes defined on the colororder documentation page look, I can call colororder with that palette name.
piechart(1:6)
colororder("meadow")
As a third option, if you've been using the colormap function with a particular colormap that you like from its documentation page, you could use that function (or one of the predefined colormap functions) to generate an RGB matrix for use with colororder. It's a little early for summer here in Massachusetts but I think it's okay to use the summer colormap for this example.
piechart(1:6)
RGBsummer = summer(6);
colororder(RGBsummer)
Walter Roberson
Walter Roberson am 8 Jan. 2012

2 Stimmen

Not using pie(). You could patch() this together yourself. You might want to start with the circle routines shown in the FAQ

1 Kommentar

Dima
Dima am 9 Jan. 2012
thanks....do you think you can show me an example of that?
thanks!)

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 10 Jan. 2012

2 Stimmen

Dima: Try this:
% Create sample data and plot it.
X = [1 1 1 1 1 1 ] ;
numberOfSegments = length(X)
hPieComponentHandles = pie(X);
% Create custom colormap: 0=pure red, 1 = white.
ramp = [0 : 1/(numberOfSegments-1) : 1]'
pieColorMap = [ones(numberOfSegments, 1), ramp, ramp]
% Note: use flipud(pieColorMap) if you want it
% the other way: 0=white, 1 = pure red.
% pieColorMap = flipud(pieColorMap);
% Apply the colors we just generated to the pie chart.
SetPieChartColors(hPieComponentHandles, pieColorMap);
title('Pie Chart with Custom Colors', 'fontSize', fontSize);

5 Kommentare

Walter Roberson
Walter Roberson am 10 Jan. 2012
You need X = [0 0.5 -0.2 0.3 0.8 -0.7]; and
hPieComponentHandles = pie(ones(1,numberOfSegments));
Dima
Dima am 10 Jan. 2012
Hello!))
Thank you for your continued assistance in this matter. I tried to run the shorter code that you provided, Image Analyst and placed the two lines that were suggested by Walter:
% Create sample data and plot it.
X = [0 0.5 -0.2 0.3 0.8 -0.7];
numberOfSegments = length(X);
hPieComponentHandles = pie(ones(1,numberOfSegments));
% Create custom colormap: 0=pure red, 1 = white.
ramp = [0 : 1/(numberOfSegments-1) : 1]'
pieColorMap = [ones(numberOfSegments, 1), ramp, ramp]
% Note: use flipud(pieColorMap) if you want it
% the other way: 0=white, 1 = pure red.
% pieColorMap = flipud(pieColorMap);
% Apply the colors we just generated to the pie chart.
SetPieChartColors(hPieComponentHandles, pieColorMap);
title('Pie Chart with Custom Colors', 'fontSize', fontSize);
This code colours each segment in the usual colour map - not with the specific shades of the green or red....I wonder where did I go wrong? Can you please correct me?
Thanks a lot!)
Dima
Dima am 10 Jan. 2012
I created a proto-type pie) that shows what the desired pie woudl look like:
http://imageshack.us/content_round.php?page=done&l=img36/707/pieh.png&via=mupload&newlp=1
the segments are labelled each with their corresponding colour code...
Dima
Dima am 10 Jan. 2012
hopefully we are close to it))
Dima
Dima am 10 Jan. 2012
you think the code above is able to generate a similarly coloured pie? thanks again!)

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 8 Jan. 2012

0 Stimmen

Dima:
Try this demo. Save it as test1.m and then run it. I think it will do exactly what you've asked for (and hopefully that's what you want).
% Program to apply red tinted colors to pie segments
% depending on the size of the pie segment.
function test1()
try
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 24;
X = [1 2 3 4 5 6];
hPieComponentHandles = pie(X);
title('Pie Chart with Custom Colors', 'fontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Ask user if they want to apply custom colors.
promptMessage = sprintf('These are the initial colors.\nDo you want to apply custom colors,\nor Cancel to exit the program?');
button = questdlg(promptMessage, 'Continue', 'Continue', 'Cancel', 'Continue');
if strcmp(button, 'Cancel')
return;
end
% Assign custom colors.
numberOfSegments = length(X)
for k = 1 : numberOfSegments
% Create a color for this sector of the pie
fractionOfPie = X(k) / sum(X)
thisColor = [1 1-fractionOfPie 1-fractionOfPie] % Display in command window.
pieColorMap(k,:) = thisColor; % Color for this segment.
% Apply the colors we just generated to the pie chart.
SetPieChartColors(hPieComponentHandles, pieColorMap);
if k < numberOfSegments
promptMessage = sprintf('Applied new color to sector %d,\nContinue or Cancel?', k);
button = questdlg(promptMessage, 'Continue', 'Continue', 'Cancel', 'Continue');
if strcmp(button, 'Cancel')
break;
end
end
end
catch ME
errorMessage = sprintf('Error in function test1.\n\nError Message:\n%s', ME.message);
fprintf(1,'%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
return; % from test1
%=====================================================================
% If you apply a colormap, MATLAB has a "feature" where it applies the
% colormap to ALL the axes on the figure, not just the current axes. So if
% you apply a colormap to the current axes (your pie chart) thinking it
% will affect only your pie chart, you will be surprised to find it affects
% all other charts and images on the dialog box. To get around that, use
% this function which the colors of the pie segments and does not affect
% any other objects in the dialog box. You need to pass in
% hPieComponentHandles which you get when you create the pie chart:
% hPieComponentHandles = pie([Value1, Value2, Value3],{'Label 1','Label 2','Label 3'});
% Then make up your color map like this:
% pieColorMap(1,:) = [.22 .71 .29]; % Color for segment 1.
% pieColorMap(2,:) = [.25 .55 .79]; % Color for segment 2.
% pieColorMap(3,:) = [.93 .11 .14]; % Color for segment 3.
% and finally, call this function
% SetPieChartColors(hPieComponentHandles, pieColorMap);
function SetPieChartColors(hPieComponentHandles, PieSegmentColors)
try
numberOfSegments = min([size(PieSegmentColors, 1) length(hPieComponentHandles)])
for s = 1 : numberOfSegments
set(hPieComponentHandles((s-1)*2+1),'FaceColor', PieSegmentColors(s,:));
end
catch ME
errorMessage = sprintf('Error in function SetPieChartColors.\n\nError Message:\n%s', ME.message);
fprintf(1,'%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
return; % from SetPieChartColors

17 Kommentare

Walter Roberson
Walter Roberson am 9 Jan. 2012
Note: For the user's question, instead of pie(X) use pie(ones(size(X)) or else the portions will not come out equal size.
Dima
Dima am 9 Jan. 2012
thank you Walter....will try it soon and hopefully it works..I do still need the circle sections to be of the same size but their colours to be different based on the values.....you think this script can be changed to reflect that?
thanks!!)
Image Analyst
Image Analyst am 9 Jan. 2012
Not sure I understand. If all pie sectors are the same value - the same percentage of the pie - then how can you have different values and different colors depending on the values? All the values would be the same - 1/6th of the whole pie - so all colors would also be the same. I guess I'm just not understanding something.
Walter Roberson
Walter Roberson am 9 Jan. 2012
The difference in values is to be represented by color instead of by size.
If the pie() line in your code is changed as I indicate, but the fractionofpie is left to use X, then the aim should be achieved.
Dima
Dima am 9 Jan. 2012
yes the difference between the equally-sized pie segments should be demonstrated through colour....actually I needed it to range through bright red at (-1) to no colour at 0 and all the way up to bright green at (+1). Can you please help me achieve this with this code (if you have some time of course)? I have learned a lot for your earlier answers, Walter and would learn so much more with this one...thanks!)
Dima
Dima am 9 Jan. 2012
Image Analyst special thanks to you for this fabulous code....I do need to change it a little.....I wonder if you think this code allows to introduce the range of the colours to paint the segments - from bright red at (-1) to bright green at (+1) paling inside out toward the zero from both sides of the spectrum?
thank you for you help!)
Image Analyst
Image Analyst am 9 Jan. 2012
Did you figure it out? Just make X=[1 1 1 1 1 1] at the beginning of the program, and modify that one line like Walter said:
fractionOfPie = sum(X(1:k)) / sum(X)
and all slices will be the same size and the colors will go from pale red to pure red. You can easily adapt it for any other colors.
Walter Roberson
Walter Roberson am 9 Jan. 2012
I don't think one should set X=[1 1 1 1 1 1] -- one needs the X to have the portion data. I also do not see any reason to take a cumulative sum for the fraction. I still think one should just be changing pie(X) to pie(ones(size(X)); that together with the color changes.
However, I am concerned that input values seem to have been switched to being in the range -1 to +1 . What is meant to calculate a percentage of the total when there is a negative in the mix? Or did the problem get changed on us and it is no longer the _percentage_ that should be considered, and instead the color should simply map according the the value of each entry independent of what the other entries are (not a percentage) ?
Image Analyst
Image Analyst am 9 Jan. 2012
OK I'm getting confused. How are the portions going to be equal-sized if all the X values aren't equal-sized? I took the cumulative sum so that the color could change from slice to slice.
Walter Roberson
Walter Roberson am 9 Jan. 2012
The OP wants something pie()-like but not necessarily pie() itself. The slices are to be equal sized dependent only on the number of values, but the coloring for each slice is to be dependent on the values. Two entries that have the same value should produce the same output color.
Originally the question had the color chosen according by percentage, but the OP appears to have changed to instead have the color dependent only upon the individual values.
Dima
Dima am 9 Jan. 2012
Thanks!) I undated the code with X=[1 1 1 1 1 1] and fractionOfPie = sum(X(1:k)) / sum(X)
now it draws the pie with 7 equally sized sections each with a slightly denser shade of the red. I apologize for confusing you by replacing the original percentages with the -1 to +1 spectrum...that can be put like this. I need each of the equally-sized pie sections to be coloured based on another vector - for example [0 0.5 -0.2 0.3 0.8 -0.7] each of the positive elements of this vector (0.5,0.3,0.8) corresponds to the specific shade of GREEN - 1 being the most saturated GREEN and 0.1 being very light GREEN . Zero stands for no colour. LIKEWISE the negative values (-0.2,-0.7) stand for the specific shades of the RED with -1 being the most dense RED available. I intended to initially express these values as percentages (e.g. -0.2 would be -20% and 0.7 would be 70%). I wonder if this functionality can be elegantly built into your already quite elegant script? thanks!
Dima
Dima am 9 Jan. 2012
Yes Walter you are right - I need a pie chart with equally sized section whose colouring is based on another vector of the values ranging from 1 (standing for bright GREEN) to -1 (standing for bright RED). Please let me know what you think.
Dima
Image Analyst
Image Analyst am 9 Jan. 2012
Dima: Not sure why you're getting 7 sections instead of 6. Well, you see how the colors were assigned. You just have to figure out some scheme to set up r, g, and b such that they range from 0-1. You can pick colors in Photoshop if you want and then divide by 255 to get your numbers in the range of 0-1. If you can't figure it out then let us know what are the 6 colors you want in RGB form (0 to 255). Maybe it won't be a formula like I did - maybe you'll just pick the colors from a custom colormap you made up, like
rgbColor = myColorMap(sectionNumber, :)/255;
Dima
Dima am 9 Jan. 2012
thanks...you mean it is best to create a way to find a place on the green to red spectrum by preparing all possibel values beforehand?
Dima
Dima am 9 Jan. 2012
as for 7 sections - I added one more))) the fact is I need to find a way to arrive at the place or the red to green sector based on the percentage value - and these can be very different....I need to represent the stats of a few indicators in one coherent way on the chart...I chose the pie chart for this....I also had a question - I need the code to draw the pie chart without prompting for user decision for each sector....can you please guide me in what part of this code shoudl be changed so that it draws the pie chart in one go?
thanks a lot for your help!)
Walter Roberson
Walter Roberson am 9 Jan. 2012
No advance computation required:
rgbmatrix = [(X(:) > 0).*X(:), zeros(length(X),1), -(X(:) < 0).*X(:)];
Then slice K is color rgbmatrix(K,:)
Image Analyst
Image Analyst am 9 Jan. 2012
If that formula for arriving at colors works for you, then do it before the k loop and then in the loop, you can just do
pieColorMap(k,:) = rgbmatrix(k,:); % Color for this segment.
and get rid of thisColor computation. Heck, you don't even really need the k loop - that was just for tutorial purposes. You could assign the colors all in one call without any loop over k at all:
SetPieChartColors(hPieComponentHandles, rgbmatrix);

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by