条形图 x 轴 Matlab

Bar plot x-axis Matlab

我被 Matlab 中的条形图困住了。我让它在 Matlab 帮助下工作,这个论坛直到这里,但在 x 轴上,仍然只有 2 个名字。我希望在栏下显示 "names",在现在显示 2 个名称的位置显示 "categories"。谢谢!

values = [4 10...
    11 2 3;...
    4 1...
    5 2 -10];
names = {'PreSplitTotalEON' 'PostSplitTotalEON'...
    'PreSplitPureEON' 'PostSplitPureEON' 'PostSplitUniper';...
    'PreSplitTotalRWE' 'PostSplitTotalRWE'...
    'PreSplitPureRWE' 'PostSplitPureRWE' 'PostSplitInnogy'};
categories = {'EON', 'RWE'};
b = bar(values,'FaceColor','flat');
xticklabels([names(1,:)';names(2,:)'])         
% This will set labels to be used for each tick of the x-axis
xticks(1:1:length([names(1,:)';names(2,:)']))
% This will set how many ticks you want on the x-axis. Here, there
% should be 48 ticks being generated. One for each piece of data you have.
xtickangle(90)
% This will rotate the label so that the labels will not overlap
% with one another. This is in degrees.
for k = 1:size(values,2) % for fancier colors.
    b(k).CData = k;
end

默认方式是使用 legend 来显示组中每个元素的名称。但是可以通过 XOffsetXData 属性访问每个柱的位置。请参见 matlab central 中的 this answer

所以你可以使用类似的东西:

ticksList = b(1).XData+arrayfun(@(x)x.XOffset, b)';
xticks(ticksList(:))
xticklabels([names(1,:)';names(2,:)'])

正确显示每个栏下方的名称。但是,我看不出您想如何在不重叠的情况下在条形下方一起显示每个条形的名称和类别。您可以通过创建新轴来在顶部显示类别。添加如下内容:

ax1 = gca;
ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
xticks(b(1).XData)
xticklabels(categories)

即完整的代码现在是:

clear all
close all

values = [4 1 11 2 3; 4 1 5 2 -10];
names = {'Pre split total EON' 'Post split total EON'...
    'Pre split pure EON' 'Post split pure EON' 'Post split Uniper';...
    'Pre split total RWE' 'Post split total RWE'...
    'Pre split pure RWE' 'Post split pure RWE' 'PostSplitInnogy'};
categories = {'EON','RWE'};
figure;
b = bar(values,'FaceColor','flat');
ticksList = b(1).XData+arrayfun(@(x)x.XOffset, b)';
xticks(ticksList(:))
xticklabels([names(1,:)';names(2,:)'])
xtickangle(90)
ax1 = gca;
ax2 = axes('Position', get(ax1, 'Position'),'Color', 'none');
set(ax2, 'XAxisLocation', 'top','YAxisLocation','Right');
set(ax2, 'XLim', get(ax1, 'XLim'),'YLim', get(ax1, 'YLim'));
set(ax2, 'YTick', []);
xticks(b(1).XData)
xticklabels(categories)
for k = 1:size(values,2) % for fancier colors.
    b(k).CData = k;
end

  • 你需要从图中观察x轴,并近似 条形的开始和结束 x 轴值。
  • 在检查中发现柱从 0.54(差距)开始 x 轴并在 2.32 附近结束。
  • 接下来,使用命令xticks将x轴分成12个刻度位置,
  • 然后使用命令 xticklabels 用所需的标签标记这些位置。就这些了。

执行此操作所需的代码如下。

close all
clear all

values = [4 10 ...
    11 2 3; ...
    4 1 ...
    5 2 -10];

% just declare the names lables as a simple 1-D cell array
% remove the columns and construct as row wise cell array
names = {'PreSplitTotalEON','PostSplitTotalEON', ...
    'PreSplitPureEON', 'PostSplitPureEON', 'PostSplitUniper', ...
    'PreSplitTotalRWE', 'PostSplitTotalRWE', ...
    'PreSplitPureRWE', 'PostSplitPureRWE', 'PostSplitInnogy'};
% declare the categories label
categories = {'EON', 'RWE'};
% construct the bar
b = bar(values,'FaceColor','flat');

%-> mark the respective xticks <-
% on close inspepection, it was found that the bars starts
% near 0.54 and end at nearly 2.31 at the x-axis
% so divide the x axis in 12 ticks within those limits.
xticks([linspace(0.54, 2.31, 12)]);

% now put the lables at those limits, that all
xticklabels({categories{1}, names{1:5}, categories{2}, names{6:10}})

xtickangle(90)
% This will rotate the label so that the labels will not overlap
% with one another. This is in degrees.
for k = 1:5 % for fancier colors.
    b(k).CData = k;
end

输出

这里是您的代码获得所需输出的修复程序(内部解释):

values = [4 10 11 2 3;
          4 1 5 2 -10];
names = {'PreSplitTotal' 'PostSplitTotal'...
    'PreSplitPure' 'PostSplitPure' 'PostSplitUniper';...
    'PreSplitTotal' 'PostSplitTotal'...
    'PreSplitPure' 'PostSplitPure' 'PostSplitInnogy'}.'; % <-- note the transpose!
categories = {'EON', 'RWE'};
N_names = size(values,2);
ax = axes('NextPlot','add'); % <-- same as 'hold on'
col = lines(N_names); % choose your favorite colormap
% draw the bars in pairs by their 'names':
for k = 1:N_names % for fancier colors.categories
    b = bar(ax,[k k+N_names+1],values(:,k),0.15,...
        'FaceColor',col(k,:));
end
xticks([1:N_names (N_names+2:N_names*2+1)]) % does not crate a tick for the space between categories
xticklabels(names(:))
xtickangle(30)
% place the category name on top of the axes:
text(ceil(N_names/2)+[0 N_names+1],ax.YLim(2).*[1 1],categories,...
    'VerticalAlignment','bottom','HorizontalAlignment','center')