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 件のコメント

Anusha Sridharan
Anusha Sridharan 2018 年 12 月 26 日
[Answers Dev] Restored edits and attachment

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2018 年 10 月 11 日

1 投票

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 件のコメント

john parker
john parker 2018 年 10 月 11 日
Sir can u give me full code?
Walter Roberson
Walter Roberson 2018 年 10 月 11 日
I do not know which legend you intend to associate with which line.
Walter Roberson
Walter Roberson 2018 年 10 月 11 日
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.
Walter Roberson
Walter Roberson 2018 年 10 月 12 日
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')

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

製品

リリース

R2016a

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by