分组条形图上方的标签
Labels above grouped bar chart
问题
我正在生成一个条形图,并希望在条形图本身 (Ydata) 上方显示每个条形图的高度。因此,对于下面的示例图片,我想在图表上方添加标签。我找不到解决办法。供您参考,我使用的是 Matlab R2016a。
代码
目前我正在使用以下代码创建我的图表。
x={ '-5-0' '0-5' '5-10' '10-15' '15-20' '20-25' '25-30' '30-35' '35-40' '40-45' '45-50' '50-55'};
before= [0 27 28 18 9 6 5 3 2 1 1 0]
after= [27 28 18 9 6 5 3 2 1 1 0 0]
y=[before',after']
h=figure;
hold on
yyaxis left
l1=bar([1:12],y,'grouped');
hYLabel=ylabel('Tonnage [%]');
yyaxis right
hylabel=ylabel('Tonnage [%]');
l1(1).FaceColor = [ 0 0.447 0.7410];
l1(1).EdgeColor = [ 0 0.447 0.7410];
l1(2).FaceColor =[0.85 0.325 0.098]
l1(2).EdgeColor =[0.85 0.325 0.098]
hTitle=title('Test');
hXLabel = xlabel('Value [$/t]');
hLegend=legend([l1(1),l1(2)], 'Test1', 'Test2');
set([gca,hTitle,hXLabel,hYLabel,hLegend] , 'FontName' , 'Helvetica','FontSize', 8)
set(hTitle,'FontSize', 11)
set(hLegend,'Fontsize',8,'Location', 'southoutside', 'Orientation','horizontal')
set(gca,'XTick',[1:12])
xlim([0.5 12.5])
set(gca,'xticklabel',x.')
set(gca,'LineWidth',1.0)
hold off
我在找什么
快速说明我在寻找什么。显然我想要在每一列上方都有一个标签。任何帮助将不胜感激。
您可以使用 this post 中的类似内容
在hold off
这行之前添加:
text(1 , y(1,1)+30, ['y = ', num2str(10)], 'VerticalAlignment', 'top', 'FontSize', 8)
现在您可以使用参数并将其放入 for 循环中以在 evrey 条上添加标签。
编辑:
如果我没看错,这就是你想要的?
在 hold off
之前添加以下内容
xt1=[1:12]-0.17;
xt2=[1:12]+0.11;
yt1=before+0.2;
yt2=after+0.2;
for i=1:12
text(xt1(i) , yt1(i), [num2str(y(i,1)), '%'], 'rotation', 90, 'FontSize', 6, 'Color',[0 0.447 0.7410])
text(xt2(i) , yt2(i), [num2str(y(i,2)), '%'], 'rotation', 90, 'FontSize', 6, 'Color',[0.85 0.325 0.098])
end
现在您还可以更改图形的大小,并且文本会保留在所需的位置。
结果如下所示:
顺便说一句,我只能访问Matlab2015,所以我不能使用你的所有功能,但是代码在Matlab2016上应该没问题
在你的行之后:
l1=bar([1:12],y,'grouped');
添加以下行:
x_shift = 0.15;
text([1:12]-x_shift,y(:,1)+1,num2str(y(:,1)),...
'FontSize',12,'HorizontalAlignment','Center','Color',[0 0.447 0.7410])
text([1:12]+x_shift,y(:,2)+1,num2str(y(:,2)),...
'FontSize',12,'HorizontalAlignment','Center','Color',[0.85 0.325 0.098])
你会得到:
如果你想要百分比格式和旋转,那么 x_shift
需要再调整一点,还有 y 轴限制,所以我把完整的代码带到这里:
x={'-5-0' '0-5' '5-10' '10-15' '15-20' '20-25' '25-30' '30-35'...
'35-40' '40-45' '45-50' '50-55'};
before= [0 27 28 18 9 6 5 3 2 1 1 0];
after= [27 28 18 9 6 5 3 2 1 1 0 0];
y=[before',after'];
ax = axes('xticklabel',x.','LineWidth',1.0,'XTick',1:12);
yyaxis(ax,'left')
l1 = bar(ax,y,'grouped');
x1_shift = -0.17;
x2_shift = 0.11;
text([1:12]+x1_shift,y(:,1)+1,[num2str(y(:,1)) repmat('%',numel(y(:,1)),1)],...
'FontSize',12,'Rotation',90,'HorizontalAlignment','left',...
'VerticalAlignment','middle','Color',[0 0.447 0.7410])
text([1:12]+x2_shift,y(:,2)+1,[num2str(y(:,2)) repmat('%',numel(y(:,2)),1)],...
'FontSize',12,'Rotation',90,'HorizontalAlignment','left',...
'VerticalAlignment','middle','Color',[0.85 0.325 0.098])
ylabel('Tonnage [%]','FontName','Helvetica','FontSize',8);
ylim([0 35])
yyaxis(ax,'right')
ylabel('Tonnage [%]','FontName','Helvetica','FontSize',8);
l1(1).FaceColor = [0 0.447 0.7410];
l1(1).EdgeColor = [0 0.447 0.7410];
l1(2).FaceColor = [0.85 0.325 0.098];
l1(2).EdgeColor = [0.85 0.325 0.098];
title('Test','FontName','Helvetica','FontSize', 11);
xlabel('Value [$/t]', 'FontName' , 'Helvetica','FontSize', 8);
hLegend = legend([l1(1),l1(2)], 'Test1', 'Test2');
set(hLegend,'Location','southoutside','Orientation','horizontal',...
'FontName', 'Helvetica','FontSize', 8)
xlim([0.5 12.5])
ylim([0 35])
box off
您会注意到我稍微更改了您的代码,使其更紧凑,但本质上它做的是一样的,并产生以下栏:
即使您调整图表大小,此处的标签也将放置在相同位置(相对于条形)。
问题
我正在生成一个条形图,并希望在条形图本身 (Ydata) 上方显示每个条形图的高度。因此,对于下面的示例图片,我想在图表上方添加标签。我找不到解决办法。供您参考,我使用的是 Matlab R2016a。
代码
目前我正在使用以下代码创建我的图表。
x={ '-5-0' '0-5' '5-10' '10-15' '15-20' '20-25' '25-30' '30-35' '35-40' '40-45' '45-50' '50-55'};
before= [0 27 28 18 9 6 5 3 2 1 1 0]
after= [27 28 18 9 6 5 3 2 1 1 0 0]
y=[before',after']
h=figure;
hold on
yyaxis left
l1=bar([1:12],y,'grouped');
hYLabel=ylabel('Tonnage [%]');
yyaxis right
hylabel=ylabel('Tonnage [%]');
l1(1).FaceColor = [ 0 0.447 0.7410];
l1(1).EdgeColor = [ 0 0.447 0.7410];
l1(2).FaceColor =[0.85 0.325 0.098]
l1(2).EdgeColor =[0.85 0.325 0.098]
hTitle=title('Test');
hXLabel = xlabel('Value [$/t]');
hLegend=legend([l1(1),l1(2)], 'Test1', 'Test2');
set([gca,hTitle,hXLabel,hYLabel,hLegend] , 'FontName' , 'Helvetica','FontSize', 8)
set(hTitle,'FontSize', 11)
set(hLegend,'Fontsize',8,'Location', 'southoutside', 'Orientation','horizontal')
set(gca,'XTick',[1:12])
xlim([0.5 12.5])
set(gca,'xticklabel',x.')
set(gca,'LineWidth',1.0)
hold off
我在找什么 快速说明我在寻找什么。显然我想要在每一列上方都有一个标签。任何帮助将不胜感激。
您可以使用 this post 中的类似内容
在hold off
这行之前添加:
text(1 , y(1,1)+30, ['y = ', num2str(10)], 'VerticalAlignment', 'top', 'FontSize', 8)
现在您可以使用参数并将其放入 for 循环中以在 evrey 条上添加标签。
编辑:
如果我没看错,这就是你想要的?
在 hold off
xt1=[1:12]-0.17;
xt2=[1:12]+0.11;
yt1=before+0.2;
yt2=after+0.2;
for i=1:12
text(xt1(i) , yt1(i), [num2str(y(i,1)), '%'], 'rotation', 90, 'FontSize', 6, 'Color',[0 0.447 0.7410])
text(xt2(i) , yt2(i), [num2str(y(i,2)), '%'], 'rotation', 90, 'FontSize', 6, 'Color',[0.85 0.325 0.098])
end
现在您还可以更改图形的大小,并且文本会保留在所需的位置。
结果如下所示:
在你的行之后:
l1=bar([1:12],y,'grouped');
添加以下行:
x_shift = 0.15;
text([1:12]-x_shift,y(:,1)+1,num2str(y(:,1)),...
'FontSize',12,'HorizontalAlignment','Center','Color',[0 0.447 0.7410])
text([1:12]+x_shift,y(:,2)+1,num2str(y(:,2)),...
'FontSize',12,'HorizontalAlignment','Center','Color',[0.85 0.325 0.098])
你会得到:
如果你想要百分比格式和旋转,那么 x_shift
需要再调整一点,还有 y 轴限制,所以我把完整的代码带到这里:
x={'-5-0' '0-5' '5-10' '10-15' '15-20' '20-25' '25-30' '30-35'...
'35-40' '40-45' '45-50' '50-55'};
before= [0 27 28 18 9 6 5 3 2 1 1 0];
after= [27 28 18 9 6 5 3 2 1 1 0 0];
y=[before',after'];
ax = axes('xticklabel',x.','LineWidth',1.0,'XTick',1:12);
yyaxis(ax,'left')
l1 = bar(ax,y,'grouped');
x1_shift = -0.17;
x2_shift = 0.11;
text([1:12]+x1_shift,y(:,1)+1,[num2str(y(:,1)) repmat('%',numel(y(:,1)),1)],...
'FontSize',12,'Rotation',90,'HorizontalAlignment','left',...
'VerticalAlignment','middle','Color',[0 0.447 0.7410])
text([1:12]+x2_shift,y(:,2)+1,[num2str(y(:,2)) repmat('%',numel(y(:,2)),1)],...
'FontSize',12,'Rotation',90,'HorizontalAlignment','left',...
'VerticalAlignment','middle','Color',[0.85 0.325 0.098])
ylabel('Tonnage [%]','FontName','Helvetica','FontSize',8);
ylim([0 35])
yyaxis(ax,'right')
ylabel('Tonnage [%]','FontName','Helvetica','FontSize',8);
l1(1).FaceColor = [0 0.447 0.7410];
l1(1).EdgeColor = [0 0.447 0.7410];
l1(2).FaceColor = [0.85 0.325 0.098];
l1(2).EdgeColor = [0.85 0.325 0.098];
title('Test','FontName','Helvetica','FontSize', 11);
xlabel('Value [$/t]', 'FontName' , 'Helvetica','FontSize', 8);
hLegend = legend([l1(1),l1(2)], 'Test1', 'Test2');
set(hLegend,'Location','southoutside','Orientation','horizontal',...
'FontName', 'Helvetica','FontSize', 8)
xlim([0.5 12.5])
ylim([0 35])
box off
您会注意到我稍微更改了您的代码,使其更紧凑,但本质上它做的是一样的,并产生以下栏:
即使您调整图表大小,此处的标签也将放置在相同位置(相对于条形)。