简体   繁体   English

条形图x轴Matlab

[英]Bar plot x-axis Matlab

I am stuck with a bar plot in Matlab. 我陷入了Matlab中的条形图问题。 I got it to work with Matlab help and this this forum until here, but on the x-axis, there are still only 2 names. 直到这里,我都得到了Matlab帮助和这个论坛的支持,但是在x轴上,仍然只有2个名字。 I would like to have the "names" under the bars and the "categories" where the 2 names show up now. 我想在栏下显示“名称”,并在其中显示两个名称的“类别”。 Thank you! 谢谢!

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

The default way would be to use legend to display the name of each element in the group. 默认方式是使用legend显示组中每个元素的名称。 But the position of each bar can be accessed through the XOffset and XData properties. 但是可以通过XOffsetXData属性访问每个条的位置。 See this answer in matlab central. 在matlab Central中查看此答案

So you can use something like: 因此,您可以使用类似:

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

to correctly display the names below each bar. 正确显示每个栏下方的名称。 However, I don't see how you want to display the names of each bar and the categories together below the bars without overlapping. 但是,我看不到如何在条形图的下方不重叠地显示每个条形图的名称和类别。 You could instead display the categories on top by creating a new axes. 您可以通过创建新轴将类别显示在顶部。 Adding Something like: 添加类似的东西:

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)

Ie the complete code would be now: 即完整的代码现在是:

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

图输出

  • You need to observe the x-axis from the graph, and approximate the start and end x-axis values of the bars. 您需要从图中观察x轴,并近似显示条的x轴的起点和终点。
  • On the inspection it was found that bars start from 0.54 (the gap) at the x-axis and ends near 2.32. 在检查中发现,条形图从x轴的0.54(间隙)开始,到2.32附近结束。
  • Next, divide the x-axis into 12 tick positions using the command xticks, 接下来,使用命令xticks将x轴划分为12个刻度位置,
  • and then mark those position with the required labels using the command xticklabels. 然后使用命令xticklabels用所需的标签标记这些位置。 That's all. 就这样。

The required code to do so is given below. 所需的代码如下所示。

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

Output 产量 在此处输入图片说明

Here is a fix for your code to get the desired output (explenations inside): 这是代码获得所需输出(内部解释)的一种修复方法:

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')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM