Bar plot with a hatched fill pattern

723 views (last 30 days)
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!

Accepted Answer

DGM
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
  28 Comments
Navinder Singh
Navinder Singh on 11 Sep 2023
Edited: Navinder Singh on 11 Sep 2023
I am not aware of the "hatchfille2_BoxChartOpt". I simply used the code (provided as solution here). Then I tried using legend command, but it doesnot work as intended.
@DGM as suggested, I checked these links. These are fine although a bit complicated for an end user. But I noticed the hatch pattern looks distorted depending on the aspect ratio and scaling of the figure box.
Below are the examples showcasing this issue. Both are generated using same code and but the size of figures/legends have been changed manually. Shoudn't the angles and the spacing be consistent throughout?
Walter Roberson
Walter Roberson on 11 Sep 2023
Edited: Walter Roberson on 11 Sep 2023
Shoudn't the angles and the spacing be consistent throughout?
No. The code assumed a certain size and aspect ratio for the drawing area in the legend. You manually resized the drawing area, violating the assumptions of the code.
The only way to draw the representative sample in the legend area is to have an axes to draw into. line() coordinates are always in data units. If you change the shape of the axes without adjusting the axes XLim and YLim then you are going to distort the relationship between data coordinates and physical coordinates. And if you do adjust XLim and YLim to keep the relationship consistent, then you cannot expect that the lines will extend to full width or height of the new axes size.
legend() has never [deliberately] provided any callback mechanism to provide hooks to redraw the representative sample icons when the legend is resized. It would be possible to set a listener on a resize event for the axes used by legend... but only when the two-output "legacy" version of legend() is used (the newer single-output version does not give access to the drawing area.)

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!