图例未使用 'hold on' 在直方图中显示正确的颜色

Legend doesn't show right color in histogram using 'hold on'

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

我的代码如下所示:

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`

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

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