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

25 次查看(过去 30 天)
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 个评论
bassant tolba
bassant tolba 2024-8-1
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');

请先登录,再进行评论。

采纳的回答

DGM
DGM 2024-8-1
编辑:DGM 2024-8-1
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 个评论
bassant tolba
bassant tolba 2024-8-6
编辑:bassant tolba 2024-8-6
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
DGM
DGM 2024-8-6
编辑:DGM 2024-8-6
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.

请先登录,再进行评论。

更多回答(1 个)

dpb
dpb 2024-7-31
编辑:dpb 2024-7-31
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.

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by