Adding a second x-axis to each plot in a tiled layout / subplot
I am trying to add a second x-axis to each plot in a tiled layout. Using this tutorial, I was able to add a second x-axis to the first tile. However, as soon as I transition to the next tile, the previous plot disappears and I am just left with a single blank plot. Adding "hold on" to any line in the code didn’t help, neither did plotting the same data a second time. The same problem arises when using subplot().
I would greatly appreciate any help! My code looks like this (I only need the second axis in order to display different x-ticks w.r.t. to the same data, which is why I’m not adding any plots to the second axis):
% create tiledlayout
tiledlayout(2, 2)
nexttile
% plot toy example
hold on
plot(0:10, 0:10)
axis([0 10 0 10])
xticks([2 7])
% add second axis
ax1 = gca;
ax2 = axes(‘Position’, ax1.Position, ‘XAxisLocation’, ‘top’, ‘YAxisLocation’,’right’, ‘Color’,’none’);
axis([0 10 0 10])
xticks([1 8])
% after this line, the plot disappears
nexttileI am trying to add a second x-axis to each plot in a tiled layout. Using this tutorial, I was able to add a second x-axis to the first tile. However, as soon as I transition to the next tile, the previous plot disappears and I am just left with a single blank plot. Adding "hold on" to any line in the code didn’t help, neither did plotting the same data a second time. The same problem arises when using subplot().
I would greatly appreciate any help! My code looks like this (I only need the second axis in order to display different x-ticks w.r.t. to the same data, which is why I’m not adding any plots to the second axis):
% create tiledlayout
tiledlayout(2, 2)
nexttile
% plot toy example
hold on
plot(0:10, 0:10)
axis([0 10 0 10])
xticks([2 7])
% add second axis
ax1 = gca;
ax2 = axes(‘Position’, ax1.Position, ‘XAxisLocation’, ‘top’, ‘YAxisLocation’,’right’, ‘Color’,’none’);
axis([0 10 0 10])
xticks([1 8])
% after this line, the plot disappears
nexttile I am trying to add a second x-axis to each plot in a tiled layout. Using this tutorial, I was able to add a second x-axis to the first tile. However, as soon as I transition to the next tile, the previous plot disappears and I am just left with a single blank plot. Adding "hold on" to any line in the code didn’t help, neither did plotting the same data a second time. The same problem arises when using subplot().
I would greatly appreciate any help! My code looks like this (I only need the second axis in order to display different x-ticks w.r.t. to the same data, which is why I’m not adding any plots to the second axis):
% create tiledlayout
tiledlayout(2, 2)
nexttile
% plot toy example
hold on
plot(0:10, 0:10)
axis([0 10 0 10])
xticks([2 7])
% add second axis
ax1 = gca;
ax2 = axes(‘Position’, ax1.Position, ‘XAxisLocation’, ‘top’, ‘YAxisLocation’,’right’, ‘Color’,’none’);
axis([0 10 0 10])
xticks([1 8])
% after this line, the plot disappears
nexttile subplot, tiledlayout, axis MATLAB Answers — New Questions