使用“保持”时,图例在直方图中不显示正确的颜色

时间:2017-06-26 14:39:36

标签: matlab histogram matlab-figure

当我使用'hold on'绘制多个直方图时,图例的颜色与第一个直方图不匹配。见下图:

enter image description here

我的代码如下所示:

figure(1)
h1 = histogram(y_rating2,50);

hold on

h2 = histogram(y_rating2,50);
l1 = line([rating_1_common_mean rating_1_common_mean],[0 max(h2.Values)],'Color','red','LineWidth',1.5); 

hold on

l2 = line([rating_2_common_mean rating_2_common_mean],[0 max(h2.Values)],'Color','green','LineWidth',1.5);

legend([h1 h2 l1 l2],{'Rating 1','Rating 2','Mean Rating 1','Mean Rating 2'});

注意: 我没试过就试过这个: Wrong legends when plotting histogram with `hold on`

1 个答案:

答案 0 :(得分:0)

您可以将FaceAlpha设置为1并在评级2后绘制等级1。例如

h2 = histogram(y_rating2, 50, 'FaceAlpha', 1);
hold on;
h1 = histogram(y_rating1, 50, 'FaceAlpha', 1);