Format Bar Graph Matlab
3 views (last 30 days)
Show older comments
I use the following code to generate a bar plot:
% generate numbers to plot
a = rand(20,1);
b = rand(20,1)+1;
[i, ~]= size(a);
x = (1:i)';
M =4;
hold on
% generate bar plot for the high values
for k = 0: M
D(k+1,1) = bar((4*k+1 : 4*k+4),b(4*k+1 : 4*k+4),0.3);
set(D(k+1,1),'FaceColor',[0.1*k 0.25*k 1-0.25*k]);
end
% generate bar plot for the smaller values in front
for k = 0: M
E(k+1,1) = bar((4*k+1 : 4*k+4),a(4*k+1 : 4*k+4),0.5);
set(E(k+1,1),'FaceColor',[0.1*k 0.15*k 0.4-0.1*k]);
end
% style
xLabl ={'1', '5', '10' , '20', '1', '5', '10' , '20', '1', '5', '10' , '20', '1', '5', '10' , '20', '1', '5', '10' , '20'};
set(gca, 'XTick', x, 'XTickLabel', xLabl);
axis([0, inf, 0,inf]);
grid;
legend('T_p = 0.5', 'T_p = 1.0', 'T_p = 1.5', 'T_p = 2.0', 'T_p = 3.0', 'Location','northwest');
xlabel('N');
I plot the maximum and mean values of my test results for different combinations of the paramters N and T_p. Results with the same choice for T_p are plotted in the same color, as indicated in the legend. But the color varies slightly for the mean and max value. I was wondering if it is possible to extend the legend to display both corresponding bar colors to the choice of T_p. It should look something like this:

What I am looking for is to extent the legend. As I tried to show with my additions from paint. I want to use the colors of the bars in the background in the legend and specifically for the same values of T_P in the same row. Idealy I could distinguish these to coloumns of the legend with the coloum caption 'mean' and 'max'. Is this possible?
0 Comments
Answers (1)
Image Analyst
on 1 Sep 2015
You can vary the color of each bar individually, as shown in my attached demo.
See Also
Categories
Find more on Legend 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!