MATLAB Answers

0

Plotting figure with 2 x-Axes and a y axis

Asked by Salil Kale on 21 Aug 2019
Latest activity Commented on by Subhadeep Koley on 29 Aug 2019
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
CODE:
plot(x1_data,y_data,'.','MarkerSize',12);
ylim([-0.2 (1+(0.1*length(filename1)))])
hold on;
grid on;
xlabel('Energy (eV)','fontsize',15),ylabel('Counts','fontsize',15);
set(gca,'fontsize',14,'FontWeight','Bold');
ax1 = gca;
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Ycolor','none',...
'Color','none');
hold on;
plot(x2_data,y_data,'Parent',ax2,'Color','none');
set(gca,'fontsize',14,'FontWeight','Bold');
hold on;
ver -support output:
>> ver -support
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.4.0.813654 (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
>>

  2 Comments

KSSV
on 21 Aug 2019
plot(y_data,x1_data)
hold on
plot(y_data,x2_data)
legend('x1','x2')
This would suffice right?
@KSSV
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.

Tags

Products


Release

R2018a

1 Answer

Answer by 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
plot(x1_data,y_data,'.','MarkerSize',12);
hold on;
grid on;
xlabel('Energy (eV)','fontsize',15),ylabel('Counts','fontsize',15);
set(gca,'fontsize',14,'FontWeight','Bold');
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
plot(x2_data,y_data,'Parent',hAX(2),'Color','none');
set(gca,'fontsize',14,'FontWeight','Bold');
Dual X axis.png

  2 Comments

Hi Subhadeep,
No this isn't what I want.
The attached figure shows the kind of plot that I want:
The only difference is that in my plot the energy wavelength axis positions will be interchanged. That is the Energy axis should be in the bottom and the numbers should be accending and the Wavelength axis should be at the top of the plot in a descending manner.
Okay I understand. Try the code below.
clc;close all;
load y_data;load x1_data;load x2_data;
plot(x1_data,y_data,'.','MarkerSize',12);
ylim([-0.2 (1+(0.1*length(filename1)))])
hold on;grid on;
xlabel('Energy (eV)','fontsize',12),ylabel('Counts','fontsize',12);
set(gca,'fontsize',12,'FontWeight','Bold');
ax1 = gca;
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Ycolor','none',...
'Color','none');
hold on;
plot(flip(x2_data),y_data,'Parent',ax2,'Color','none');
set(gca,'xdir','reverse'); % This line changes the Wavelength axis to decending
set(gca,'fontsize',12,'FontWeight','Bold');
decend.png

Sign in to comment.