ari*_*iel 5 matlab plot histogram
我在MATLAB中编写了一个绘制直方图的代码.我需要将其中一个箱子的颜色与其他箱子颜色不同(让我们说是红色).有谁知道怎么做?例如,给定:
A = randn(1,100);
hist(A);
Run Code Online (Sandbox Code Playgroud)
我如何制作0.7属于红色的箱子?
制作两个像Jonas这样的重叠条形图的另一种方法是建议一次调用将条形图绘制bar为一组补丁对象,然后修改'FaceVertexCData'属性以重新着色补丁面:
A = randn(1,100); %# The sample data
[N,binCenters] = hist(A); %# Bin the data
hBar = bar(binCenters,N,'hist'); %# Plot the histogram
index = abs(binCenters-0.7) < diff(binCenters(1:2))/2; %# Find the index of the
%# bin containing 0.7
colors = [index(:) ... %# Create a matrix of RGB colors to make
zeros(numel(index),1) ... %# the indexed bin red and the other bins
0.5.*(~index(:))]; %# dark blue
set(hBar,'FaceVertexCData',colors); %# Re-color the bins
Run Code Online (Sandbox Code Playgroud)
这是输出:

我想最简单的方法是先绘制直方图,然后在其上绘制红色框。
A = randn(1,100);
[n,xout] = hist(A); %# create location, height of bars
figure,bar(xout,n,1); %# draw histogram
dx = xout(2)-xout(1); %# find bin width
idx = abs(xout-0.7) < dx/2; %# find the bin containing 0.7
hold on;bar([xout(idx)-dx,xout(idx),xout(idx)+dx],[0,n(idx),0],1,'r'); %# plot red bar
Run Code Online (Sandbox Code Playgroud)