Plotting figure with 2 x-Axes and a y axis

38 views (last 30 days)
I have 3 arrays of length 903:
x1_ data, x2_data and y_data.
x1_data, x2_data are the bottom and top x axes that I want and
and y_data is the common y_axis I want for both x_axes.
I have attached array.mat files for the 3 variables and the output that I am getting.
Also below is the relevant snippet of the code I have used:
Also output of ver- output
ylim([-0.2 (1+(0.1*length(filename1)))])
hold on;
grid on;
xlabel('Energy (eV)','fontsize',15),ylabel('Counts','fontsize',15);
ax1 = gca;
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
hold on;
hold on;
ver -support output:
>> ver -support
MATLAB Version: (R2018a)
MATLAB License Number: 623588
Operating System: Microsoft Windows 10 Home Version 10.0 (Build 18362)
Java Version: Java 1.8.0_144-b01 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
MATLAB Version 9.4 (R2018a) License 623588
Simulink Version 9.1 (R2018a) License 623588
Bioinformatics Toolbox Version 4.10 (R2018a) License 623588
Control System Toolbox Version 10.4 (R2018a) License 623588
Curve Fitting Toolbox Version 3.5.7 (R2018a) License 623588
DSP System Toolbox Version 9.6 (R2018a) License 623588
Data Acquisition Toolbox Version 3.13 (R2018a) License 623588
Financial Toolbox Version 5.11 (R2018a) License 623588
Image Processing Toolbox Version 10.2 (R2018a) License 623588
Instrument Control Toolbox Version 3.13 (R2018a) License 623588
Optimization Toolbox Version 8.1 (R2018a) License 623588
Parallel Computing Toolbox Version 6.12 (R2018a) License 623588
Signal Processing Toolbox Version 8.0 (R2018a) License 623588
Simscape Version 4.4 (R2018a) License 623588
Simscape Multibody Version 5.2 (R2018a) License 623588
Simulink Control Design Version 5.1 (R2018a) License 623588
Stateflow Version 9.1 (R2018a) License 623588
Statistics and Machine Learning Toolbox Version 11.3 (R2018a) License 623588
Symbolic Math Toolbox Version 8.1 (R2018a) License 623588
Salil Kale
Salil Kale on 21 Aug 2019
No this isn't what I want. Its 2 x-Axis I want on the same figure. NOT two curves on the same figure.

Sign in to comment.

Answers (3)

Subhadeep Koley
Subhadeep Koley on 26 Aug 2019
Since the code you provided already creates two X axes one in top and one in bottom therefore, I assume you want both the X axes in the bottom of the figure.
The following code might help you.
load y_data;
load x1_data;
load x2_data;
hAX=axes;% first axes, save handle
hold on;
grid on;
xlabel('Energy (eV)','fontsize',15),ylabel('Counts','fontsize',15);
pos=get(hAX,'position');% get the position vector
pos1=pos(2); % save the original bottom position
pos(2)=pos(2)+pos1; pos(4)=pos(4)-pos1;% raise bottom/reduce height->same overall %upper position
set(hAX,'position',pos)% and resize first axes
pos(2)=0.2; pos(4)=0.00;% reset bottom to original and small height
hAX(2)=axes('position',pos,'color','none');% and create the second
Dual X axis.png

Sign in to comment.

Harsimran Singh
Harsimran Singh on 14 Mar 2022
hello Subhadeep, I am using your code which you metioned above. It works well. But I am facing a problem.
I have data in 3 columns,
Column #1= Date(YYYY-MM-DD)
Column #2= Time(hr)
Column #3= Temp
I want to plot a graph with Temp on y-axis and need to have two x-axis. One x-axis should have Time(HR) and other x-axis should have Date(YYYY-MM-DD).
At the end I want time(HR) should match with date. Means when date is 2021-05-18 on one x-axis, time on the another x-axis should be 1000.44, as per data
I am using Matlab R2021b
Any suggestion, how I could solve this?
Thanks in advance!

Adila Nalisa Binti Mohd Roslan
Hi Subhadeep, I am using your code that you provided. It works well. However I am notice that for the second axis is not link with the curves for 1st axis. How can I link 2 axis so that the curve that have been plotted follow for both x-axis.
Thanks in advance!





Community Treasure Hunt

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

Start Hunting!