Filter löschen
Filter löschen

Struggling with legend on grouped bar charts (Matlab)

20 Ansichten (letzte 30 Tage)
NC
NC am 14 Jun. 2015
Bearbeitet: NC am 16 Jun. 2015
Hi,
I am struggling to set up properly the legend on a grouped bar chart that I have generated. My grouped bar chart looks like this:
As it can be seen on the legend, the second entry (2015 Data) appears on dark blue which is the same colour as the first entry (2014 Data). However, it should appear on light blue colour instead. This is the code that I am using:
bar(1:numel(Groups),[element1_2014 element2_2014 element3_2014],1,'FaceColor',[0.2,0.2,0.5]);
tickStep=1;
set(gca,'xtick',1:tickStep:numel(Groups))
set(gca,'xticklabel',Groups(1:tickStep:numel(Groups)))
set(gca,'ytick',-0.3:0.1:0.3)
axis([-Inf Inf -0.3 0.3])
hold on
bar(1:numel(Groups),[element1_2014 element2_2015 element3_2015],0.5,'FaceColor',[0,0.7,0.7],'EdgeColor',[0,0.7,0.7]);
hold off
legend('2014 Data','2015 Data')
Does anybody know how can I fix the second legend entry (2015 Data) so that it appears in light blue?. Thank you!.
  4 Kommentare
Star Strider
Star Strider am 15 Jun. 2015
We do not have your data, so we cannot run your code. There may be something about your data that would cause the result you posted in your original Question.
NC
NC am 15 Jun. 2015
@Star Strider: I have used the following data:
Groups={'Group A'; 'Group B'; 'Group C'; 'Group D'; 'Group E'; 'Group F'};
element1_2014=[-0.027574; -0.064481; 0.017968; 0.045343; 0.720751; -0.104822];
element2_2014=[0.437578; 0.610541; 0.210301; 0.256830; 4.469348; 0.775665];
element3_2014=[0.857653; 1.196660; 0.412191; 0.503386; 8.759922; 1.520304];
element1_2015=[0.031310; -0.064481; -0.000531; 0.027406; 0.720751; -0.050727];
element2_2015=[0.420139; 0.610541; 0.108316; 0.318413; 4.469348; 0.781778];
element3_2015=[0.823472; 1.196660; 0.212300; 0.624089; 8.759922; 1.532285];

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Mike Garrity
Mike Garrity am 15 Jun. 2015
If you use
legend show
instead of passing in the two names, you'll see what's going on here. In that case you'll get six legend entries. That's because you have six bar series. In your case, the call to legend is saying that it should create legend entries for the first two, which are both blue.
There are a copy of different ways to do what you want. The simplest is probably something like this:
h2014 = bar(1:numel(Groups),[element1_2014 element2_2014 element3_2014], ...
1,'FaceColor',[0.2,0.2,0.5]);
tickStep=1;
set(gca,'xtick',1:tickStep:numel(Groups))
set(gca,'xticklabel',Groups(1:tickStep:numel(Groups)))
set(gca,'ytick',-0.3:0.1:0.3)
axis([-Inf Inf -0.3 0.3])
hold on
h2015 = bar(1:numel(Groups),[element1_2014 element2_2015 element3_2015], ...
0.5,'FaceColor',[0,0.7,0.7],'EdgeColor',[0,0.7,0.7]);
hold off
legend([h2014(1), h2015(1)], '2014 Data','2015 Data')
That's saying that you want the legend to include the first series of each of your two sets of 3.

Weitere Antworten (1)

Joseph Cheng
Joseph Cheng am 15 Jun. 2015
Well if we start by reading the documentation on legend() we see that we can obtain the handles of a legend and modify the parameters. by adding the following at the end. you change the facecolor of the rectangle shown in the legend box.
legend('2014 Data','2015 Data')
[LEGH,OBJH,OUTH,OUTM] = legend;
set(get(OBJH(4),'children'),'faceColor',[0,0.7,0.7])
Now playing around with the data and the documentation we know that the OBJH is the items in side the legend. and the order indexed is texts from top to bottom, then marker top to bottom. If it is a line plot it'll be the same (texts, line, marker).
Since you specifically are labling and
  1 Kommentar
NC
NC am 16 Jun. 2015
Bearbeitet: NC am 16 Jun. 2015
@Joseph: Thanks for your answer Joseph, I have tried it but it did not work for my case. I thought my problem could be something related with the handles of the legend, I just could not find the way to get it right.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by