How to draw vertical lines in between categorical x-axis ticks

12 visualizaciones (últimos 30 días)
HW
HW el 4 de En. de 2018
Comentada: Jos (10584) el 6 de En. de 2018
I have some data that I display using the plot function, with categorical labels on the x-axis. I would like to draw vertical lines in between some of the x-axis tick marks, but not all. I would appreciate suggestions on how to do this. I'm using Matlab 2017a.
I can easily draw a vertical line exactly on the x-axis tick marks, using something like this:
plot([xNames(5), xNames(5)],[0 1],'k')
where xNames is my array with the categorical names. But I would like to adjust this so that a vertical line is drawn between, e.g., xNames(5) and xNames(6).

Respuesta aceptada

HW
HW el 6 de En. de 2018
I figured it out; the key is to not plot with categorical axis too early. I did something like
plot(1:34, 2*[1:34],'ko')
set(gca,'XTick',1:34)
hold on, plot([3.5 3.5],[-0.5 0.5],'k')
hold on, plot([6.5 6.5],[-0.5 0.5],'k')
hold on, plot([20.5 20.5],[-0.5 0.5],'k')
hold on, plot([30.5 30.5],[-0.5 0.5],'k')
set(gca,'XTickLabel',string(xNames))
xtickangle(45)
  1 comentario
Jos (10584)
Jos (10584) el 6 de En. de 2018
the repeated hold on and plot(..) commands can be replaced by using
gridxy([3.5 6.5 20.5 30.5]), 'color', 'k')
(see link in my answer)

Iniciar sesión para comentar.

Más respuestas (1)

Jos (10584)
Jos (10584) el 4 de En. de 2018
Editada: Jos (10584) el 4 de En. de 2018
Use the average?
v = (xNames(5)+ xNames(6))/2
plot([v v], ...)
You might also be interested in various utilities on the File Exchange that can plot vertical and horizontal grid lines at specific places, like my own GRIDXY
  3 comentarios
Jos (10584)
Jos (10584) el 5 de En. de 2018
insert a new category name between 5 and 6 and use that?
HW
HW el 5 de En. de 2018
I really would like to have the line to be drawn between two tick marks. I would prefer to not add anything visual to the x-axis, even additional tick marks.

Iniciar sesión para comentar.

Categorías

Más información sobre Bar Plots en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by