5

我在 MATLAB 中编写了一个绘制直方图的代码。我需要将其中一个垃圾箱着色为与其他垃圾箱不同的颜色(比如说红色)。有人知道怎么做吗?例如,给定:

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

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

4

2 回答 2

6

像Jonas 建议的那样制作两个重叠条形图的另一种方法是调用一次以bar将 bin 绘制为一组补丁对象,然后修改'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

这是输出:

替代文字

于 2010-12-17T20:16:10.773 回答
2

I guess the easiest way is to draw the histogram first and then just draw the red bin over it.

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
于 2010-12-17T19:10:29.310 回答