绘制带有“保持"状态的直方图时出现错误的图例 [英] Wrong legends when plotting histogram with `hold on`

查看:59
本文介绍了绘制带有“保持"状态的直方图时出现错误的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在子图上绘制直方图,其中每个图都有两个直方图,如下面子图的一部分所示:

I am plotting histograms on a subplot, where each plot has two histograms as shown in one part of the subplot below:

问题:我希望变量名为result_uT_per_window的历史记录具有红色图例,变量名为uT_top_of_global_window的历史记录具有蓝色图例.我以为我应该在代码中做到这一点,但事实并非如此.这是代码:

Question: I would want the hist with variable named result_uT_per_window to have red legend, and hist with variable named uT_top_of_global_window to have blue legend. I thought what I have in code is supposed to do that, but it doesn't. This is the code:

    hold on
    hist(nonzeros(result_uT_per_window(:,window_no)))
    hist(uT_top_of_global_window)
    h = findobj(gca, 'Type','patch');
    set(h(1), 'FaceColor','r', 'EdgeColor','w')
    set(h(2), 'FaceColor','b', 'EdgeColor','w')
    xlabel('Total Velocity (in m/s)')
    ylabel('Frequency')
    legend('From moving window','From global window')

您能注意到我要去哪里了吗?谢谢.

Can you notice where am I going wrong? Thanks.

推荐答案

假设第一个hist命令生成的内容是h(1),就会出错:

You go wrong in assuming that h(1) is what was produced by your first hist command:

data1=normrnd(10,1,10000,1);
data2=normrnd(20,1,10000,1);
figure;
hold on;
hist(data1);
hist(data2);
h = findobj(gca, 'Type','patch');
set(h(1), 'FaceColor','r', 'EdgeColor','w') % color h1 plot red
set(h(2), 'FaceColor','b', 'EdgeColor','w') % color h2 plot blue

产生

显示data1(平均值为10)以蓝色绘制,证明它的句柄是h(2),即使它是首先绘制的.

showing that data1 (with mean value 10) is plotted in blue, proving that its handle is h(2) even though it was plotted first.

因此,要解决您的问题,您可以写

Hence, to solve your problem you could write

h = flipud(findobj(gca, 'Type','patch'));

按期望的顺序将手柄放入h.

to bring the handles in h in the order that you expect.

这篇关于绘制带有“保持"状态的直方图时出现错误的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆