Adding a second x-axis to each plot in a tiled layout / subplot

79 visualizaciones (últimos 30 días)
Roger Wang
Roger Wang el 19 de En. de 2021
Respondida: Jasmin McInerney el 31 de Ag. de 2021
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

Respuestas (2)

Jasmin McInerney
Jasmin McInerney el 31 de Ag. de 2021
I also had this quesiton and solved it by nesting the tiled layouts. Here is how that can be done: https://au.mathworks.com/matlabcentral/answers/788939-nesting-tiledlayouts-within-another-tiledlayout#answer_664819
Good luck :)

dpb
dpb el 19 de En. de 2021
The tiled layout object is documented as
"A tiled chart layout contains an invisible grid of tiles that covers the entire figure or parent container. Each tile can contain an axes for displaying a plot. After creating a layout, call the nexttile function to place an axes object into the layout..."
The key there is the word "an"; that's one and only one.
A subplot() is no different than a regular axes other than it occupies a portion of the figure space. To use two axes with it, use yyaxis instead of trying to add an axes manually; much simpler.
> subplot(2,2,1)
>> yyaxis right
>> plot(1:5,x)
>> yyaxis left
>> plot(1:5,y)
>> subplot(2,2,4)
>> yyaxis right
>> plot(1:5,y)
>> yyaxis left
>> plot(1:5,x)
produced
yyaxis left after tiledlayout(2,2) simply overwrites the tiled object and creates a new full-sized axes in the figure so there's no subterfuge that way, either.
Seems like tiledlayout is definitely a second-class citizen as far as what can be done within it. Maybe it's just not quite ripe and needs time to mature; it is a pretty recent introduction.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by