Matlab条:更改条的颜色以及条与轴之间的间距 [英] Matlab bars: changing the color of the bar and the space between bars and axes

查看:173
本文介绍了Matlab条:更改条的颜色以及条与轴之间的间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个棘手的问题,我认为不能简单地回答.

我的数据表很大,想为每个2个对应的平均值保存条形图.我已经使用"for循环"进行了此操作,但是我无法为条形设置不同的颜色,也无法在条形和轴之间设置适当的间距.

这是最简单的示例:

k = [2 5]
bar(k)

Matlab将这些数据[2 5]视为一个组",并且不可能只改变一个条形的颜色.

当然,当我们有不同的组"时,颜色是可变的.

y = bar([1 2 3; 4 5 6]);
y(2).FaceColor = 'red';

那我的例子呢?我只需要两个条,就需要为它们涂上不同的颜色.另外,我想在每个条形图和轴之间设置一个小的空间(同样,用两个条形图也不是那么简单,并且我不能使用条形宽度",因为这不能满足我的实际需求).

有人知道如何解决这个问题吗?

感谢您的任何回复! 玛丽

解决方案

在这里可以解决此问题.您可以使用diag(k)创建一个2 * 2矩阵,该矩阵的所有零均为零,但主对角线将与您的值一起使用,Matlab会将其读取为2个不同的组.然后,将虚拟"条的Xdata更改(您看不到,但在x轴上留一些空间为nan,这样Matlab会忽略它.

k = [2 5];
b = bar(diag(k));
set(b,{'XData'},{[1 nan],[nan 1]}); % remove the group you don't want
set(gca,{'xticklabel','XTick'},{{'ONE','TWO'},[0.85 1.15]});
b(2).FaceColor = 'r'; % choose a different color

结果:

如果您想将其推广到更多组(此处为5):

k = 1:5;
data = diag(k);
xdata = eye(numel(k))./eye(numel(k)); % a martix of nan with 1 on the main diagonal
b = bar(diag(k));
% remove all data except one bin in all groups:
set(b,{'XData'},mat2cell(xdata,ones(size(data,1),1),size(data,2)))
X = xlim; 5 get x-axis limits
w = (1-X(1))/(numel(k)/2); % calculate the width of one bin
set(gca,'XTick',X(1)+w/2:w:X(2)) % set the X ticks to the center of the bins
set(gca,'xticklabel',{'ONE','TWO','THREE','FOUR','FIVE'}); % set the labels
set(b,{'FaceColor'},mat2cell(lines(numel(k)),ones(size(data,1),1),3)); % set the colors

您将得到:

最后,如果希望它们悬停"在x轴上方一点,则可以添加以下几行:

b(1).BaseLine.Color = 'none'; % remove the base line
ylim([-0.1 max(k(:))]) % shift the bars up a little

并获得:

I have a tricky question, which I think cannot be simply answered.

I have a large data table and want to save bar charts for each 2 corresponding mean values. I already did that with "for loop", but I cannot set different colors to my bars and cannot set proper spaces between bars and axes.

That's the simplest example:

k = [2 5]
bar(k)

Matlab thinks about those data [2 5] as about one "group" and it does not give a possibility of changing color for only one bar.

Of course, when we have different "groups", colors are changeable.

y = bar([1 2 3; 4 5 6]);
y(2).FaceColor = 'red';

But what about my example? I need only two bars, and I need to color them differently. Also, I want to set a small space between each bar and the axes (and again with two bars it is not that simple, and I cannot use "bar width" because that doesn't give me what I really want).

Does anyone know how to get around this?

Thanks for any reply! Mary

解决方案

Here is an option to workaround this. You can use diag(k) to create a 2*2 matrix with all zeros except the main diagonal that will be with your values, and Matlab will read it as 2 different groups. Then you change the Xdata of the 'dummy' bars (which you don't see but take some space on the x axis to nan so Matlab will ignore it.

k = [2 5];
b = bar(diag(k));
set(b,{'XData'},{[1 nan],[nan 1]}); % remove the group you don't want
set(gca,{'xticklabel','XTick'},{{'ONE','TWO'},[0.85 1.15]});
b(2).FaceColor = 'r'; % choose a different color

The result:

And if you want to generalize this to more groups (here it's 5):

k = 1:5;
data = diag(k);
xdata = eye(numel(k))./eye(numel(k)); % a martix of nan with 1 on the main diagonal
b = bar(diag(k));
% remove all data except one bin in all groups:
set(b,{'XData'},mat2cell(xdata,ones(size(data,1),1),size(data,2)))
X = xlim; 5 get x-axis limits
w = (1-X(1))/(numel(k)/2); % calculate the width of one bin
set(gca,'XTick',X(1)+w/2:w:X(2)) % set the X ticks to the center of the bins
set(gca,'xticklabel',{'ONE','TWO','THREE','FOUR','FIVE'}); % set the labels
set(b,{'FaceColor'},mat2cell(lines(numel(k)),ones(size(data,1),1),3)); % set the colors

and you get:

And finally, if you want them to 'hover' a little above the x-axis, you can add the following lines:

b(1).BaseLine.Color = 'none'; % remove the base line
ylim([-0.1 max(k(:))]) % shift the bars up a little

and get:

这篇关于Matlab条:更改条的颜色以及条与轴之间的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆