How can i change the colours of segmentation on a chart pie?
198 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Miss B
el 6 de Abr. de 2020
Comentada: Dave B
hace alrededor de 9 horas
Hey,
I have plotted a pie chart, but the colours of the segmentation does not appeal to me...is there any way I can alter the colours?
Thank you.
0 comentarios
Respuesta aceptada
Adam Danz
el 6 de Abr. de 2020
Editada: Adam Danz
el 22 de Jun. de 2020
Unforunately the pie() function does not allow you to directly specify the face color of each wedge as inputs.
Here are two ways to change the wedge colors after producing the pie plot.
Produce a demo pie chart & define colors
ax = gca();
pieData = [.3 .4 .3];
h = pie(ax, pieData);
% Define 3 colors, one for each of the 3 wedges
newColors = [...
1, 0.41016, 0.70313; %hot pink
0, 1, 0.49609; %spring green
0.59766, 0.19531, 0.79688]; %dark orchid
Option 1 : change the axes colormap
You can use on of Matlab's many pre-defined colormaps or you can create your own has we've done above. This works with pie() and pie3() objects.
% ax is the handle to the pie chart axes
% newColors is a nx3 matrix for n pie-wedges
ax.Colormap = newColors;
% to use a pre-defined colormap (in this example, 'Spring')
% h is the output to pie()
ax.Colormap = spring(numel(h)/2);
Option 2: Change the FaceColor of the wedges
For pie() objects
% h=pie() output is a vector of alternating patch and text handles.
% Isolate the patch handles
patchHand = findobj(h, 'Type', 'Patch');
% Set the color of all patches using the nx3 newColors matrix
set(patchHand, {'FaceColor'}, mat2cell(newColors, ones(size(newColors,1),1), 3))
% Or set the color of a single wedge
patchHand(2).FaceColor = 'r';
For pie3() objects
% Extract the surface and patch handles from the output to pie3()
surfaceHand = findobj(h, 'Type', 'Surface'); % edges
patchHand = findobj(h, 'Type', 'Patch'); % tops & bottoms
% Set the color all patches (tops & bottoms) and surfaces (edges) using the nx3 newColors matrix
set(surfaceHand, {'FaceColor'}, mat2cell(newColors, ones(size(newColors,1),1), 3));
set(patchHand, {'FaceColor'}, mat2cell(repelem(newColors,2,1), ones(size(newColors,1)*2,1), 3));
0 comentarios
Más respuestas (1)
Dave B
el 20 de Sept. de 2023
Starting in R2023b we have a new piechart which is a little easier to change colors for. Here are some demos with our built in color palettes, and then one with a custom palette. You can use the colororder function to change them or set the ColorOrder property on the PieChart object.
vals = [1 1 2 3 5];
figure
piechart(vals)
colororder("sail")
figure
piechart(vals)
colororder("reef")
figure
piechart(vals)
colororder("meadow")
figure
piechart(vals)
colororder("earth")
figure
% use validatecolor as an easy way to turn hex values into RGBs
mypalette = validatecolor(["#3BA8ED" "#ED4747" "#2FEDD1" "#ED9418" "#24ED5D"], "multiple");
piechart(vals)
colororder(mypalette)
2 comentarios
Jeremias
hace alrededor de 10 horas
Editada: Jeremias
hace 34 minutos
Hi Dave,
thank you for this solution which in principal worked perfectly for me.
Unfortunately I now have the problem that the colors appear "dimmed down" and only if I hover over one segment it is displayed in the color specified in my code. If I save my piechart as png or any other format I have tried it will not show the correct color but the dimmed version,
Do you know of any solution?
Thank you in advance!
Dave B
hace alrededor de 9 horas
Actually I do! The piechart uses transparency by default, which allows it to show the sort-of highlight effect, but that makes the colors look a little muted.
You can control the amount of transparency with the FaceAlpha property, and if you set it to 1 it will produce full opacity and the colors won't look muted. Here's the same palette as above,
vals = [1 1 2 3 5];
mypalette = validatecolor(["#3BA8ED" "#ED4747" "#2FEDD1" "#ED9418" "#24ED5D"], "multiple");
figure
piechart(vals, 'FaceAlpha', 1)
colororder(mypalette)
Ver también
Categorías
Más información sobre Pie Charts en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!