MATLAB: Repetitive values for x axis

axisplot

How can I get this behaviour for the x-axis?

Best Answer

One way to plot 3 lines that share x-coordinates is to plot the lines individually. That's shown in the first subplot below.
A second way is to create 1 vector of x and y coordinates where NaN values are used to separate and disconnect the lines. That's shown in the second subplot below.
The very strange and difficult to interpret x-tick marks that repeat is thankfully not an option in Matlab but you can change the x-tick labels. In the two demos below, the actual x ticks range from 0 to 20, monotonically. Then I change the labels.
x0 = 0 : .5 : 10;
x1 = x0 + 5;
x2 = x1 + 5;
y = x0*10;
clf
subplot(1,2,1)
hold on
plot(x0,y, 'r-')
plot(x1,y, 'r-')
plot(x2,y, 'r-')
grid on
set(gca, 'XTick', 0:2:20, 'XTickLabels', [0 5 0 5 0 5 0 5 10 15 20])
subplot(1,2,2)
x = [x0, nan, x1, nan, x2, nan];
y = repmat([y,nan], 1, 3);
plot(x,y, 'r')
grid on
set(gca, 'XTick', 0:2:20, 'XTickLabels', [0 5 0 5 0 5 0 5 10 15 20])
Related Question