How to create multiple coloured hatched patterns in stacked bar graph?

28 views (last 30 days)
Hello
I have a stacked bar plot as follows with different colours.
However, I want to use coloured patterns instead of flat colours. I have tried to use applyhatch and applyhatch_plusC, but getting some strange errors arising from "hardcopy" function. Can someone please help me to create the hatched bar graph? The hatched pattern needs to be reflected in the legend as well.
The code to generate this graph is shown below.
%% stacked bar graph
clear all;close all;clc;
NumStackElements = 4;
groupLabels = {'20'; '40'; '60'; '80'};
stackDataI(:,:,1) = [4.53 4.84 5.28;
5.63 6.16 6.60;
7.26 8.80 9.68;
9.46 11.00 11.88];
stackDataI(:,:,2) = [3.39 3.63 3.96;
4.22 4.62 4.95;
5.45 6.60 7.26;
7.09 8.25 8.91];
stackDataI(:,:,3) = [2.37 2.53 2.76;
2.94 3.22 3.45;
3.79 4.60 5.06;
4.95 5.75 6.21];
NumGroupsPerAxis = size(stackDataI, 1);
NumStacksPerGroup = size(stackDataI, 2);
% Count off the number of bins
groupBins = 1:NumGroupsPerAxis;
MaxGroupWidth = 0.65; % Fraction of 1. If 1, then we have all bars in groups touching
groupOffset = MaxGroupWidth/NumStacksPerGroup;
figure;
hold on; grid on; box on;
for i=1:NumStacksPerGroup
Y = squeeze(stackDataI(:,i,:));
% Center the bars:
internalPosCount = i - ((NumStacksPerGroup+1) / 2);
% Offset the group draw positions:
groupDrawPos = (internalPosCount)* groupOffset + groupBins;
h(i,:) = bar(Y, 'stacked');
set(h(i,:),'BarWidth',groupOffset);
set(h(i,:),'XData',groupDrawPos);
end
% colors go horizontaly - left to right
% get(groot,'DefaultAxesColorOrder')
set(h(1),'FaceColor',[0.00138953219082905,0.470736452061139,0.750347383047707]); set(h(2),'FaceColor',[0.340435386753126,0.650430754979157,0.835108846688282]); set(h(3),'FaceColor',[0.732746641963872,0.858355720240852,0.933186660490968]);
set(h(4),'FaceColor',[1,0.700416859657249,0.00138953219082905]); set(h(5),'FaceColor',[1,0.757804539138490,0.192681797128300]); set(h(6),'FaceColor',[1,0.883279295970357,0.610930986567856]);
set(h(7),'FaceColor',[0.800000000000000,0.360000000000000,0.360000000000000]); set(h(8),'FaceColor',[0.855210745715609,0.536674386289949,0.536674386289949]); set(h(9),'FaceColor',[0.910884668828161,0.714830940250116,0.714830940250116]);
axis([0.5 4.5 0 30]);
set(gca,'XTickMode','manual');
set(gca,'XTick',1:NumGroupsPerAxis);
set(gca,'XTickLabelMode','manual');
set(gca,'XTickLabel',groupLabels);
set(gca,'TickLabelInterpreter', 'latex');
hx = xlabel('\textbf{Item}','FontWeight','bold','interpreter','latex','FontSize',12);
hy = ylabel('\textbf{Cost}','FontWeight','bold','interpreter','latex','FontSize',12);
hl1 = legend([h(1),h(2),h(3)],{'X-1','X-2','X-3'},'FontSize',12,'Location','NorthWest','interpreter','latex');
ah1=axes('position',get(gca,'position'),'visible','off');
hl2 = legend(ah1,[h(4),h(5),h(6)],{'Y-1','Y-2','Y-3'},'FontSize',12,'Location','NorthEast','interpreter','latex');
ah1=axes('position',get(gca,'position'),'visible','off');
hl3 = legend(ah1,[h(7),h(8),h(9)],{'Z-1','Z-2','Z-3'},'FontSize',12,'Location','NorthEast','interpreter','latex');

Accepted Answer

Sourav Mondal
Sourav Mondal on 26 Jan 2022
Edited: Sourav Mondal on 26 Jan 2022
As MATLAB is yet to provide a solution for this problem, I found a work-around solution without much coding which does not loose the clarity when used in latex compiler.
  1. Save the bar graph as a .emf file.
  2. Open PowerPoint and select insert image option to select this file.
  3. Right click on the image and select ungroup 2-3 times.
  4. Select bar segments and go to edit properties. Then select textured fill option. Foreground can be in black and the background in color.
  5. Then save the image in .pdf format and crop the image as required.
If this file is now used in latex, the resolution remain perfectly fine.

More Answers (0)

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!