Plotting figure with 2 x-Axes and a y axis
3 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
KSSV
2019 年 8 月 21 日
plot(y_data,x1_data)
hold on
plot(y_data,x2_data)
legend('x1','x2')
This would suffice right?
回答 (3 件)
Subhadeep Koley
2019 年 8 月 26 日
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');
3 件のコメント
Subhadeep Koley
2019 年 8 月 29 日
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');
Harsimran Singh
2022 年 3 月 14 日
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!
0 件のコメント
Adila Nalisa Binti Mohd Roslan
2022 年 3 月 16 日
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!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!