我陷入Matlab中的条形图问题。直到这里,我都得到了Matlab帮助和这个论坛的支持,但是在x轴上,仍然只有2个名字。我想在栏下显示“名称”,并在其中显示两个名称的“类别”。谢谢!
Net::HTTP
答案 0 :(得分:2)
默认方法是使用legend
显示组中每个元素的名称。但是可以通过XOffset
和XData
属性访问每个条的位置。请参阅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
答案 1 :(得分:1)
执行此操作所需的代码如下。
Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Container(
padding: EdgeInsets.fromLTRB(5, 5, 5, 5),
child: Form(
child: ListView(
scrollDirection: Axis.vertical,
children: <Widget>[
//other widgets here
DropdownButtonFormField(items: this.dropDownItems),
],
),
)),
);
答案 2 :(得分:1)
这是修复代码以获取所需输出(内部解释)的方法:
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')