MATLAB: Does the legend in a 3D Pie chart (PIE3) show incorrect colors in MATLAB 7.6 (R2008a)

colorincorrectlegendMATLABpie3

If I plot a 3D Pie chart using PIE3 command and insert a legend, the colors in the legend are incorrectly shown. For example,
pie3([2 4 3 5])
legend('A','B','C','D')

Best Answer

This is a limitation in MATLAB in the way LEGEND and the PIE3 functions interact. PIE3 uses multiple graphical objects such as 'patch' and 'surface' to represent one slice in the pie. Each slice in a 3D pie chart is represented by two 'patch' objects and one 'surface' object.
To work around the limitation, use the FINDALL command to retrieve handles to surface objects which represent each color in the pie chart and use these handles with LEGEND. For example,
Create a 3D Pie chart
pie3([2 4 3 5]);
Find handles of objects of type surface. These should all have a unique color.
handle_surface=findall(gcf,'type','surface')
The variable 'handle_surface' contains the handles but in reverse order.The following command will reverse the handles vector so that the legend order matches the actual order of data.
handle_surface = flipud( handle_surface);
Supply this to legend along with corresponding text
legend(handle_surface,'A','B','C','D');
Related Question