How can I plot attached three interrelated stacked bars and separately style as well as label their partially recurring block components?
Show older comments
Dear all,
I would like to visualize three decompositions by creating a graph with three stacked bar plots. I attach a schematic drawing of what I have in mind (and apologize for the format - I hope this makes it clearer than some lengthy explanation).
I have created stacked bar plots in the past, but what makes it complex / too hard for me is that
a) the blocks are interrelated across the decompositions A, B, C (e.g. block "b" turns up in decompositions A and C)
b) the separate styling and the labeling of the blocks
c) the labeling of the three stacked bars as "A", "B", "C".
Is there a way to insert the stacked bars next to each other, creating and labeling them independently but making matlab recognize the repeated occurrence of the block elements when it creates the legend?
I would be really grateful for any advice! Thank you

Accepted Answer
More Answers (1)
Use something like
y = [5 2 0 4 0 0
5 0 3 0 3 0
5 2 0 0 3 1];
x=categorical({'A';'B';'C';'D'});
bar(x(1:3),y,'stacked')
legend(cellstr(['a':'f'].'),'Location','northeast')
xticks(x(1:3))
to produce

Unfortunately, default colors are awful and we still don't have builtin hatching patterns so you'll have to work to set some less garish colors...
NB: the "trick" is each row in an array is a bar for a stacked plot; each column is the set of variable values for the bar; zero is a valid value (as is NaN for some special effects especially valuable).
NB Second: The extra category for the x axis to provide the room for the legend inside and then don't display the tick for it.
7 Comments
Distelfink
on 7 May 2022
Edited: Distelfink
on 7 May 2022
A MAJOR shortfall in MATLAB I've railed about along with bar itself for 30 years...
<Selecting-or-setting-hatching-patterns> was my first attempt after the Answers forum was opened; I'd been after TMW for years prior until finally just gave it up as lost cause...as I've pointed out to them we had them on CalComp pen plotters in the 60s and 70s and still don't exist in MATLAB????
<Answer_916574> seems to be a recent incarnation of one of the FileExchange submittals that looks like were able to get to work...I've not done anything recently in the area, specifically. I do NOT know how nicely this plays with legend(); back when my original post was made, several of the submittals at the time wouldn't reflect the hatch in the legend automagically; you had to manually hatch those to match. But, back then you had full access to the handles of the axes object from which the legend was constructed, now they've taken that facility away by making it almost opaque to user customization. You'll just have to experiment. All in all, it's a disaster of an area and an embarrassment and a major time waster for MATLAB users. As the comments there indicate, at that time all submittals on File Exchange had serious warts/shortcomings in some area; there was nothing available that "just worked" seamlessly. That was right at the HG1/HG2 transition time which didn't help any, but the basic routines at the time were also just not yet production-quality code.
Add your voice in the wilderness to enhancement requests -- they should be built into the base functions at the basic level of HG2.
Distelfink
on 7 May 2022
Agreed, it definitely should NOT be complicated at all; you should be able to just set a hatching property directly with a named parameter/pattern value pair.
However, despite my obvious displeasure/frustration w/ TMW over the issue, I still suggest using the second link to the File Exchange submission and give it a shot -- if it works well, shouldn't take longer than to export and do it elsewhere.
You don't need to be concerned about HG1/HG2; that's now history; everything now is HG2 -- TMW totally revamped the graphics subsystem back ages ago now (I forget just when but by now ancient history in version-time-frames) so you're not going to run into HG1 stuff now. That's why the second link is the one that I posted -- it is at least one submittal that is current and seemed to work although I haven't tried it myself. The only caution is that an unwary search of FileExchange could possibly lead to some older submittals still.
Distelfink
on 8 May 2022
dpb
on 8 May 2022
I'd suggest an Answer to the Q? here -- it would keep context together and by being Answer indicates a solution to the problem/Q? raised...
There's nothing wrong imo of answering your own Q? either by discovering something new on your own or with assistance, the point is to get to a satisfactory solution with, hopefully, a byproduct that somebody else may find useful and while not an official support site, some feedback that may help influence futher development.
Distelfink
on 8 May 2022
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!