How to make a specific bar to be hatched with a specific color

I have the array y1 which consists of 5 sets, and each set consists of 6 elements. For example, the first set is 0.25 1.14 2.20 0.21 1.09 2.16. I need to make the last three elements in each set to be cross hatched with a specific color I choose.. how can I make it. my code is below
x=[1,2,3,4,5];
y1=[0.25 1.14 2.20 0.21 1.09 2.16 ; 0.48 2.26 4.40 0.42 2.20 4.34; 0.72 3.38 6.58 0.74 3.27 5.86 ;1.01 4.56 8.82 0.99 4.34 7.65;1.33 5.76 11.04 1.33 5.50 9.61 ]
figure
h1 = bar(y1);
set(h1, {'DisplayName'}, {'\textbf{Proposed framework without AES}','\textbf{Proposed framework with AES-128}','\textbf{Proposed framework with AES-256}','\textbf{Delay-energy-aware without AES}','\textbf{Delay-energy-aware with AES-128}','\textbf{Delay-energy-aware with AES-256}'}')
set(gca,'TickLabelInterpreter','latex', 'LineWidth', 1,'FontSize',12, 'YMinorTick','on');
legend('Location','northwest','Interpreter','latex', 'FontWeight','bold','FontSize',9.5,...
'FontName','Palatino Linotype',...
'Location','best');
xlabel('$\textbf{Number of tasks}$','FontWeight','bold','FontSize',12,...
'FontName','Palatino Linotype','Interpreter','latex');
ylabel('$\textbf{Total delay [S]}$','FontWeight','bold','FontSize',12,...
'FontName','Palatino Linotype','Interpreter','latex');

2 Comments

Thank you so much for sharing thi interesting link.
I tried to use it. However, I would like to make the legend appear with the hatched color as the bars color. However, this is what I get.. Please, how can I do that ?
Thank you so much
x=[1,2,3,4,5];
y1=[0.25 1.14 2.20 0.21 1.09 2.16 ; 0.48 2.26 4.40 0.42 2.20 4.34; 0.72 3.38 6.58 0.74 3.27 5.86 ;1.01 4.56 8.82 0.99 4.34 7.65;1.33 5.76 11.04 1.33 5.50 9.61 ] % 3 papers with 1.85 enc and dec
y1 = 5x6
0.2500 1.1400 2.2000 0.2100 1.0900 2.1600 0.4800 2.2600 4.4000 0.4200 2.2000 4.3400 0.7200 3.3800 6.5800 0.7400 3.2700 5.8600 1.0100 4.5600 8.8200 0.9900 4.3400 7.6500 1.3300 5.7600 11.0400 1.3300 5.5000 9.6100
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%all
figure
h1 = bar(y1);
cm = colororder; % or replace with the desired colormap
hatchfill2(h1(4),'cross','HatchAngle',45,'hatchcolor',cm(1,:));
Unrecognized function or variable 'hatchfill2'.
hatchfill2(h1(5),'cross','HatchAngle',45,'hatchcolor',cm(5,:));
hatchfill2(h1(6),'cross','HatchAngle',45,'hatchcolor',cm(3,:));
h1(4).FaceColor = 'none';
h1(5).FaceColor = 'none';
h1(6).FaceColor = 'none';
set(h1, {'DisplayName'}, {'\textbf{Proposed framework without AES}','\textbf{Proposed framework with AES-128}','\textbf{Proposed framework with AES-256}','\textbf{Delay-energy-aware without AES}','\textbf{Delay-energy-aware with AES-128}','\textbf{Delay-energy-aware with AES-256}'}')
set(gca,'TickLabelInterpreter','latex', 'LineWidth', 1,'FontSize',12, 'YMinorTick','on');
legend('Location','northwest','Interpreter','latex', 'FontWeight','bold','FontSize',9.5,...
'FontName','Palatino Linotype',...
'Location','best');
xlabel('$\textbf{Number of tasks}$','FontWeight','bold','FontSize',12,...
'FontName','Palatino Linotype','Interpreter','latex');
ylabel('$\textbf{Total delay [S]}$','FontWeight','bold','FontSize',12,...
'FontName','Palatino Linotype','Interpreter','latex');

Sign in to comment.

 Accepted Answer

Here's a start
x = [1,2,3,4,5];
y1 = [0.25 1.14 2.20 0.21 1.09 2.16 ;
0.48 2.26 4.40 0.42 2.20 4.34;
0.72 3.38 6.58 0.74 3.27 5.86;
1.01 4.56 8.82 0.99 4.34 7.65;
1.33 5.76 11.04 1.33 5.50 9.61]; % 3 papers with 1.85 enc and dec
h1 = bar(y1);
% hatchfill parameters
cm = colororder; % or replace with the desired colormap
hfcolor = cm([1 5 3],:); % i'm going to reorder this for convenience
hfstyle = {'cross','cross','cross'};
hfangle = [45 45 45];
% generate hatch fills on the bar objects
hhf = gobjects(6,1);
for k = 1:3
hhf(k) = hatchfill2(h1(k+3),hfstyle{k}, ...
'HatchAngle',hfangle(k), ...
'HatchColor',hfcolor(k,:), ...
'HatchDensity',50); % see note
h1(k+3).FaceColor = 'none';
end
% figure setup
set(gca,'TickLabelInterpreter','latex', 'LineWidth', 1,'FontSize',12, 'YMinorTick','on');
xlabel('$\textbf{Number of tasks}$','FontWeight','bold','FontSize',12,...
'FontName','Palatino Linotype','Interpreter','latex');
ylabel('$\textbf{Total delay [S]}$','FontWeight','bold','FontSize',12,...
'FontName','Palatino Linotype','Interpreter','latex');
ylim([0 12])
% assemble the legend
legendstr = {'\textbf{Proposed framework without AES}', ...
'\textbf{Proposed framework with AES-128}', ...
'\textbf{Proposed framework with AES-256}', ...
'\textbf{Delay-energy-aware without AES}', ...
'\textbf{Delay-energy-aware with AES-128}', ...
'\textbf{Delay-energy-aware with AES-256}'};
[~,hlobj] = legendflex(h1,legendstr,'Interpreter','latex', ...
'Anchor',{'nw','nw'}, ...
'Buffer',[10 -10], ...
'FontWeight','bold', ...
'FontSize',9.5, ...
'FontName','Palatino Linotype');
% hatch the legend patches to match the bars
for k = 4:6
hhf(k) = hatchfill2(hlobj(k+6).Children,hfstyle{k-3}, ...
'HatchAngle',hfangle(k-3), ... % use the same parameters as before
'HatchColor',hfcolor(k-3,:), ...
'HatchDensity',20); % but density is different
end
As I think Walter discussed in that thread, the angle and density of the fill generated by hatchfill()/hatchfill2() depends on the geometry of the parent axes. That makes it problematic to try to get the hatch patterns to match exactly between the bar chart and the patch in the legend object. You may have to play with trying to adjust the two density parameters as best you can.

11 Comments

Really, Thank you so much for your great effort.
I deeply appreciate that.
Please, the last thing, do you know how to remove the offset between -2 and 0 because I do not want it to appear.
Thanks in advance
Also, please when I try your code. It gives me this error.
Error: Unrecognized function or variable 'legendflex'.
Can you please share with me the link to install the correct version of function legendflex. I'm using MATLAB R2022a.
Thank you so much
For some reason the forum editor arranged the axes with that odd range, whereas I don't see that on desktop. If you have similar issues, you should be able to explicitly specify the range using
ylim([0 12])
I've edited the example above to do that.
The necessary tools are on the FEX:
Though if I recall, there may be some patches necessary to make those tools work correctly in modern versions. That said, all necessary FEX tools are attached above in their patched form. (getpos/setpos are from legendflex())
Thank you sooo much for you continous help.
I tried to intasll hatchfill2 and legendflex from the above links, however, unfortunaltelly, it seems that they are incompatible with my MATLAB version.. Can you kindly let me know your MATLAB version to install?
Thank you a million.
What version are you using? That should work in any version in the last 10 years or so. I've tested it in R2015b, R2019b, and here using R2024a.
My guess is that the function files are not on the path. You can use the pathtool() dialog to add or remove things from the path, otherwise if the files are in the current working directory, they'll be found.
If you're getting some other kind of error, you'll have to say what the error is.
Thank you sooo much really for your effort.
It works now.
I'm sorry, I have another question.. If I change the input y1 to be like this
y1=[0.6 41.70;0.6 44.19 ;0.6 47.89 ;0.6 51.33 ;0.6 51.95];
and I need to make the first bar only which has values (0.6,0.6,0.6,0.6,0.6) to be hatched.. how can I make this please ? .. because I tried to modify the above code to fit what I want but it did not work.
And again thank you for your contnous help.
I appreciate that
Let's back up for a minute and try to make the previous example easier to use.
% the data
x = [1,2,3,4,5];
y1 = [0.25 1.14 2.20 0.21 1.09 2.16 ;
0.48 2.26 4.40 0.42 2.20 4.34;
0.72 3.38 6.58 0.74 3.27 5.86;
1.01 4.56 8.82 0.99 4.34 7.65;
1.33 5.76 11.04 1.33 5.50 9.61];
% parameters for all bars
CT = lines(7); % or replace with the desired colormap
CT = CT([1 2 3 1 5 3],:); % rearrange the order as before
% which bars to hatch
% the hatchfill parameters should be (at least) the same length
hatchbars = [4 5 6];
% hatchfill parameters
hfstyle = {'cross','cross','cross'};
hfangle = [45 45 45];
% create the initial plot
colororder(CT); % set the color order
hb = bar(y1);
% generate hatch fills on the bar objects
nbars = numel(hb); % number of total bars
nhbars = numel(hatchbars); % number of hatched bars
hhf = gobjects(nbars,1);
for k = 1:nhbars
hhf(k) = hatchfill2(hb(hatchbars(k)),hfstyle{k}, ...
'HatchAngle',hfangle(k), ...
'HatchColor',CT(hatchbars(k),:), ...
'HatchDensity',50); % see note
hb(hatchbars(k)).FaceColor = 'none';
end
% figure setup
set(gca,'TickLabelInterpreter','latex', 'LineWidth', 1,'FontSize',12, 'YMinorTick','on');
xlabel('$\textbf{Number of tasks}$','FontWeight','bold','FontSize',12,...
'FontName','Palatino Linotype','Interpreter','latex');
ylabel('$\textbf{Total delay [S]}$','FontWeight','bold','FontSize',12,...
'FontName','Palatino Linotype','Interpreter','latex');
ylim([0 max(ylim)]) % this is smarter than before
% assemble the legend
legendstr = {'\textbf{Proposed framework without AES}', ...
'\textbf{Proposed framework with AES-128}', ...
'\textbf{Proposed framework with AES-256}', ...
'\textbf{Delay-energy-aware without AES}', ...
'\textbf{Delay-energy-aware with AES-128}', ...
'\textbf{Delay-energy-aware with AES-256}'};
[~,hlobj] = legendflex(hb,legendstr,'Interpreter','latex', ...
'Anchor',{'nw','nw'}, ...
'Buffer',[10 -10], ...
'FontWeight','bold', ...
'FontSize',9.5, ...
'FontName','Palatino Linotype');
% hatch the legend patches to match the bars
for k = 1:nhbars
hhf(hatchbars(k)) = hatchfill2(hlobj(hatchbars(k)+nbars).Children,hfstyle{k}, ...
'HatchAngle',hfangle(k), ... % use the same parameters as before
'HatchColor',CT(hatchbars(k),:), ...
'HatchDensity',20); % but density is different
end
In this revised example, it should be a lot easier to control which bars get hatched. We just need to make sure the color table and parameter vectors are long enough to handle the respective numbers of bars. All the indexing should be taken care of.
Now that it's easier to use, changing it to suit the new requirements really just involves changing the variable hatchbars and adjusting the parameters and legend strings to suit. The rest of the code is the same.
figure
% the data
x = [1,2,3,4,5];
y1 = [6 41.70; % i'm changing this so that we can see the fill
0.6 44.19;
0.6 47.89;
0.6 51.33;
0.6 51.95];
% parameters for all bars
CT = lines(7); % or replace with the desired colormap
% which bars to hatch
% the hatchfill parameters should be the (at least) same length
hatchbars = [1];
% hatchfill parameters
hfstyle = {'cross'};
hfangle = [45];
% create the initial plot
colororder(CT); % set the color order
hb = bar(y1);
% generate hatch fills on the bar objects
nbars = numel(hb); % number of total bars
nhbars = numel(hatchbars); % number of hatched bars
hhf = gobjects(nbars,1);
for k = 1:nhbars
hhf(k) = hatchfill2(hb(hatchbars(k)),hfstyle{k}, ...
'HatchAngle',hfangle(k), ...
'HatchColor',CT(hatchbars(k),:), ...
'HatchDensity',50); % see note
hb(hatchbars(k)).FaceColor = 'none';
end
% figure setup
set(gca,'TickLabelInterpreter','latex', 'LineWidth', 1,'FontSize',12, 'YMinorTick','on');
xlabel('$\textbf{Number of tasks}$','FontWeight','bold','FontSize',12,...
'FontName','Palatino Linotype','Interpreter','latex');
ylabel('$\textbf{Total delay [S]}$','FontWeight','bold','FontSize',12,...
'FontName','Palatino Linotype','Interpreter','latex');
ylim([0 max(ylim)])
% assemble the legend
legendstr = {'\textbf{Thing 1 (hatched)}', ...
'\textbf{Thing 2 (plain)}'};
[~,hlobj] = legendflex(hb,legendstr,'Interpreter','latex', ...
'Anchor',{'nw','nw'}, ...
'Buffer',[10 -10], ...
'FontWeight','bold', ...
'FontSize',9.5, ...
'FontName','Palatino Linotype');
% hatch the legend patches to match the bars
for k = 1:nhbars
hhf(hatchbars(k)) = hatchfill2(hlobj(hatchbars(k)+nbars).Children,hfstyle{k}, ...
'HatchAngle',hfangle(k), ... % use the same parameters as before
'HatchColor',CT(hatchbars(k),:), ...
'HatchDensity',10); % but density is different
end
As before, the relative density of the hatchfills in the legend may need to be tweaked. The aspect ratio of the fill in the legend is still distorted, but that's due to the relative aspect ratios of the plot box and the legend box. As far as I know, there isn't a simple way to fix that without modifying hatchfill2().
Really, that's amazing !!..
Thank you sooooo mcuh for your continous help.
I tried to amke the solid bar to be blue color instead of orange color.. but the legend is still in orange,, is there anyway to make it aslo blue...and really thank you for your support and help.
Hope you all the best
If you want both bars to use the same color, you can do:
% parameters for all bars
CT = lines(7); % or replace with the desired colormap
CT = CT([1 1],:); % [blue; blue]
Thank you for your comment. However, I could make the bar blue, but still the legend orange. I cannot change it
below is the figure attached
That's odd. Again, I'm running an older version, so let me check here with the full code:
% the data
x = [1,2,3,4,5];
y1 = [6 41.70; % i'm changing this so that we can see the fill
0.6 44.19;
0.6 47.89;
0.6 51.33;
0.6 51.95];
% parameters for all bars
CT = lines(7); % or replace with the desired colormap
CT = CT([1 1],:);
% which bars to hatch
% the hatchfill parameters should be the (at least) same length
hatchbars = [1];
% hatchfill parameters
hfstyle = {'cross'};
hfangle = [45];
% create the initial plot
colororder(CT); % set the color order
hb = bar(y1);
% generate hatch fills on the bar objects
nbars = numel(hb); % number of total bars
nhbars = numel(hatchbars); % number of hatched bars
hhf = gobjects(nbars,1);
for k = 1:nhbars
hhf(k) = hatchfill2(hb(hatchbars(k)),hfstyle{k}, ...
'HatchAngle',hfangle(k), ...
'HatchColor',CT(hatchbars(k),:), ...
'HatchDensity',50); % see note
hb(hatchbars(k)).FaceColor = 'none';
end
% figure setup
set(gca,'TickLabelInterpreter','latex', 'LineWidth', 1,'FontSize',12, 'YMinorTick','on');
xlabel('$\textbf{Number of tasks}$','FontWeight','bold','FontSize',12,...
'FontName','Palatino Linotype','Interpreter','latex');
ylabel('$\textbf{Total delay [S]}$','FontWeight','bold','FontSize',12,...
'FontName','Palatino Linotype','Interpreter','latex');
ylim([0 max(ylim)])
% assemble the legend
legendstr = {'\textbf{Thing 1 (hatched)}', ...
'\textbf{Thing 2 (plain)}'};
[~,hlobj] = legendflex(hb,legendstr,'Interpreter','latex', ...
'Anchor',{'nw','nw'}, ...
'Buffer',[10 -10], ...
'FontWeight','bold', ...
'FontSize',9.5, ...
'FontName','Palatino Linotype');
% hatch the legend patches to match the bars
for k = 1:nhbars
hhf(hatchbars(k)) = hatchfill2(hlobj(hatchbars(k)+nbars).Children,hfstyle{k}, ...
'HatchAngle',hfangle(k), ... % use the same parameters as before
'HatchColor',CT(hatchbars(k),:), ...
'HatchDensity',10); % but density is different
end
You might want to double check that the code you're using follows this example. I suspect there's some part of the older example lingering in what you've adapted.
The object in question is a descendant patch object within hlobj. For N legend entries, hlobj will contain N text objects, followed by N hggroups. Within each group, there may be different things. For this normal bar, it will contain a lone patch object. For hatched bars, it will contain a line object and a patch object.
% hlobj contains the legend text and patch objects
hlobj % inspect the contents
hlobj =
4x1 graphics array: Text (\textbf{Thing 1 (hatched)}) Text (\textbf{Thing 2 (plain)}) Group (\textbf{Thing 1 (hatched)}) Group (\textbf{Thing 2 (plain)})
% inspect the legend patch associated with the solid bars
findobj(hlobj(4),'type','patch') % the facecolor is blue
ans =
Patch (\textbf{Thing 2 (plain)}) with properties: FaceColor: [0 0.4471 0.7412] FaceAlpha: 1 EdgeColor: [0 0 0] LineStyle: '-' Faces: [1 2 3 4 5] Vertices: [5x2 double] Use GET to show all properties
This might be an inroads to troubleshooting. If nothing else, the face color can be directly set using this information.
Otherwise, if you want help finding the problem in the code as you've written it, I'd need to see how you've adapted it.

Sign in to comment.

More Answers (1)

x=[1,2,3,4,5];
y1=[0.25 1.14 2.20 0.21 1.09 2.16 ; 0.48 2.26 4.40 0.42 2.20 4.34; 0.72 3.38 6.58 0.74 3.27 5.86 ;1.01 4.56 8.82 0.99 4.34 7.65;1.33 5.76 11.04 1.33 5.50 9.61 ];
figure
hB = bar(y1);
C=[0.9 0.9 0.9]; % custom color rgb triplet
set(hB,{'FaceColor'},{'flat'}); % must use face color to control individual bars -- pain, but is what it is
for i=4:6 % last threee of six -- generalize for function instead hardcoding
hB(i).CData=C; % and set those to the custom color
end
hAx=gca;
set(hB, {'DisplayName'}, {'\textbf{Proposed framework without AES}','\textbf{Proposed framework with AES-128}','\textbf{Proposed framework with AES-256}','\textbf{Delay-energy-aware without AES}','\textbf{Delay-energy-aware with AES-128}','\textbf{Delay-energy-aware with AES-256}'}')
set(hAx,'TickLabelInterpreter','latex', 'LineWidth', 1,'FontSize',12, 'YMinorTick','on');
legend('Location','northwest','Interpreter','latex', 'FontWeight','bold','FontSize',9.5,...
'FontName','Palatino Linotype',...
'Location','best');
xlabel('$\textbf{Number of tasks}$','FontWeight','bold','FontSize',12,...
'FontName','Palatino Linotype','Interpreter','latex');
ylabel('$\textbf{Total delay [S]}$','FontWeight','bold','FontSize',12,...
'FontName','Palatino Linotype','Interpreter','latex');
Color is not that bad; cross-hatching is a sore point in MATLAB -- after all these years there still are no builtin hatching patterns. There are several attempts with File Exchange submittals but I've found none that are really satisfactory, unfortunately. I haven't looked into it for quite a while now, however, ... but why not a standard part of HG2 is a glaring weakness wouldn't count on being fixed any time realsoonnow™; I began complaining about and submitting enhancement requests with the original 3.1 release some 30 years ago by now...I finally gave it up as lost cause.
ADDENDUM/ERRATUM
Oh, except on reading the Q?, again, you want the cross-hatching of a given color, not the bars...that'll be whether one of the FEX submissions will actually work satisfactorily now or take drawing the lines directly.
<hatchfill2> says it supports bar now; as said, I've not tried recently; the version on the FEX page indicates it is later than when I gave up the consulting gig and the need went away with it, so you may have a reasonable chance now.

Asked:

on 31 Jul 2024

Edited:

DGM
on 6 Aug 2024

Community Treasure Hunt

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

Start Hunting!