如何更改直方图中特定箱的颜色?

发布于 2024-10-07 20:07:06 字数 162 浏览 0 评论 0原文

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

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

我如何使 0.7 属于红色的 bin ?

I wrote a code in MATLAB that plots a histogram. I need to color one of the bins in a different color than the other bins (let's say red). Does anybody know how to do it? For example, given:

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

How would I make the bin that 0.7 belongs to red?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

著墨染雨君画夕 2024-10-14 20:07:06

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

这是输出:

替代文本

An alternative to making two overlapping bar plots like Jonas suggests is to make one call to bar to plot the bins as a set of patch objects, then modify the 'FaceVertexCData' property to recolor the patch faces:

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

And here's the output:

alt text

流年已逝 2024-10-14 20:07:06

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

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文