Subplot existing plots with left, right, bottom, and top axes.
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello
I have to plot with different units like the left axis has km unit and the right axis has mile unit. When I plot the right and top axes not shown in the subplot. Please see the code.
figure('Renderer', 'painters', 'Position', [10 10 700 500])
h1=figure (1)
x=0:1:10;
y=x.^2;
ax1 = gca;
plot(x,y)
grid on
set(gca,'GridLineStyle',':')
xt = get(ax1,'XTick')
yt = get(ax1,'YTick')
ax2 = copyobj(ax1,h1);
set(ax2,'XAxisLocation','top')
set(ax2,'YAxisLocation','right')
x = xt*3.28084;
y = yt*10.7639;
set(ax2,'XTickLabel',x)
set(ax2,'YTickLabel',y)
figure('Renderer', 'painters', 'Position', [10 10 700 500])
h2=figure (2)
x=0:1:10;
y=x.^0.5;
ax1 = gca;
plot(x,y)
grid on
set(gca,'GridLineStyle',':')
xt = get(ax1,'XTick')
yt = get(ax1,'YTick')
ax2 = copyobj(ax1,h2);
set(ax2,'XAxisLocation','top')
set(ax2,'YAxisLocation','right')
x = xt*3.28084;
y = yt*10.7639;
set(ax2,'XTickLabel',x)
set(ax2,'YTickLabel',y)
figlist=get(groot,'Children');
newfig=figure;
tcl=tiledlayout(newfig,'flow')
for i = 1:numel(figlist)
figure(figlist(i));
ax=gca;
ax.Parent=tcl;
ax.Layout.Tile=i;
end
0 Kommentare
Antworten (1)
Jatin
am 10 Sep. 2024
You can utilize a tiled layout using "nexttile", where each tile contains an independent plot. Additionally, by using "linkaxes", you can link "ax1" and "ax2" to synchronize the pan and zoom actions between the axes. Please check if the following code resolves your issue:
% Create a new figure with tiled layout
newfig = figure('Renderer', 'painters', 'Position', [10 10 700 500]);
tcl = tiledlayout(newfig, 2, 1); % Two rows, one column
% First subplot
nexttile;
x = 0:1:10;
y = x.^2;
plot(x, y);
grid on;
set(gca, 'GridLineStyle', ':');
xlabel('Distance (km)');
ylabel('Value (km^2)');
% Create secondary axes for miles
ax1 = gca;
ax2 = axes('Position', ax1.Position, 'XAxisLocation', 'top', 'YAxisLocation', 'right', 'Color', 'none', 'XColor', 'r', 'YColor', 'r');
linkaxes([ax1, ax2], 'xy'); % Link axes for synchronized zoom and pan
% Set secondary axes ticks and labels
x_miles = x * 0.621371; % Convert km to miles
y_miles = y * 0.386102; % Convert km^2 to mile^2
xticks(ax2, x);
yticks(ax2, y);
xticklabels(ax2, num2str(x_miles', '%.2f'));
yticklabels(ax2, num2str(y_miles', '%.2f'));
xlabel(ax2, 'Distance (miles)');
ylabel(ax2, 'Value (mile^2)');
% Second subplot
nexttile;
x = 0:1:10;
y = x.^0.5;
plot(x, y);
grid on;
set(gca, 'GridLineStyle', ':');
xlabel('Distance (km)');
ylabel('Value (sqrt(km))');
% Create secondary axes for miles
ax1 = gca;
ax2 = axes('Position', ax1.Position, 'XAxisLocation', 'top', 'YAxisLocation', 'right', 'Color', 'none', 'XColor', 'r', 'YColor', 'r');
linkaxes([ax1, ax2], 'xy'); % Link axes for synchronized zoom and pan
% Set secondary axes ticks and labels
x_miles = x * 0.621371; % Convert km to miles
y_miles = y * sqrt(0.621371); % Convert sqrt(km) to sqrt(miles)
xticks(ax2, x);
yticks(ax2, y);
xticklabels(ax2, num2str(x_miles', '%.2f'));
yticklabels(ax2, num2str(y_miles', '%.2f'));
xlabel(ax2, 'Distance (miles)');
ylabel(ax2, 'Value (sqrt(miles))');
Refer to the below documentation on "nexttile" for more details:
0 Kommentare
Siehe auch
Kategorien
Mehr zu Subplots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!