R_Bekker asked . 2023-08-07

How can I explode consecutive pie chart wedges together?

I'm generating the below pie chart, which plots displays the percentage of occurrence of distinct events, and how many times these events do not occur. The pie chart is divided into a number of equal sized "larger" segments based on the num_events parameter. In the example, this is three (divided by the black lines). Within each of these segments the percentage of occurrence is shaded, while the percentage of non-occurrence is left white.
Would it be possible to explode the shaded region along with its neighboring white region, together as in the figure on the right?
 
num_events=3; % numTx has to be less than or equal to 7.
scenarios = 1;
begin_a = 97;
data = rand(scenarios,num_events);
figure
overlayCircleOnPieCharts(data,num_events);


%% Function to plot pie chart, remove the boundary and dividing lines.
function ax = overlayCircleOnPieCharts(clearance,num_events)
[numRowC,numColC] = size(clearance); 
if numColC~=num_events
    disp("Error: check unequal dimensions. Verify number of treatments.")
    return;
end
c_matrix = 100*clearance; % convert data to percentages
nc_matrix = 100*ones(numRowC,numColC) - c_matrix; % calculate non-clearance percentages
n_nc_matrix = reshape([c_matrix;nc_matrix],size(c_matrix,1),[]); % reshape to have matrix with alternating columns of clearance non-clearance
norm_matrix = n_nc_matrix./(num_events*100); % normalizes the data
non_clearance_color = ones(num_events,3);
clearance_color = [hex2rgb('#1B9E77') 
    hex2rgb('#D95F02')
    hex2rgb('#7570B3')
    hex2rgb('#E7298A')
    hex2rgb('#66A61E')
    hex2rgb('#E6AB02')
    hex2rgb('#A6761D')];
colrs = reshape([clearance_color(1:num_events,:)';non_clearance_color'],3,[])'; % creates colors correctly
    
tiledlayout('flow');
for k = 1:numRowC
    a_pi = nexttile;
    
    pie(norm_matrix(k,:));
    
    hold on
    % pie chart, with outlines removed
    pi_bakery = gca;
    pi_bakery.Colormap = colrs; % set colors, data contained in clearance matrix is shaded
    pi_bakery.Children(2).EdgeAlpha = 0;
    pi_bakery.Children(4).EdgeAlpha = 0;
    pi_bakery.Children(6).EdgeAlpha = 0;
    pi_bakery.Children(8).EdgeAlpha = 0;
    pi_bakery.Children(10).EdgeAlpha = 0;
    pi_bakery.Children(12).EdgeAlpha = 0;
    
    % circle overlayed, divided in num_events equal sections
    x_cent = 0;
    y_cent=0;
    r=1;
    th = 0:pi/50:2*pi;
    xunit = r * cos(th) + x_cent;
    yunit = r * sin(th) + y_cent;
    plot(xunit, yunit,'k','LineWidth',2.5);
    plot([x_cent x_cent],[y_cent y_cent+r],'k','LineWidth',3) % Top line
    
    for j = 1:num_events%numTx [edit to get the code to run - Voss]
        plot([x_cent (x_cent + r*sin(2*pi*((1+j)/num_events)))],[y_cent (y_cent +r*cos(2*pi*((1+j)/num_events)))],'k','LineWidth',2.5) % right hand side line
    end
    
end
end

 

pie chart , explode groups , Data Distribution Plots , 2-D and 3-D Plots

Expert Answer

Prashant Kumar answered . 2024-05-17 20:05:33

I don't see a way to explode slices together in pairs with the pie function (each slice would be exploded or not-exploded independently of the other slices, so adjacent exploded slices would not touch each other), but you can create the effect you want by creating the patch and text objects similarly to how pie does and incorporating the desired modifications.

 

num_events=3; % numTx has to be less than or equal to 7.
scenarios = 1;
data = rand(scenarios,num_events);
figure
overlayCircleOnPieCharts(data,num_events);

 

num_events=6; % numTx has to be less than or equal to 7.
scenarios = 2;
data = rand(scenarios,num_events);
figure
overlayCircleOnPieCharts(data,num_events);

 

num_events=5; % numTx has to be less than or equal to 7.
scenarios = 4;
data = rand(scenarios,num_events);
figure
overlayCircleOnPieCharts(data,num_events);

 

%% Function to plot pie chart, remove the boundary and dividing lines.
function ax = overlayCircleOnPieCharts(clearance,num_events)
[numRowC,numColC] = size(clearance); 
if numColC~=num_events
    disp("Error: check unequal dimensions. Verify number of treatments.")
    return;
end
c_matrix = 100*clearance; % convert data to percentages
nc_matrix = 100 - c_matrix; % calculate non-clearance percentages
n_nc_matrix = reshape([c_matrix;nc_matrix],size(c_matrix,1),[]); % reshape to have matrix with alternating columns of clearance non-clearance
norm_matrix = n_nc_matrix./(num_events*100); % normalizes the data
non_clearance_color = ones(num_events,3);
clearance_color = [hex2rgb('#1B9E77') 
    hex2rgb('#D95F02')
    hex2rgb('#7570B3')
    hex2rgb('#E7298A')
    hex2rgb('#66A61E')
    hex2rgb('#E6AB02')
    hex2rgb('#A6761D')];
colrs = reshape([clearance_color(1:num_events,:)';non_clearance_color'],3,[])'; % creates colors correctly
    
tiledlayout('flow');
% start, end, and center angles for each wedge
dth = 2*pi/num_events;
th_start = pi/2+dth*(0:num_events-1);
th_end = pi/2+dth*(1:num_events);
th_center = th_start+dth/2;
% distance to "explode" wedges outward
d = 0.1;
% offset for exploding (shifting out from center)
x_offset = d*cos(th_center);
y_offset = d*sin(th_center);
% factor for determining where to place the texts, 
% e.g., 1.15 means 15% beyond where the unexploded 
% circle would be, measured from the center
f_t = 1.15;
% for horizontal alignment of texts (texts on right-half plane are
% left-aligned, and texts of left half-plane are right-aligned, 
% similar to how pie() works, I think)
h_alignment = {'left','right'};
for k = 1:numRowC
    
    a_pi = nexttile;
    
    % set axes square and turn off (like pie() does)
    axis(a_pi,'square','off')
    
    % angles of the boundaries between c and nc regions
    th_boundary = pi/2+dth*((0:num_events-1)+norm_matrix(k,1:2:end)*num_events);
    
    % for each wedge:
    for j = 1:num_events%numTx
        
        % coordinates of arc for c part
        th = linspace(th_start(j),th_boundary(j),100);
        x_c = cos(th);
        y_c = sin(th);
        
        % coordinates of arc for nc part
        th = linspace(th_boundary(j),th_end(j),100);
        x_nc = cos(th);
        y_nc = sin(th);
        
        % create the c and nc region patches
        patch(x_offset(j)+[0 x_c 0],y_offset(j)+[0 y_c 0],colrs(2*j-1,:),'EdgeColor','none')
        patch(x_offset(j)+[0 x_nc 0],y_offset(j)+[0 y_nc 0],colrs(2*j,:),'EdgeColor','none')
        
        % create the wedge outline
        line(x_offset(j)+[0 x_c x_nc 0],y_offset(j)+[0 y_c y_nc 0],'Color','k','LineWidth',2.5)
        
        % create the c text
        x_t = f_t*(x_offset(j)+x_c(round(end/2)));
        y_t = f_t*(y_offset(j)+y_c(round(end/2)));
        val = 100*norm_matrix(k,2*j-1);
        if val < 1
            str = '< 1%';
        else
            str = sprintf('%.0f%%',val);
        end
        text(x_t,y_t,str,'HorizontalAlignment',h_alignment{1+(x_t < 0)})
        
        % create the nc text
        x_t = f_t*(x_offset(j)+x_nc(round(end/2)));
        y_t = f_t*(y_offset(j)+y_nc(round(end/2)));
        val = 100*norm_matrix(k,2*j);
        if val < 1
            str = '< 1%';
        else
            str = sprintf('%.0f%%',val);
        end
        text(x_t,y_t,str,'HorizontalAlignment',h_alignment{1+(x_t < 0)})
        
    end
    
    % set the viewed area of the axes
    xlim((1.2+d)*[-1 1])
    ylim((1.2+d)*[-1 1])
    
end
end

 


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.