Offset ticks and gridlines

22 visualizaciones (últimos 30 días)
John Cactus
John Cactus el 2 de En. de 2020
Editada: Adam Danz el 6 de En. de 2020
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??

Respuestas (2)

Adam Danz
Adam Danz el 2 de En. de 2020
Editada: Adam Danz el 6 de En. de 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];

J Chen
J Chen el 2 de En. de 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'})

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by