I want to make this graph. Anyone can help me for code pls. And legend gives error on this code
x=[6,7,8,9,10,11,12,13,14,15,16,17,18];
y1=[32.3, 32, 31.9, 34.4, 39.8, 44.8, 49.6, 54.4, 58.7, 62.6, 65.3, 66.2, 64.8];
y2=[32.6, 32.3, 32.2, 35.2, 41.2, 46.3, 51.3, 56, 60.4, 64.1, 66.8, 67.4, 65.6];
z=[y1;y2];
a=plotyy(x,y1,x,y2);
hold on
y3=[64, 90, 399, 662, 981, 1220, 1366, 1376, 1284, 1072, 777, 469, 221];
y4=[138.138, 138.138, 138.138, 368.368, 644.644, 690.69, 782.782, 736.736, 782.782, 690.69, 690.69, 552.552, 368.368];
u=[y3;y4];
b=plotyy(x,y3,x,y4);
hold off
title('Solar Energy')
xlabel('Time')
ylabel('Temperature')
ylabel(b(2),'Energy Rate')
legend('Collector input temperature','Collector output temperature','Radiation intensity(W/m^2)','Heat(W)'

1 Comment

[Answers Dev] Restored edits and attachment

Sign in to comment.

 Accepted Answer

You do
a=plotyy(x,y1,x,y2);
hold on
b=plotyy(x,y3,x,y4);
The x, y1 plots in the current axes of the time, and then a second axes is created to plot x, y2, leaving the first axes as the current axes, with an output in a of the two axes. Then you "hold on". Then you plotyy(x, y3, x, y4) which plots x, y3 in the current axes, which is the same one plotted to for x, y1 . To plot x, y4, plotyy() then creates a new axes, not the same as the second one that was created for x, y2; the current axes and the new axes are stored in b, and the existing current axes is left the current axes. So now a(1) is an axes that has two children, x,y1 and x,y3; a(2) is a second axes with one child, x,y2; b(1) is the same axes as a(1) so there are the same two children for it; b(2) is a third axes with one child, x,y4 .
At this point you call legend() on the current axes, which is the one that has x, y1 and x, y3 in it, and most recently also wrote to axes b(2) . legend() puts the first two entries against x, y1 and x, y3, and seeing b(2) recently written to, manages to put the third entry against b(2) . But it has lost track of the fact that it wrote to a(2) and doesn't know what to do with the fourth legend entry.
I recommend that you do not use plotyy the second time: that you do something like
hold(a(1), 'on')
hold(a(2), 'on')
plot(a(1), x, y3)
plot(a(2), x, y4)
and that then you are careful about the order of the legend entries.

4 Comments

john parker
john parker on 11 Oct 2018
Sir can u give me full code?
I do not know which legend you intend to associate with which line.
I copied and executed your code, and then I took the time to write up a detailed explanation of why you see what you see, and I made a recommendation as to how you can fix the problem. You made no obvious attempt to think about what I had written, and asked for full replacement code. I cannot provide full replacement code at this time because I do not know which variable (y1, y2, y3, y4) is goes with which legend entry.
t=[6,7,8,9,10,11,12,13,14,15,16,17,18];
cintemp=[32.3, 32, 31.9, 34.4, 39.8, 44.8, 49.6, 54.4, 58.7, 62.6, 65.3, 66.2, 64.8];
couttemp=[32.6, 32.3, 32.2, 35.2, 41.2, 46.3, 51.3, 56, 60.4, 64.1, 66.8, 67.4, 65.6];
radintens=[64, 90, 399, 662, 981, 1220, 1366, 1376, 1284, 1072, 777, 469, 221];
heat=[138.138, 138.138, 138.138, 368.368, 644.644, 690.69, 782.782, 736.736, 782.782, 690.69, 690.69, 552.552, 368.368];
a = plotyy(t, cintemp, t, radintens );
hold(a(1), 'on')
hold(a(2), 'on');
plot(a(1), t, couttemp);
plot(a(2), t, heat);
hold(a(1), 'off');
hold(a(2), 'off');
title(a(1), 'Solar Energy')
xlabel(a(1), 'Time')
ylabel(a(1), 'Temperature')
ylabel(a(2), 'Energy Rate')
set(a(1).Children(2), 'DisplayName', 'Collector input temperature', 'Color', [0 114 189]/255)
set(a(1).Children(1), 'DisplayName', 'Collector output temperature', 'Color', [237 177 32]/255)
set(a(2).Children(2), 'DisplayName', 'Radiation intensity (W/m^2)', 'Color', [246 216 143]/255)
set(a(2).Children(1), 'DisplayName', 'Heat (W)', 'Color', [126 47 142]/255)
legend('show')

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2016a

Community Treasure Hunt

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

Start Hunting!