Offset ticks and gridlines
17 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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??
0 Kommentare
Antworten (2)
Adam Danz
am 2 Jan. 2020
Bearbeitet: Adam Danz
am 6 Jan. 2020
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];
0 Kommentare
J Chen
am 2 Jan. 2020
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'})
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!