如何将条形样式编辑为下图?

时间:2018-12-12 20:05:59

标签: matlab plot

如何编辑条形图,使其显示为带有网格的下图。

enter image description here

   x=[4:4:48]; 
      y=[25.312399   1.81357174   ;
 9.3078819    1.47970432 ; 
 7.66729673    1.26972206  ;
  6.96170053  1.17688473 ;
6.77668306   1.2387898;
6.0174443   1.26357444;
5.59616954  0.95115584;
5.2694634    1.041687;
5.1104946    1.02342079 ;
4.917285     0.8655728 ;
 4.7892952    0.85538917;
 4.7373291      0.91927867 ]
 bar(x,y);
  xlabel('Check size (s)')

   ylabel('Computaion Costs (s)')

1 个答案:

答案 0 :(得分:2)

您可以访问Axes对象并设置YGrid属性'on'

x=[4:4:48]; 
y=[25.312399   1.81357174   ;
 9.3078819    1.47970432 ; 
 7.66729673    1.26972206  ;
  6.96170053  1.17688473 ;
6.77668306   1.2387898;
6.0174443   1.26357444;
5.59616954  0.95115584;
5.2694634    1.041687;
5.1104946    1.02342079 ;
4.917285     0.8655728 ;
 4.7892952    0.85538917;
 4.7373291      0.91927867 ]
bar(x,y);
xlabel('Check size (s)')
ylabel('Computaion Costs (s)')

ax = gca;
ax.YGrid = 'on';

输出: enter image description here

如果您还想添加小刻度和小格,如示例图所示,则可以添加以下附加代码:

ax.YMinorTick = 'on';
ax.YMinorGrid = 'on';

输出: enter image description here

更新:条形的颜色和线宽也进行了更改,以匹配您的示例图形

bar(x,y,'FaceColor',[0.447, 0.945, 0.302],'LineWidth',2);
xlabel('Check size (s)')
ylabel('Computaion Costs (s)')
ax = gca;
ax.YGrid = 'on';

ax.YMinorTick = 'on';
ax.YMinorGrid = 'on';

enter image description here