MATLAB Answers

Exploded Bar plot (stacked)?

4 views (last 30 days)
Dominik Mattioli
Dominik Mattioli on 16 Jul 2021
Answered: Arthi Sathyamurthi on 23 Jul 2021
Is there functionality for "exploding" the stacked bars in a bar plot? I have data that can be visualized using the bar( x, y, 'stacked' ) method, but the y-axis values don't hold intuitive value. I think that my approach (below) may remedy this issue with a new plot design, but I'm curious if this MATLAB functionality already exists.
So if this were the result of plotting your data 'stacked':
Then this would be the exploded stacked plot, removing the need for a legend by using the y-axis:
% Stacked, conventional.
figure; bar( xdata, ydata, 'stacked' );
% Stacked, exploded. First, figure out the patch dimensions, then plot.
boxPatchXY = cell( numGroups, numItems );
bufferY = 0.125;
bufferX = 0.2;
minY = 10 * bufferY;
minX = 0.6 : 1 : numItems + 0.6;
maxX = 1.4 : 1 : numItems + 1.4;
midY = NaN( numGroups, 1 );
for group = 1:numGroups
groupData = ydata( group, : );
if group == 1
minY = minY + bufferY;
else
minY = maxY + bufferY;
end
maxY = max( groupData ) + minY;
midY( group ) = ( minY + maxY ) / 2 ;
for item = 1:numItems
startX = minX( item );
endX = maxX( item );
itemX = vertcat( startX, startX, endX, endX, endX, startX );
itemY = vertcat( midY( group ), midY( group ) - groupData( item ) / 2,...
midY( group ) - groupData( item ) / 2, midY( group ),...
midY( group ) + groupData( item ) / 2, midY( group ) + groupData( item ) / 2 );
boxPatchXY{ group, item } = horzcat( itemX, itemY );
end
end
boxPatchPlots = gobjects( numel( boxPatchXY ), 1 );
for idx = 1:numel( boxPatchPlots )
boxPatchPlots( idx ) = patch( boxPatchXY{ idx }( :, 1 ), boxPatchXY{ idx }( :, 2 ),...
'FaceColor', barColors{ idx }, 'EdgeColor', barColors{ idx },...
'Parent', aLaneColumns );
end

Answers (1)

Arthi Sathyamurthi
Arthi Sathyamurthi on 23 Jul 2021
Hello Dominik,
Currently we are not supporting 'explode' feature in the bar plot function. I have brought this issue to the notice of the concerned people and it might be considered for a future release.

Community Treasure Hunt

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

Start Hunting!

Translated by