Plot 2 bar plots in the same graph. Problem with the legend.

7 ビュー (過去 30 日間)
Fio
Fio 2014 年 2 月 22 日
編集済み: Fio 2014 年 2 月 22 日
Hi! I wanted to plot 2 bar plots in the same graph. I used the following commands
[n1, xout1] = hist(data1);
bar(xout1,n1);
hold on
[n2, xout2] = hist(data2);
bar(xout2,n2);
h = findobj(gca,'Type','patch');
set(h(1), 'FaceColor','none','EdgeColor','m','LineWidth',1);
set(h(2), 'FaceColor','none','EdgeColor','b','LineWidth',2);
Then I want to add a legend
legend('1','2')
The problem I faced is that the color of the plot and the color of the legend are different.
Could someone help me to fix it.
Thanks in advance.

回答 (1 件)

Dishant Arora
Dishant Arora 2014 年 2 月 22 日
h1 = bar(xout1,n1);
h2 = bar(xout2,n2);
legend([h1 , h2] , '1', '2');
  2 件のコメント
Fio
Fio 2014 年 2 月 22 日
This didn' t work for me. The graph is as the above but the histograms now are blue.
Dishant Arora
Dishant Arora 2014 年 2 月 22 日
change the bar color in one of the plots, i.e
h2 = bar(xout,n2,'r');

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

カテゴリ

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