How to make a specific bar to be hatched with a specific color
28 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
DGM
le 1 Août 2024
Modifié(e) : DGM
le 1 Août 2024
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 commentaires
DGM
le 6 Août 2024
Modifié(e) : DGM
le 6 Août 2024
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
% inspect the legend patch associated with the solid bars
findobj(hlobj(4),'type','patch') % the facecolor is blue
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.
Plus de réponses (1)
dpb
le 31 Juil 2024
Modifié(e) : dpb
le 31 Juil 2024
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.
0 commentaires
Voir également
Catégories
En savoir plus sur Legend dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!