Plot hatched bars with the hatched legends

The first problem I have 6 bars, I need to make the last three bars (number 5,6,7) hatched for each point on the x axis (such as point 1 , point 2...until point 5). but this code makes the bars number 4,5, and 6 are hatched not the bars number 5,6,7.
The second problem I need to show the hatched in the legend.. this code deos not appear them.
Furthermore, I used here the libraies hatchfill2_r8 and legendflex
This is my code
x = [1,2,3,4,5];
y1=[0.405 0.25 1.14 1.39 0.20 1.09 1.34 ; 0.62 0.47 2.25 2.76 0.42 2.13 2.62;0.88 0.82 3.48 4.24 0.68 3.20 3.92 ;1.26 1.16 4.71 5.72 1.04 4.38 5.34 ;1.71 1.40 5.83 7.09 1.13 5.31 6.50 ];%all
h1 = bar(y1);
% hatchfill parameters
cm = colororder; % or replace with the desired colormap
hfcolor = cm([1 2 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 9])
% assemble the legend
legendstr = {'\textbf{Kunlun model}', ...
'\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

 Respuesta aceptada

Mathieu NOE
Mathieu NOE el 4 de Feb. de 2025
hello is this the correct result you expect ?
two simple corrections (wrong indexes)
1/ to get the correct bars hatched :
for k = 1:3 hhf(k) = hatchfill2(h1(k+3),hfstyle{k}, ...
replaced with
for k = 1:3 hhf(k) = hatchfill2(h1(k+4),hfstyle{k}, ...
2/ to correct the legend
for k = 4:6 hhf(k) = hatchfill2(hlobj(k+6).Children,hfstyle{k-3}, ..
replaced with :
for k = 4:6 hhf(k) = hatchfill2(hlobj(k+8).Children,hfstyle{k-3}, ...
full code :
x = [1,2,3,4,5];
y1=[0.405 0.25 1.14 1.39 0.20 1.09 1.34 ; 0.62 0.47 2.25 2.76 0.42 2.13 2.62;0.88 0.82 3.48 4.24 0.68 3.20 3.92 ;1.26 1.16 4.71 5.72 1.04 4.38 5.34 ;1.71 1.40 5.83 7.09 1.13 5.31 6.50 ];%all
h1 = bar(y1);
% hatchfill parameters
cm = colororder; % or replace with the desired colormap
hfcolor = cm([1 2 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+4),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 9])
% assemble the legend
legendstr = {'\textbf{Kunlun model}', ...
'\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+8).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

18 comentarios

bassant tolba
bassant tolba el 4 de Feb. de 2025
Editada: bassant tolba el 4 de Feb. de 2025
Really, thank you so much for your efforts, However, I tried to run your above code, but I still have the same error that I got before about not showing the hatching in the legend unfortunately, can you please help me to solve this
This is error
Check for incorrect argument data type or missing argument in call to function 'getpos'.
Error in legendflex (line 465)
legpospx = getpos(h.leg, 'px');
Error in file (line 33)
[~,hlobj] = legendflex(h1,legendstr,'Interpreter','latex', ...
bassant tolba
bassant tolba el 4 de Feb. de 2025
or if you have specific libraries should i install, please share them with me. Thank you so much really
Mathieu NOE
Mathieu NOE el 4 de Feb. de 2025
I just ran the code you provided and made the small corrections
I just needed to download the two Fex submissions legendflex and hatchfill2
nothing more , nothing less
do you have more code on your side that could impact , for example h1 object ?
bassant tolba
bassant tolba el 4 de Feb. de 2025

Really, I do not know.. and I tried to modify but I couldn’t. so, please can you kindly share with me the installed files of hatchfil and legend flex please to run them with my code. Thank you so much

Mathieu NOE
Mathieu NOE el 5 de Feb. de 2025
can you share your code so I can test it on my side ?
attached my code (tested ok again) + functions
all the best
bassant tolba
bassant tolba el 5 de Feb. de 2025
Really, I can not thank you enough for your help. I tried your code on my side and it worked correctly. The last two questions I'm sorry, they may be trivial.
The first question when I change the color of the bars, it does not change in the legend .. so how can i make them change in legend ..
The second question, I need to change the color of the last hatched bar (which is red).. how can I make it also?
and really thank you a million
no problem, you can do this way - here I sipmly picked one of the already predefined colormap and uses it for bars and legend
or you can use your own colors , that must be defined as a N x 3 array , in the order you prefer
this is my result here with a HSV colormap . for sake of simplicity the hatch color is by default.
code modified :
NB I don't use any color argumant here when I call legendflex or hatchfill2 - I keep the colors I have used in the bar plot section.
x = [1,2,3,4,5];
data=[0.405 0.25 1.14 1.39 0.20 1.09 1.34 ; 0.62 0.47 2.25 2.76 0.42 2.13 2.62;0.88 0.82 3.48 4.24 0.68 3.20 3.92 ;1.26 1.16 4.71 5.72 1.04 4.38 5.34 ;1.71 1.40 5.83 7.09 1.13 5.31 6.50 ];%all
cm = colormap('hsv');
[mmap,nmap] = size(cm);
% grouped bar plot with colors defined by j index going from 1 to n in N discrete values - mapping the entire range of
% selected colormap
h1 = bar(data);
[M,N] = size(data);
for j = 1:N
ind = fix(1+(mmap-1)*(j-1)/(N-1));
col = cm(ind,:);
h1(j).FaceColor = 'flat';
h1(j).CData = col;
end
% hatchfill parameters
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+4),hfstyle{k}, ...
'HatchAngle',hfangle(k), ...
'HatchDensity',50); % see note
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 9])
% assemble the legend
legendstr = {'\textbf{Kunlun model}', ...
'\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+8).Children,hfstyle{k-3}, ...
'HatchAngle',hfangle(k-3), ... % use the same parameters as before
'HatchDensity',20); % but density is different
end
more fun with jet colormap - used in reversed order :
x = [1,2,3,4,5];
data=[0.405 0.25 1.14 1.39 0.20 1.09 1.34 ; 0.62 0.47 2.25 2.76 0.42 2.13 2.62;0.88 0.82 3.48 4.24 0.68 3.20 3.92 ;1.26 1.16 4.71 5.72 1.04 4.38 5.34 ;1.71 1.40 5.83 7.09 1.13 5.31 6.50 ];%all
cm = colormap('jet');
% cm = colormap('lines');
[mmap,nmap] = size(cm);
% grouped bar plot with colors defined by j index going from 1 to n in N discrete values - mapping the entire range of
% selected colormap
h1 = bar(data);
[M,N] = size(data);
for j = 1:N
% for gradually changing colormaps use these lines
ind = fix(1+(mmap-1)*(j-1)/(N-1));
% col = cm(ind,:); % ascending order
col = cm(mmap + 1 - ind,:); % descending order
% for discrete colormap like 'lines' use this line
% col = cm(j,:); % ascending order
% col = cm(N+1- j,:); % descending order
h1(j).FaceColor = 'flat';
h1(j).CData = col;
end
% hatchfill parameters
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+4),hfstyle{k}, ...
'HatchAngle',hfangle(k), ...
'HatchDensity',50); % see note
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 9])
% assemble the legend
legendstr = {'\textbf{Kunlun model}', ...
'\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+8).Children,hfstyle{k-3}, ...
'HatchAngle',hfangle(k-3), ... % use the same parameters as before
'HatchDensity',20); % but density is different
end
and if you have defined your own prefered colors lke an the example below :
x = [1,2,3,4,5];
data=[0.405 0.25 1.14 1.39 0.20 1.09 1.34 ; 0.62 0.47 2.25 2.76 0.42 2.13 2.62;0.88 0.82 3.48 4.24 0.68 3.20 3.92 ;1.26 1.16 4.71 5.72 1.04 4.38 5.34 ;1.71 1.40 5.83 7.09 1.13 5.31 6.50 ];%all
% my 8 prefered colors
% [0.0,0.0,0.3] % dark blue
% [1.0,1.0,0.0] % bright yellow
% [0.0,1.0,0.0] % bright gren
% [0.3,0.3,0.3] % dark gray
% [1.0,0.4,0.2] % orange
% [0.0,1.0,1.0] % light blue
% [1.0,0.0,1.0] % pink
% [0.9,0.9,0.9] % light gray
cm = [[0.0,0.0,0.3]
[1.0,1.0,0.0]
[0.0,1.0,0.0]
[0.3,0.3,0.3]
[1.0,0.4,0.2]
[0.0,1.0,1.0]
[1.0,0.0,1.0]
[0.9,0.9,0.9]];
[mmap,nmap] = size(cm);
% grouped bar plot with colors defined by j index going from 1 to n in N discrete values
h1 = bar(data);
[M,N] = size(data);
for j = 1:N
col = cm(j,:); % ascending order
% col = cm(N+1- j,:); % descending order
h1(j).FaceColor = 'flat';
h1(j).CData = col;
end
% hatchfill parameters
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+4),hfstyle{k}, ...
'HatchAngle',hfangle(k), ...
'HatchDensity',50); % see note
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 9])
% assemble the legend
legendstr = {'\textbf{Kunlun model}', ...
'\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+8).Children,hfstyle{k-3}, ...
'HatchAngle',hfangle(k-3), ... % use the same parameters as before
'HatchDensity',20); % but density is different
end
Mathieu NOE
Mathieu NOE el 5 de Feb. de 2025
fyi , you can use the colormapeditor command to create your own colormap
bassant tolba
bassant tolba el 5 de Feb. de 2025
Editada: bassant tolba el 5 de Feb. de 2025
Really this is amazing !!.. Thank you so muuuuuch for your effort. I really appreciate it.
The last question if you do not mind :D.. I will use the code of the last answer your provided which has the eight prefered colors, so is there any option to change the color of the hatched lines which is on bars also ?
and reallyyy thank you soo much...Hope you all the best
what color would you like for the hatches ? same or individual ? for the last case , you already coded it in your original version of the code :
'HatchColor',hfcolor(k-3,:), ...
in this section
% hatch the legend patches to match the bars
for k = 4:6
hhf(k) = hatchfill2(hlobj(k+8).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
bassant tolba
bassant tolba el 5 de Feb. de 2025
yeah it worked !!.
Really, thank you sooo much..I really appreciate that.. I hope you all the best in your career really...
tx u2
and if you like to play further with colors / colormaps , there are several good Fex submissions like
here one last example with : cm=slanCM('tab10',N);
x = [1,2,3,4,5];
data=[0.405 0.25 1.14 1.39 0.20 1.09 1.34 ; 0.62 0.47 2.25 2.76 0.42 2.13 2.62;0.88 0.82 3.48 4.24 0.68 3.20 3.92 ;1.26 1.16 4.71 5.72 1.04 4.38 5.34 ;1.71 1.40 5.83 7.09 1.13 5.31 6.50 ];%all
% grouped bar plot with colors defined by j index going from 1 to n in N discrete values
h1 = bar(data);
[M,N] = size(data);
cm=slanCM('tab10',N);
for j = 1:N
col = cm(j,:); % ascending order
% col = cm(N+1- j,:); % descending order
h1(j).FaceColor = 'flat';
h1(j).CData = col;
end
% hatchfill parameters
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+4),hfstyle{k}, ...
'HatchAngle',hfangle(k), ...
'HatchDensity',50); % see note
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 9])
% assemble the legend
legendstr = {'\textbf{Kunlun model}', ...
'\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+8).Children,hfstyle{k-3}, ...
'HatchAngle',hfangle(k-3), ... % use the same parameters as before
'HatchDensity',20); % but density is different
end
bassant tolba
bassant tolba el 5 de Feb. de 2025
Editada: Mathieu NOE el 5 de Feb. de 2025
Ooh this is really amazing..
I will try it..also, please, I have one further question I’m sorry.. now I added a new bar.. and I tried to fix the indices like you showed me before, but the last bar does not appear in the legend.. kindly, please how can I fix it? …
Thank you sooo muchhhh
Here is my code.
x = [1,2,3,4,5];
data=[0.405 0.333 0.25 1.14 1.39 0.20 1.09 1.34 ; 0.62 0.52 0.47 2.25 2.76 0.42 2.13 2.62;0.88 0.86 0.82 3.48 4.24 0.68 3.20 3.92 ;1.26 1.21 1.16 4.71 5.72 1.04 4.38 5.34 ;1.71 1.55 1.40 5.83 7.09 1.13 5.31 6.50 ];%all
% my 8 prefered colors
% [0.0,0.0,0.3] % dark blue
% [1.0,1.0,0.0] % bright yellow
% [0.0,1.0,0.0] % bright gren
% [0.3,0.3,0.3] % dark gray
% [1.0,0.4,0.2] % orange
% [0.0,1.0,1.0] % light blue
% [1.0,0.0,1.0] % pink
% [0.9,0.9,0.9] % light gray
cm=[[0.4940 0.1840 0.5560]
[0.3010 0.7450 0.9330]
[0 0.4470 0.7410]
[0.8500 0.3250 0.0980]
[0.9290 0.6940 0.1250]
[1, 1, 1]
[1, 1, 1]
[1, 1, 1]]
% cm = [[0.0,0.0,0.3]
% [1.0,1.0,0.0]
% [0.0,1.0,0.0]
% [0.3,0.3,0.3]
% [1.0,0.4,0.2]
% [0.0,1.0,1.0]
% [1.0,0.0,1.0]
% [0.9,0.9,0.9]];
[mmap,nmap] = size(cm);
% grouped bar plot with colors defined by j index going from 1 to n in N discrete values
h1 = bar(data);
[M,N] = size(data);
for j = 1:N
col = cm(j,:); % ascending order
% col = cm(N+1- j,:); % descending order
h1(j).FaceColor = 'flat';
h1(j).CData = col;
end
% hatchfill parameters
hfstyle = {'cross','cross','cross'};
hfangle = [45 45 45];
hfcolor = cm([3 4 5],:);
% generate hatch fills on the bar objects
hhf = gobjects(6,1);
for k = 1:3
hhf(k) = hatchfill2(h1(k+5),hfstyle{k}, ...
'HatchAngle',hfangle(k), ...
'HatchColor',hfcolor(k,:), ...
'HatchDensity',50); % see note
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 9])
% assemble the legend
legendstr = {'\textbf{Kunlun model}', ...
'\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+9).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
I prefered to change a bit the way everything is indexed in your code to make it more robust
so , normally, with the new code below , there is no risks of bad surprises (until next arrises !)
the only paramter you may want to change is the numerical value in this line : here we want the 5th and following bars to be white and hatched.
% define which bars must be hatched
% the numbers correspond to the bars that must appear white and hatched
col_index_hat = (5:N);
this defines which bars must be white and hatched (if I understand correctly what you want)
you can test the code with a different value (3 or 4 instead of 5) and see the effect
also you must ensure that the legendd size is matching the data size , otherwise you will get a wrong legend plot.
that's why I added this piece of code to add asmany legend entries as needed - but of course it's now your task to fill the legend with the right info's.
% if more data is added then we need to add also new legend entries
if numel(legendstr)< N
for k =1:N-numel(legendstr)
legendstr =[legendstr, '\textbf{new data}']; % add new string cells
end
end
this should be the result with the first data set (5 x 7 )
and with the larger data set (5 x 8)
full code :
% x = [1,2,3,4,5]; % seems we don't need this line
% 5 by 7 data
data=[0.405 0.25 1.14 1.39 0.20 1.09 1.34 ; 0.62 0.47 2.25 2.76 0.42 2.13 2.62;0.88 0.82 3.48 4.24 0.68 3.20 3.92 ;1.26 1.16 4.71 5.72 1.04 4.38 5.34 ;1.71 1.40 5.83 7.09 1.13 5.31 6.50 ];%all
% 5 by 8 data
% data=[0.405 0.333 0.25 1.14 1.39 0.20 1.09 1.34 ; 0.62 0.52 0.47 2.25 2.76 0.42 2.13 2.62;0.88 0.86 0.82 3.48 4.24 0.68 3.20 3.92 ;1.26 1.21 1.16 4.71 5.72 1.04 4.38 5.34 ;1.71 1.55 1.40 5.83 7.09 1.13 5.31 6.50 ];%all
% general colormap for bars (no need here to repeat a white line multiple times at the bottom)
cm=[[0.4940 0.1840 0.5560]
[0.3010 0.7450 0.9330]
[0 0.4470 0.7410]
[0.8500 0.3250 0.0980]
[0.9290 0.6940 0.1250]];
[mmap,nmap] = size(cm);
% colormap for hatches
hfcolor = [
[0.0,0.0,0.3] % dark blue
[0.0,1.0,0.0] % bright gren
[1.0,0.0,1.0] % pink
[1.0,0.4,0.2] % orange
[0.0,1.0,1.0] % light blue
[0.3,0.3,0.3] % dark gray
[0.9,0.9,0.9] % light gray
[1.0,1.0,0.0] % bright yellow
];
% grouped bar plot with colors defined by j index going from 1 to n in N discrete values
h1 = bar(data);
[M,N] = size(data);
% define which bars must be hatched
% the numbers correspond to the bars that must appear white and hatched
col_index_hat = (5:N);
% color management (hatched vs. non hatched bars)
for j = 1:N
% either pick a "true" color or white according to bar number
if j>=col_index_hat(1) % use white col only for hatched bars
col = [1, 1, 1];
else
col = cm(j,:); % ascending order
% col = cm(N+1- j,:); % descending order
end
h1(j).FaceColor = 'flat';
h1(j).CData = col;
end
% hatchfill parameters (valid whatever the number of hatched bars)
hfstyle = 'cross';
hfangle = [45];
% generate hatch fills on the bar objects
% hhf = gobjects(6,1); % seems we don't need this line
for k = 1:numel(col_index_hat)
hhf(k) = hatchfill2(h1(col_index_hat(k)),hfstyle, ...
'HatchAngle',hfangle, ...
'HatchColor',hfcolor(k,:), ...
'HatchDensity',50); % see note
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 9])
% assemble the legend
legendstr = {'\textbf{Kunlun model}', ...
'\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}'};
% if more data is added then we need to add also new legend entries
if numel(legendstr)< N
for k =1:N-numel(legendstr)
legendstr =[legendstr, '\textbf{new data}']; % add new string cells
end
end
[~,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 = 1:numel(col_index_hat)
hhf(k) = hatchfill2(hlobj(col_index_hat(k)+N).Children,hfstyle, ...
'HatchAngle',hfangle, ... % use the same parameters as before
'HatchColor',hfcolor(k,:), ...
'HatchDensity',20); % but density is different
end
bassant tolba
bassant tolba el 5 de Feb. de 2025
Really thank you soo much for your effort. I deeply appreciate that.. you helped me so much.. I hope you all the best and moreee.
Mathieu NOE
Mathieu NOE el 6 de Feb. de 2025
tx
have a nice day

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 4 de Feb. de 2025

Comentada:

el 6 de Feb. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by