How do I fill legend on double y axis plot?
2 views (last 30 days)
Show older comments
Niall Harris on 20 Mar 2023
Answered: Star Strider on 20 Mar 2023
I have a double y axis plot and I have 3 curves on the left axis and 1 on the right. When I add the legend to the plot I can fill out the legend tag for the first 3 curves, but when I try to add the fourth entry an error appears that says there are extra entries. How do I add the tag for the right axis into the legend?
Star Strider on 20 Mar 2023
Without seeing your code (and your MATLAB version, although I’m not certain how relevant that is in this instance), it’s not possible to determine precisely what the problem is.
Using the 'DisplayName' property works here —
t = linspace(0, 1, 250);
y = sin([1;3;5;7]*2*pi*t);
for k = 1:3
hpl(k) = plot(t, y(k,:), 'DisplayName',"Curve "+k);
hpr = plot(t, y(4,:), 'DisplayName',"Curve "+4);
I added the plot handles in case it’s necessary to pass them as the first argument to the legend call, although that’s not necessary here. It may be necessary for you to use them, so I included them.
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!