Legend on bar chart with two y-axis on UIAxes
39 views (last 30 days)
Show older comments
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.
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,
0 Comments
Accepted Answer
dpb
on 20 Jan 2026 at 20:11
Edited: dpb
on 20 Jan 2026 at 21:42
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
dpb
on 21 Jan 2026 at 15:19
Edited: dpb
on 24 Jan 2026 at 14:02
"...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.
More Answers (0)
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!