Bar plot with a hatched fill pattern
589 views (last 30 days)
Show older comments
I have a grouped bar plot, bb:
bb = bar(ax, x, y)
where 'ax' is the axis handle, x is a 1x7 datetime vector and y is a 5x7 double vector. For each of the seven dates, I get five bars with data.
I then specify the color of the bars:
for i = 1:5
bb(i).FaceColor = colmapLight(i,:);
bb(i).EdgeColor = colmapDark(i,:);
end
In addition to specifying the colors, I want to use a hatched fill pattern, e.g. horizontal lines in the first two bars in each group, and dots in the last three. I tried using the functions mentioned in this post (https://blogs.mathworks.com/pick/2011/07/15/creating-hatched-patches/), but I haven't managed to make any of them work. I think the hatchfill function (https://se.mathworks.com/matlabcentral/fileexchange/30733-hatchfill) suits my needs best (I want to keep my custom bar colors; plus I don't need a bitmap copy of the figure, want to keep it as a fig). However, the function works on 'patch' objects and I don't know how to get their handles. The following:
hPatch = findobj(bb, 'Type', 'patch');
returns an empty, 0x0 GraphicsPlaceholder.
Does anyone know a way to solve this? Thanks in advance!
0 Comments
Accepted Answer
DGM
on 13 Mar 2022
You'd want to avoid hatchfill(). That's so old that it predates the current graphics system. Things like contour and bar plots are constructed differently than they were prior to R2014. There is an updated version that seems to work okay for this.
y = 0.5*randn(3,5)+2; % a simplified example
hp = bar(y);
cm = colororder; % or replace with the desired colormap
hatchfill2(hp(1),'single','HatchAngle',0,'hatchcolor',cm(1,:));
hatchfill2(hp(2),'cross','HatchAngle',45,'hatchcolor',cm(2,:));
hatchfill2(hp(3),'single','HatchAngle',45,'hatchcolor',cm(3,:));
hatchfill2(hp(4),'single','HatchAngle',-45,'hatchcolor',cm(4,:));
hatchfill2(hp(5),'cross','HatchAngle',30,'hatchcolor',cm(5,:));
for b = 1:numel(hp)
hp(b).FaceColor = 'none';
end
31 Comments
DGM
on 1 Aug 2024
Edited: DGM
on 1 Aug 2024
Yeah, pretty much.
EDIT: I figured I could make it even more ridiculous by reverting to raster composition. (No I'm not serious)
% say you have any collection of colored plot objects
y = round(0.5*randn(3,5)+2);
hp = bar(y,'stacked');
legend({'meow','trill','purr','hiss','growl'},'location','eastoutside')
title('cat utterances')
xlabel('subject')
% take a raster screenshot
saveas(gcf,'myscreenshot.png')
% load it
inpict = imread('myscreenshot.png');
% these are the colors used in the image
% need the colors in the same class
CT = im2uint8(lines(5)); % there are 5 colors used
% patterns to use for the fills
% these are included with MIMT
pats = {imread('sources/patterns/ospat/OldSchool_A6_165.png');
imread('sources/patterns/ospat/OldSchool_A6_046.png');
imread('sources/patterns/ospat/OldSchool_A6_064.png');
imread('sources/patterns/ospat/OldSchool_A6_089.png');
imread('sources/patterns/ospat/OldSchool_A6_108.png')};
% FG colors
fgct = ccmap('hsyp',5); % MIMT
% BG colors
bgct = zeros(5,3); % all black
% replace the colored regions as specified
szo = size(inpict,1:3);
outpict = inpict;
for k = 1:size(CT,1)
% i'm assuming we can get an exact mask
% i.e. that there's no antialiasing or JPG crust
% i make this assumption on the basis that this example
% is ridiculous anyway
regionmk = all(inpict == permute(CT(k,:),[1 3 2]),3);
% create a pattern fill
patmask = ptile(im2gray(pats{k}),szo); % MIMT
patfill = replacepixels(fgct(k,:),bgct(k,:),patmask); % MIMT
% fill the selected region
outpict = replacepixels(patfill,outpict,regionmk); % MIMT
end
imwrite(outpict,'sillyhatch.png')
It's fun to use these old pattern fills again, but nobody would work at an appropriately low resolution for them to render well. I suppose the same method would work with larger pattern tiles, but it loses the charm.
... but maybe there's no accounting for taste.
As an aside, I think a lot of people regard hatch fills as antiquated, or maybe just cluttered. I see hatch fills mostly as a practical compromise for printability, and I think that's still a justifiable solution 30 years later. I assume the biggest factor against the need for hatch fills would be simply that fewer things get physically printed, but fewer isn't none. I suppose catering to colorblindness accessibility is probably more of a concern today than it was in the past, and hatch fills can have some utility there too.
dpb
on 1 Aug 2024
" I think a lot of people regard hatch fills as antiquated..."
Apparently Mathworks is among them... :(
At the time, I was working where needed black/white presentation materials; color gradations eventually wash out into indistinguishable shades. I don't know that has completely gone away, either.
More Answers (0)
See Also
Categories
Find more on Graphics Object Properties 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!