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

41 ビュー (過去 30 日間)
Roger Wang
Roger Wang 2021 年 1 月 19 日
コメント済み: Sam Chak 2024 年 8 月 18 日
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

回答 (2 件)

Jasmin McInerney
Jasmin McInerney 2021 年 8 月 31 日
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 2021 年 1 月 19 日
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.
  3 件のコメント
dpb
dpb 2024 年 8 月 18 日
You must "roll your own" (look at the source for yyaxis to get the idea) or, I believe there are some submittals at the File Exchange...
Sam Chak
Sam Chak 2024 年 8 月 18 日
Aliza, do you want to use "xxaxis" to show the implicit intersection of two variables that has a common y value?
Is it something like putting the Pressure vs. Temperature, and the Pressure vs. Volume in the same chart?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAxes Appearance についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by