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에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by