Offset ticks and gridlines

18 次查看(过去 30 天)
John Cactus
John Cactus 2020-1-2
编辑: Adam Danz 2020-1-6
I have a grouped bar chart and currently the major gridlines are in the middle of each group, is it possible to have the ticks and the vertical major gridline between each group instead as opposed to the centre??

回答(2 个)

Adam Danz
Adam Danz 2020-1-2
编辑:Adam Danz 2020-1-6
Assuming the grouped bars are centered at integer values along the x axis, the last line of code below places the x ticks at the halfway point between each integer that spans the x axis.
% Set up demo
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
h = bar(y)
grid on
% Get axis handle if you don't have it already
ax = gca();
% Set x tick to 1/2 way between integers that span x axis
ax.XTick = (floor(min(xlim(ax))) : 1 : ceil(max(xlim(ax)))) + 0.5
If the x centers of the grouped bars were specified and are not centered at integer spacing, you can compute the halfway mark between each group like this.
% Set up demo
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
h = bar([4,5,8,10],y)
grid on
% Get axis handle if you don't have it already
ax = gca();
% Set x tick to 1/2 way between bar groups
ax.XTick = unique([h.XData]) + [diff(unique([h.XData]))/2, inf];

J Chen
J Chen 2020-1-2
Use xticks to place the ticks at the left of each group, e,g,, xticks([0.5 1.5 2.5]). You can use xt = xticks to get the coordnates of the current ticks.
Use xticklabels to change your tick labels, e.g., xticklabels({'1','2','3'})

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by