Offset ticks and gridlines

20 ビュー (過去 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 Center および File ExchangeGrid 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