2010-12-17 79 views
5

我在MATLAB中编写了一个绘制直方图的代码。我需要用不同颜色的箱子给其中一个箱子着色(比方说红色)。有人知道该怎么做吗?例如,给定:如何更改直方图中特定仓的颜色?

A = randn(1,100); 
hist(A); 

我该如何制作0.7属于红色的bin?

回答

2

我想最简单的方法是首先绘制直方图,然后在其上绘制红色框。

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 
+0

谢谢,但我在寻找一种方式ŧ o绘制直方图.. – ariel 2010-12-17 19:27:14

+1

@ariel:看起来我在调用'bar'时犯了一个错误。现在它应该工作。 – Jonas 2010-12-17 19:56:07

+0

+1并确认它有效。 – gnovice 2010-12-17 20:16:47

6

到使两个重叠条曲线等Jonas suggests另一种方法是使一个呼叫到bar绘制箱为一组patch objects,然后修改重新着色补丁面孔:

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 

而这里的输出:

alt text

+0

当我看到直方图只是一个单独的对象后,我甚至没有检查过酒吧系列给你个别的句柄。 +1更谨慎。 – Jonas 2010-12-18 03:59:11