Clear Filters
Clear Filters

How to plot two graphs with different x and y axis on the same figure?

15 views (last 30 days)
clear
clc
%read day 1 data
files = dir('UKM-SIDpi-10_NWC_2018-11-23.csv');
for i=1:numel(files)
tab = readtable(files(i).name); % or csvread(files(i).name)
tab = renamevars(tab,["Var1","Var2"],["x1","y1"]);
tabA = tab(11520:17280,[1 2]);
end
%read day 2 data
files2 = dir('UKM-SIDpi-10_NWC_2018-11-24.csv');
for ii=1:numel(files)
tab2 = readtable(files2(ii).name);
tab2 = renamevars(tab2,["Var1","Var2"],["x1","y1"]);
tabB = tab2(1:11520,[1 2]);
end
%combine table
new_table = [tabA; tabB];
%%read data GOES
%%call file and display all variables
files3 = dir('sci_xrsf-l2-avg1m_g16_d20181124_v2-2-0.nc');
ncdisp('sci_xrsf-l2-avg1m_g16_d20181124_v2-2-0.nc');
% load the variable in the file
xrsa_flux = ncread('sci_xrsf-l2-avg1m_g16_d20181124_v2-2-0.nc','xrsa_flux');
xrsb_flux = ncread('sci_xrsf-l2-avg1m_g16_d20181124_v2-2-0.nc','xrsb_flux');
time = ncread('sci_xrsf-l2-avg1m_g16_d20181124_v2-2-0.nc','time');
time_3 = double(time)/86400 + datenum('2000-01-01 12:00:00');
displaytime = datestr(time_3(:,1),'dd-mm-yyyy HH:MM:SS');
time_cell = cellstr(displaytime);
tab4 = cell2table(time_cell);
time_goes = cell2mat(time_cell);
tab3 = table(displaytime,xrsa_flux,xrsb_flux);
tab3 = renamevars(tab3,["displaytime","xrsa_flux","xrsb_flux"],["x1","y1","y2"]);
%%change time char to double so that can plot graph
time_str = convertCharsToStrings(time_3);
%make tiledlayout
t = tiledlayout(1,1);
%plot graph ukmsidpi
ax1 = gca;
yyaxis left;
plot(ax1,new_table.x1,new_table.y1,'-r');
axis tight;
xlabel('Time (UT)');
ylabel('signal Strength');
MyTimes = ["2018-11-23 16:00:00"; "2018-11-23 20:00:00"; "2018-11-23 21:00:00"; "2018-11-23 22:00:00"; "2018-11-23 23:00:00"; "2018-11-24 02:00:00"; "2018-11-24 06:00:00"; "2018-11-24 09:00:00"; "2018-11-24 10:00:00"; "2018-11-24 12:00:00"; "2018-11-24 15:59:55"];
MyTimes = datetime(MyTimes);
xticks(MyTimes);
xticklabels({'24/11 00', '24/11 04', '24/11 05', '24/11 06','24/11 07', '24/11 10', '24/11 14', '24/11 17', '24/11 18', '24/11 20', '24/11 23:59'});
xtickangle(45);
xline([datetime("2018-11-23 21:00:00") datetime("2018-11-23 23:00:00") datetime("2018-11-24 09:00:00") datetime("2018-11-24 12:00:00")]);
title('Signal strength vs Time(UT)');
xlabel('Time (UT)');
ylabel('signal Strength');
%plot graph GOES
ax2 = axes(t);
yyaxis right;
plot(ax2,time_str,xrsa_flux,'-g',time_str,xrsb_flux,'-b');
xticks([]);
  2 Comments
hareesh rao
hareesh rao on 28 Jun 2023
As you can see, my first graph is plotted on my second graph. I tried to use hold function but it doesn't seem to work as well
DGM
DGM on 28 Jun 2023
Without seeing the data, it's not clear whether your x-data is correlated. If it's not, then it's not clear why you'd try to put them in overlaid axes, or otherwise make them appear correlated.
If you have a big complicated thing that depends on data, it's often helpful to either provide some data (real or fake), or otherwise reduce the problem to a minimum working example that demonstrates the problem.
I'm just going to guess:
% three series dependent on the same time vector
% i'm not going to bother wrangling datetimes for this example
t = linspace(0,4*pi,1000);
y1 = sin(2*t);
y2 = 2*sin(t);
y3 = 2*cos(t);
%make tiledlayout
% this isn't used for anything, so why have it?
%t = tiledlayout(1,1);
%plot graph ukmsidpi
ax1 = gca;
yyaxis left;
plot(ax1,t,y1,'-r');
axis tight;
xlabel('Time (UT)');
ylabel('signal Strength');
%MyTimes = ["2018-11-23 16:00:00"; "2018-11-23 20:00:00"; "2018-11-23 21:00:00"; "2018-11-23 22:00:00"; "2018-11-23 23:00:00"; "2018-11-24 02:00:00"; "2018-11-24 06:00:00"; "2018-11-24 09:00:00"; "2018-11-24 10:00:00"; "2018-11-24 12:00:00"; "2018-11-24 15:59:55"];
%MyTimes = datetime(MyTimes);
%xticks(MyTimes);
%xticklabels({'24/11 00', '24/11 04', '24/11 05', '24/11 06','24/11 07', '24/11 10', '24/11 14', '24/11 17', '24/11 18', '24/11 20', '24/11 23:59'});
xtickangle(45);
%xline([datetime("2018-11-23 21:00:00") datetime("2018-11-23 23:00:00") datetime("2018-11-24 09:00:00") datetime("2018-11-24 12:00:00")]);
title('Signal strength vs Time(UT)');
xlabel('Time (UT)');
ylabel('signal Strength');
%plot graph GOES
%ax2 = axes(t); % if your xdata is correlated, you shouldn't need overlaid axes
yyaxis right;
plot(ax1,t,y2,'-g',t,y3,'-b'); % two series sharing the same y-ruler
%xticks([]); % if you don't have overlaid axes, you don't need to hide ticks

Sign in to comment.

Answers (1)

Avni Agrawal
Avni Agrawal on 16 Nov 2023
Edited: Avni Agrawal on 16 Nov 2023
Hi,
I understand that you are trying to plot two different plots in single figure. To plot two graphs with different x and y axes on the same figure in MATLAB, you can use the yyaxis function. Please take a look at code snippet mentioned below:
% Create a new figure
figure;
% Plot the first graph
yyaxis left; % Use the left y-axis
plot(new_table.x1,new_table.y1,'-r'); %remove ax1 i.e first paramater plot(ax1,new_table.x1,new_table.y1,'-r')
% Plot the second graph
yyaxis right; % Use the right y-axis
plot(time_str,xrsa_flux,'-g',time_str,xrsb_flux,'-b');%remove ax1 i.e first paramater plot(ax2, time_str,xrsa_flux,'-g',time_str,xrsb_flux,'-b')
Please take a look at this documentation for better understanding:
  4 Comments
DGM
DGM on 16 Nov 2023
Edited: DGM on 16 Nov 2023
I noticed that, but it was never clear to me that they weren't trying to do something like a stacked plot. They never clarified what the intentions were or whether the data was correlated. Granted, it's hard to argue anything but guesses at this point, but I'm assuming that it's still at least unclear whether extra axes were appropriate.
Since it's a dead question, I think it's fair to create your own interpretation of the question and then answer it as you've interpreted it, but I think you should make it clear what things you're assuming in your interpretation. The text of your answer only suggests the use of yyaxis for problems generically described by the thread title. One needs to scroll into the comments and then scroll up and compare it to the question code to notice that the changes have nothing to do with yyaxis().
An answer which includes complete and demonstrated (i.e. executed in the forum editor) code is at least comprehensive in that it contains evidence of what the assumptions and expected outcomes should be.
Avni Agrawal
Avni Agrawal on 16 Nov 2023
I apologize for any confusion caused earlier. I had mentioned in the comments to remove 'ax1', which is the first parameter in the plot function. I'll ensure to provide clear instructions in the future

Sign in to comment.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!