Clear Filters
Clear Filters

how to make strips of different colors and sizes in a bar plot?

71 views (last 30 days)
Dear all,
I want to include in some to the following bars:
Strips in which I can change the sizes and colors.
Like this one:
To have for example, instead of the yellow, strips of red and green.
But it's not making strips unfortunately. (only lines, dots...)
I'm sure I'm not the first one facing this issue. Please let me know if you have a way around this.
PS: I have matlab 2016a

Answers (1)

dpb on 7 Nov 2023
Edited: dpb on 7 Nov 2023
It's been a notable hole in MATLAB graphics from the beginning that there are no builtin hatching patterns available; I've railed about it off and on since the release of the initial Windows version but there has been no action in the area for 30+ years so I'm not expecting it any time "real soon now".
I haven't looked for a couple of years now, but while a number of submittals on the FEX made an attempt at hatching, none that I tried have really been satisfactory; all had warts of one degree of severity or another from simply unpleasing patterns to not functional for certain uses or releases.
I do not know of any other solutions(*); I believe there is no generic solution available other than exporting to some other application that does have the facility.
It's really a lack in HG2 that remains mind-boggling it hasn't been addressed.
(*) One possible tack would be if you have found a FEX submittal that work but without the pattern you're interested in, look first to see if you can change colors/linestyle/linewidth as optional parameters to it besides the defaults it uses. If that were so, you should be able to do at least a limited simulation of what you're looking for by changing the color and lindwidth to something very large that would look like a stripe.
If those parameters aren't available in the user interface, then poke around inside the function to find where those are defined and see about adding those as user optional inputs...or, in a pinch for testing, just make patches to change some values manually.
dpb on 8 Nov 2023
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
hB(1) % commenting this out causes an out of bounds error????
ans =
Bar with properties: BarLayout: 'stacked' BarWidth: 0.8000 FaceColor: [0 0.4470 0.7410] EdgeColor: [0 0 0] BaseValue: 0 XData: [1 2 3 4] YData: [2 2 2 2] Use GET to show all properties
hold on
x1=hB(1).Face.VertexData(1,1:2:3); dx=diff(x1);
y1=hB(1).Face.VertexData(2,1:2:3); dy=diff(y1);
x=[x1(1) x1(1)+d x1(2) x1(2) x1(2)-d x1(1) ];
y=[y1(1) y1(1) y1(2)-d y1(2) y1(2) y1(1)+d];
illustrates a way to add patches that end at the section boundaries; this doesn't go over them as does the line, but the simple case above using the boundary values as the limits ends up masking out the bar outlines and the bottom boxing line.
It also is data values dependent so the angles won't be the same if apply the same simple logic to the second bar segments that are different lengths.
One really would need to convert to the pixel equivalents in order to make the angles consistent and be able to set within the boundaries.
dpb on 8 Nov 2023
Either of the above has a problem that legend will "know nuthink!" about the combination of bar segment color and the added line/patch being together; that would be yet another kludge to make something work.
All in all, one needs builtin hatching patterns to be able to make anything work truly seamlessly and not spend a lot of valuable time futzing around trying to get something that should be easy.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!