# MATLAB: Create aditional legend on bar chart

barcolorslegend

Hello,
I got a bar chart, and i want to have the bars with different colors. I would like to know if it possible to show sometype of legend in funciton of colors.
I would like to show up, red saying Reaction, green for pressure and etc.
This way i can only show the first one and not able to show the rest.
``X = [10 17 3 25 31 20 11 13 14];myC = [1 0 0; 0 1 0; 1 0 1; 0 0 1; 0.5 0 0.5; 1 0.5 1; 0.5 0 0; 0 0.5 0; 1 0 0.5];figure();fig = bar(X);fig.FaceColor = 'flat';% hold onfig.CData(1,:) = [1 0 0];fig.CData(2,:) = [0 1 0];fig.CData(3,:) = [1 0 1];fig.CData(4,:) = [0 0 1];fig.CData(5,:) = [0.5 0 0.5];fig.CData(6,:) = [1 0.5 1];fig.CData(7,:) = [0.5 0 0];fig.CData(8,:) = [0 0.5 0];fig.CData(9,:) = [1 0 0.5];lgd = legend(fig,{'Reaction';'Pressure';'Load';'Separation';'Tanks';'valves';'XV';'TIC';'Hydrogen'},'location','best');``
If not possible, other suggestion is welcome!
``M=diag(X); figurehB=bar(M,'stacked');arrayfun(@(i) set(hB(i),'FaceColor',myC(i,:)),1:numel(hB))hLg=legend({'Reaction';'Pressure';'Load';'Separation';'Tanks';'valves';'XV';'TIC';'Hydrogen'},'location','best');``
``myC=cell2mat(myC,ones(1,size(myC,1)));set(hB,{'FaceColor'},myC)``