Legend on bar chart with two y-axis on UIAxes

I am plotting two different series on a left and a right Y-axis within an app and would like the legend to display the bars in the correct colors.
I have followed the answer provided here but it still only gives me one color. My code snippet is:
cla(app.UIAxes)
y_1 = [10, 15, 7, 12];
y_2 = [2, 5, 3, 8];
x = categorical({'A', 'B', 'C', 'D'});
hold(app.UIAxes,"on")
yyaxis(app.UIAxes,"left")
a=bar(app.UIAxes,x,[y_1;nan(size(y_1))],'grouped','red');
yyaxis(app.UIAxes,"right")
b=bar(app.UIAxes,x,[nan(size(y_2));y_2],'grouped','green');
hold(app.UIAxes,"off")
legend(app.UIAxes,[a,b],"Option 1 (LHS)","Option 2 (RHS)");
The output from the above is:
The full dummy app is attached.
How can I create a legend that shows the Option 2 series with a green bar?
Thanks in advance,

 Accepted Answer

dpb
dpb on 20 Jan 2026
Edited: dpb on 20 Jan 2026
The problem is that in order to use the 'grouped' option on the two axes you had to introduce the column of NaN so bar would offset the locations from the midpoint.
Doing that causes there to be two bar objects for each A and B and your legend() call is showing only the two "A" red bars because you used the array variable names so the entire arrays are passed, not just one for each color of bar. Thus the number of handles in A is all that are needed and to write two labels so the B array of handles is ignored. The warning message may not be noticed in appdesigner or it may simply have not been understood .
Use
hLg=legend(app.UIAxes,[a(1) b(1)],"Option 1 (LHS)","Option 2 (RHS)");
and all will be well. The above uses one handle from A and another from B to display the two colors as desired.
If do the same thing from command line at the legend() call with the two arrays as written will produce
>> legend([hA hB],'A','B')
Warning: Ignoring extra legend entries.
> In legend>process_inputs (line 592)
In legend>make_legend (line 319)
In legend (line 263)
>>

3 Comments

x=categorical(cellstr(['A':'D'].'));
y1 = [10, 15, 7, 12].';
y2 = [2, 5, 3, 8].';
subplot(2,1,1)
yyaxis('left')
hA=bar(x,[y1 nan(size(y1))],'grouped','r');
yyaxis('right')
hB=bar(x,[nan(size(y1)) y2],'grouped','g');
hLg=legend([hA(1) hB(1)],'A','B');
% let's try regular bar() for grins...
subplot(2,1,2)
hA=bar(x,[y1 y2],'grouped');
hA(1).FaceColor='r'; % have to specify color by handle this way
hA(2).FaceColor='g';
hLg=legend('A','B');
There's a bug here in online version -- the bar colors don't show up...
It apparently is ok on OP's desktop in appdesigner, too, but fails here...
For grins, the ordinary bar() grouped plot worked as expected other than not the way the OP wanted the data shown.
Thank you so much - this had me perplexed, but it makes perfect sense now. And I did not notice the legend entries warning - that should have put me on the right track.
dpb
dpb on 21 Jan 2026
Edited: dpb on 24 Jan 2026
"...I did not notice the legend entries warning - that should have put me on the right track. "
It's easy to not see warnings in the appdesigner mode since using the GUI tends to divert one's attention away unless gets a fatal error.
I've always thought the warning message in this case is confusing or at least could be more explicit as to the issue it is complaining about. One gets the same warning if the number of text legend entries outnumbers the number of handles passed as one does here when it's the other way 'round -- the number of handles passed outnumbers the number of text legend entries. It would be much more clear if the message said which case it is; the message as provided always makes one think of there being too many text entries.
I think I recall having submitted a recommendation for this to be enhanced years ago, but it's never been acted upon.

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products

Release

R2025b

Asked:

on 20 Jan 2026

Edited:

dpb
on 24 Jan 2026

Community Treasure Hunt

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

Start Hunting!