plot function legend is wrong.

8 ビュー (過去 30 日間)
Ben Whitby
Ben Whitby 2023 年 3 月 20 日
コメント済み: Ben Whitby 2023 年 3 月 20 日
Hi All,
Can anybody give me some pointers on why the legend in the following plot is wrong. I use a loop to plot two variables, one is an array.
figure (55)
for i = 1:152
plot(t3,PowerAuts(:,:,i)); hold on;
end
hold on
plot(t3, MeanDailyPwr1,'color','k','LineWidth',4);
legend('MeanDailyPwr1','Orientation','horizontal')
grid on
ylim([0 40]);
yticks([0:5:40]);
xlim([t3(1) t3(end)])
ax.FontSize = 22;
xlabel('Time (hh:mm)','FontSize',24)
ylabel('Load Demand (kW)','FontSize',24)
title('Daily Load Demand','FontSize',28)
I want to display the variable MeanDailyPower1 in the legend but it is the wrong color at the moment.
  2 件のコメント
Antoni Garcia-Herreros
Antoni Garcia-Herreros 2023 年 3 月 20 日
You may want to check here or here
You should reference your plot:
pMean=plot(t3, MeanDailyPwr1,'color','k','LineWidth',4);
l=legend(pMean,'MeanDailyPwr1','Orientation','horizontal');
Ben Whitby
Ben Whitby 2023 年 3 月 20 日
I got it. Thank you.

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

回答 (2 件)

Star Strider
Star Strider 2023 年 3 月 20 日
The legend is probably picking up the first line plotted, that by default is blue.
Perhaps this —
figure (55)
for i = 1:152
plot(t3,PowerAuts(:,:,i)); hold on;
end
hold on
hpMDP = plot(t3, MeanDailyPwr1,'color','k','LineWidth',4);
legend(hpMDP, 'MeanDailyPwr1','Orientation','horizontal')
grid on
ylim([0 40]);
yticks([0:5:40]);
xlim([t3(1) t3(end)])
ax.FontSize = 22;
xlabel('Time (hh:mm)','FontSize',24)
ylabel('Load Demand (kW)','FontSize',24)
title('Daily Load Demand','FontSize',28)
Including the handle to the 'MeanDailyPower' plot specifies what the legend is to refer to.
.

Dave B
Dave B 2023 年 3 月 20 日
編集済み: Dave B 2023 年 3 月 20 日
The legend is labeling the first line in the chart rather than the last one. An easy way to specify which line should be labeled in the legend is to grab the output from plot and pass that into the legend function
a = rand(10);
hold on
for i = 1:size(a,1)
plot(a(i,:))
end
aveline=plot(mean(a),'k','LineWidth',2);
legend(aveline,'The Average')

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by