I need to adda legend to this code, as tried in the last three lines !

4 ビュー (過去 30 日間)
Seifeldin Mohamed
Seifeldin Mohamed 2023 年 7 月 24 日
編集済み: Voss 2023 年 7 月 31 日
x = categorical({'Present '; 's'});
y = [2363; 2090];
z = [0.19154; 0.17777];
nil = [0; 0];
yyaxis left
bar(x, [y nil], 'grouped')
ylabel('Static Temep');
yyaxis right
bar(x, [nil z], 'grouped')
ylabel('Mass 2');
grid on

回答 (1 件)

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath 2023 年 7 月 24 日
No, the code you provided has a few issues in adding legends.
Here's the corrected version of the code:
x = categorical({'Present', 'all'});
y = [2363; 2090];
z = [0.19154; 0.17777];
nil = [0; 0];
yyaxis left
bar(x, [y nil], 'grouped')
ylabel('Static Temperature')
yyaxis right
bar(x, [nil z], 'grouped')
ylabel('Mass CO2')
grid on
grid minor
TestL = {'Static Temperature', 'Mass CO2'};
hB = bar(x, [y z], 'grouped');
hLg = legend(hB, TestL, 'Location', 'northwest'); % Use hB instead of X and Y
  • With these changes, the legend should appear correctly on your plot. The TestL variable contains the legend labels, and the hLg variable stores the handle to the legend object, which can be used to further customize the legend if needed.
  2 件のコメント
Seifeldin Mohamed
Seifeldin Mohamed 2023 年 7 月 24 日
移動済み: Voss 2023 年 7 月 31 日
Thank you so much, however, how can I chnage the color of the legend itself to be simialr to the color of the y-axis ?
Voss
Voss 2023 年 7 月 31 日
編集済み: Voss 2023 年 7 月 31 日
figure
plot(magic(5))
h = legend("Line "+(1:5));
Here's how you can alter the (background, text, edge) color of a legend:
h.Color = [0.5 0 0.5];
h.TextColor = [1 0.5 0];
h.EdgeColor = 'c';

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by