how to set stacked bar graph legend?

71 ビュー (過去 30 日間)
Kangkeon Jeon
Kangkeon Jeon 2019 年 11 月 12 日
編集済み: Dyuman Joshi 2024 年 1 月 31 日
so i have a graph like this. how do i set up legend so i can mark each color what they represent?

採用された回答

dpb
dpb 2019 年 11 月 12 日
Did you try
legend('A','B','C')
???
  4 件のコメント
Mario
Mario 2024 年 1 月 31 日
編集済み: Mario 2024 年 1 月 31 日
I have the same problem and the code
bar(randi(400,4,3),'stacked')
legend('A','B','C')
gives me the error: Index in position 1 exceeds array bounds. Index must not exceed 11.
The bars are plotted as they should, but the legend won't appear.
I am using R2022a.
Help would be much appreciated.
edit:
Directly after posting this I found the error. I was using a variable called 'legend' in another script which was in the workspace. Changed it to something else, no it works fine. Wanted to share this stupid mistake, maxbe I am not the only one.
Dyuman Joshi
Dyuman Joshi 2024 年 1 月 31 日
編集済み: Dyuman Joshi 2024 年 1 月 31 日
The piece of code you shared works here without any error, see below.
Share the full error message i.e. all of the red text.
Edit - This should be a (gentle) reminder to not name variables or scripts with the same as in-built functions in MATLAB.
bar(randi(400,4,3),'stacked')
legend('A','B','C')

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

その他の回答 (0 件)

カテゴリ

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