how do I add a second x-axis label below existing one instead of plotting two figures ?

186 visualizaciones (últimos 30 días)
I've written a code (SteelPlateThicknessParam4Help) which uses 2 functions (attahced also) - should be in the same directory.
The main output is 2 plots, which differ in their x-axis (attahced the photos).
How can I plot these different scaled x-axis on one plot instead of two ?
Irad

Respuesta aceptada

Dyuman Joshi
Dyuman Joshi el 17 de Ag. de 2023
%Minimum working example solution for differently scaled x-axis with similar range of y-values
%Adjust/Modify properties as necessary (font size, xticks, etc)
x1 = [1 2 3 4 5];
x2 = [0.1 0.2 0.3 0.4 0.5];
y = rand(2, 5);
%This generates values between 0 and 1
%i.e both columns have similar range of values
%% Use multiple axes
%1st axes and plotting
ax1 = axes('Position', [0.15 0.2 0.775 0.715]);
p1 = plot(ax1, x1 , y(1,:), 'r*-');
xlabel('X1 label')
%Add another axes at the same position and plot
ax2 = axes('Position', ax1.Position);
p2 = plot(ax2, x2 , y(2,:), 'bo-');
ax2.Color ='none';
ax2.YTick=[];
ax2.YTickLabel=[];
xlabel('X2 label')
%Adjust tick label location for the 2nd axes
ax2.XRuler.TickLabelGapOffset=33;
%link the axes
linkaxes([ax1, ax2], 'y')
%Add common legend for both plots by combining plot handles
legend([p1;p2],'a','b')
  3 comentarios
Irad Brandys
Irad Brandys el 19 de Ag. de 2023
Dear Joshi,
Thank you very much for the example.
Much more clear now and works well for me.
Irad

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Specifying Target for Graphics Output en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by