How to make a specific bar to be hatched with a specific color
Show older comments
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');
Accepted Answer
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.
Categories
Find more on Annotations 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!






