Matlab 每日小时直方图(连续箱)

Matlab 每日小时直方图(连续箱),matlab,histogram,Matlab,Histogram,我生成下图中的直方图(在x轴上有一天中的小时数)。 你知道如何以最高价值为中心吗?在这种情况下,我想把右边的部分旁边的第一个垃圾箱的左侧 时间是一条包含12天时间戳(1分钟分辨率)的射线 Time{1,1}='00:00:00'; Time{2,1}='00:01:00'; ... Time{1000,1}='16:39:00'; ... Time{17280,1}='23:59:00' 如果对象正在睡眠,则睡眠是一个包含1的向量,如果不是,则为0 sleeping(1,1)=1; sleep

我生成下图中的直方图(在x轴上有一天中的小时数)。 你知道如何以最高价值为中心吗?在这种情况下,我想把右边的部分旁边的第一个垃圾箱的左侧

时间是一条包含12天时间戳(1分钟分辨率)的射线

Time{1,1}='00:00:00';
Time{2,1}='00:01:00';
...
Time{1000,1}='16:39:00';
...
Time{17280,1}='23:59:00'
如果对象正在睡眠,则睡眠是一个包含1的向量,如果不是,则为0

sleeping(1,1)=1;
sleeping(2,1)=1;
...
sleeping(1000,1)=0;
...
sleeping(17280,1)=1;



    figure
    hist(datenum(Time(sleeping==1)),24)
    datetick

首先,使用hist返回直方图的值(
VAL
)和存储单元(
bins
):

[vals, bins] = hist(datenum(Time(sleeping==1)),24);
然后,找出最大值的实际位置,以使直方图相对于最大值居中:

[val, idx] = max(vals);
然后,获取直方图中心的位置(如果使用
24
-bin直方图,则应为
12
)。然后,估计将直方图中心移动到绘图中心所需的偏移量

centerPos = round(length(bins)/2);
shiftSize = centerPos - idx;
找到
shiftSize
后,我们可以有三种情况:a)换档向左,在这种情况下,换档将更大。我的处理方法如下:

if shiftSize < 0    % move to the left w.r.t. center
    shiftSize = length(bins) - shiftSize;
end
最后,我们绘制,并得到当前轴进行调整

figure, bar(bins, nvals); ax = gca;
现在,你要确保你有24小时的所有垃圾箱

ax.XTick = bins; 
请注意,如果执行ax.XTick=nbins操作,则会出现错误,因为XTick必须单调递增。最后,将这些x轴标签更改为已移动的标签:

ax.XTickLabel = datestr(nbins, 'HH:MM');
以下是您提供的示例的完整代码

Time{1,1}='00:00:00';
Time{2,1}='00:01:00';
Time{1000,1}='16:39:00';
Time{17280,1}='23:59:00';

sleeping(1,1)=1;
sleeping(2,1)=1;
sleeping(1000,1)=0;
sleeping(17280,1)=1;

[vals, bins] = hist(datenum(Time(sleeping==1)),24);

% get the maximum
[val, idx] = max(vals);

% circularly shift to center around the maximum
centerPos = round(length(bins)/2);
shiftSize = centerPos - idx;
if shiftSize < 0    % move to the left w.r.t. center
    shiftSize = length(bins) - shiftSize;
end

if shiftSize    % if it is 0 it means that it is already centered
    nvals = circshift(vals, shiftSize, 2);
    nbins = circshift(bins, shiftSize, 2);
end

figure, bar(bins,nvals); ax = gca;
ax.XTick = bins; 
ax.XTickLabel = datestr(nbins, 'HH:MM');
Time{1,1}='00:00:00';
时间{2,1}='00:01:00';
时间{1000,1}='16:39:00';
时间{17280,1}='23:59:00';
睡眠(1,1)=1;
睡眠(2,1)=1;
睡眠(1000,1)=0;
睡眠(17280,1)=1;
[VAL,bins]=hist(datenum(Time(sleeping==1)),24);
%获得最大值
[val,idx]=最大值(val);
%以最大值为中心循环移动
中心位置=圆形(长度(箱)/2);
shiftSize=centerPos-idx;
如果换档尺寸<0%,则向左移动w.r.t.中心
移位大小=长度(箱子)-移位大小;
结束
如果shiftSize%为0,则表示它已居中
nvals=循环移位(VAL,移位大小,2);
nbins=循环移位(箱子,移位大小,2);
结束
图,条形图(料仓、NVAL);ax=gca;
ax.XTick=料仓;
ax.XTickLabel=datestr(nbins,'HH:MM');
这给了我以下的情节:


如果您有问题,请告诉我。

您是否可以编辑问题以包含生成此绘图的代码?从第二天开始,右边的部分不是很正确,因此没有真正反映第一天的观察结果吗?是的,但它属于哪一天并不重要…我对小时直方图感兴趣…你对所有小时的直方图感兴趣?(例如,任何一天的凌晨3点都是相同的bin)或者您对一天的小时直方图感兴趣?(无论哪种方式,代码都有问题)。使用两个输出参数调用
hist
。使用带有两个输出参数的
max
检测最大值。然后使用
mod
circshift
循环移动x轴values@nkjt我对所有小时的柱状图感兴趣。问题是什么
Time{1,1}='00:00:00';
Time{2,1}='00:01:00';
Time{1000,1}='16:39:00';
Time{17280,1}='23:59:00';

sleeping(1,1)=1;
sleeping(2,1)=1;
sleeping(1000,1)=0;
sleeping(17280,1)=1;

[vals, bins] = hist(datenum(Time(sleeping==1)),24);

% get the maximum
[val, idx] = max(vals);

% circularly shift to center around the maximum
centerPos = round(length(bins)/2);
shiftSize = centerPos - idx;
if shiftSize < 0    % move to the left w.r.t. center
    shiftSize = length(bins) - shiftSize;
end

if shiftSize    % if it is 0 it means that it is already centered
    nvals = circshift(vals, shiftSize, 2);
    nbins = circshift(bins, shiftSize, 2);
end

figure, bar(bins,nvals); ax = gca;
ax.XTick = bins; 
ax.XTickLabel = datestr(nbins, 'HH:MM');