Plot hatched bars with the hatched legends

조회 수: 15 (최근 30일)
bassant tolba
bassant tolba 2025년 2월 4일
댓글: Mathieu NOE 2025년 2월 6일
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

채택된 답변

Mathieu NOE
Mathieu NOE 2025년 2월 4일
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
bassant tolba
bassant tolba 2025년 2월 5일
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 2025년 2월 6일
tx
have a nice day

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by