Legend on bar chart with two y-axis on UIAxes
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,
Accepted Answer
More Answers (0)
Categories
Find more on Develop Apps Using App Designer 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!