Superimposing extra axes in second tile of a tiled chart layout

51 views (last 30 days)
Running the below appended code gives a figure shown as below. I want the green curve corresponding to y4 (green squares) to appear in the secondary axes (y-right and x-top) of tile 2. I know that I am making a mistake in line 38 (comments are added to this line) of the code by calling the axes corresponding to tile1 instead of tile2. But I do not know how to call the axes for the second tile. Could someone help me with this?
clear; clc;
%% Data for plotting
x1 = (15:15:1800)';
y1 = x1./(1-x1);
y2 = exp(-x1./(1+x1));
y3 = log10(x1);
y4 = log(x1);
x2 = 50*x1;
t = tiledlayout(1,2);
%% Tile 1
ax1 = axes(t);
l1 = plot(ax1,x1,y1,'-bo');
ax1.XAxisLocation = 'bottom';
ax1.YAxisLocation = 'left';
ax1.Color = 'none';
xlabel('time (days)'); ylabel('y1');
axis square;
ax2 = axes(t);
l2 = plot(ax2,x2,y2,'-xr');
ax2.XAxisLocation = 'top';
ax2.YAxisLocation = 'right';
ax2.Color = 'none';
xlabel('Number of cycles'); ylabel('y2');
ax2.XAxis.Exponent = 0; xtickformat('%.0f');
axis square;
ax1.Box = 'off';
ax2.Box = 'off';
%% Tile 2
ax3 = nexttile;
l3 = plot(ax3,x1,y3,'-pm');
ax3.Color = 'none';
xlabel('time (days)'); ylabel('y3');
axis square
ax4 = axes(t); % How to superimpose a secondary axes on the second tile instead of the first tile?
l4 = plot(ax4,x2,y4,'-sg');
ax4.XAxisLocation = 'top';
ax4.YAxisLocation = 'right';
ax4.Color = 'none';
xlabel('Number of cycles'); ylabel('y4');
ax4.XAxis.Exponent = 0; xtickformat('%.0f');
axis square
ax3.Box = 'off';
ax4.Box = 'off';

Accepted Answer

Harikrishnan Balachandran Nair
I understand that you have created a 2*1 tiled and are trying to superimpose another axis on the second tile.
By manually creating an 'axes' object with the tiled layout chart 't' as the parent container, the axes goes to the first tile by default.
In the code you have provided, inorder to create a new 'axes' object at the second tile, you can first create the axes 'ax4' with 't' as the parent container, and then manually specify the tile location by changing the property 'Tile' , inside the 'Layout' property of object 'ax4'. This can be done by adding the following lines of code:
ax4 =axes(t);
You can refer to the following Documentation to get a better idea on it :

More Answers (0)


Find more on 2-D and 3-D Plots in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!