Plotting figure with 2 x-Axes and a y axis

9 vues (au cours des 30 derniers jours)
Salil Kale
Salil Kale le 21 Août 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 commentaires
KSSV
KSSV le 21 Août 2019
plot(y_data,x1_data)
hold on
plot(y_data,x2_data)
legend('x1','x2')
This would suffice right?
Salil Kale
Salil Kale le 21 Août 2019
@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.

Connectez-vous pour commenter.

Réponses (3)

Subhadeep Koley
Subhadeep Koley le 26 Août 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
  3 commentaires
Subhadeep Koley
Subhadeep Koley le 29 Août 2019
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
Dzul Idham
Dzul Idham le 30 Mai 2021
how set title on above graph

Connectez-vous pour commenter.


Harsimran Singh
Harsimran Singh le 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!

Catégories

En savoir plus sur Manage Products dans Help Center et File Exchange

Tags

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by