How to plot a 2nd Yaxis on a 2D plot

I am wanting to plot a second Y axis on my 2D plot.
I need to include 'Elevation' from the textread line
So my plot should be, left yaxis = Bougeur Anomaly, right Yaxis = Elevation, Xaxis = Profile
My code so far
%% Program for reading data from txt file
clear all;
close all;
main_path = 'C:\Users\user\Documents\';
print_check = 1;
data_path= strcat(main_path, 'Data\gobs_elevation_2021.txt');
scriptname = mfilename( 'fullpath' );
%% Read data file
[Profile Elevation gobs]=textread(data_path,'%f %f %f', 'headerlines', 1);
FreeAir = 3.086*Elevation;
Bouguer = 0.4192*2.65*Elevation;
gLat = -8.108*sind(2*52);
gBAnom = gobs+gLat+(FreeAir-Bouguer);
%%Simple Plot
fig1=figure('Position',[10 10 550 550])
hold on
plot(Profile, gobs,'.k','Markersize',8)
plot(Profile, gBAnom,'.r','Markersize',8)
% hold on
% plot(depth,fit,'Linewidth', 2,'LineStyle','--','Color',[0.17 0.3 0.34])
grid on
%xlim([0 100])
%ylim([-10 10])
%set(gca,'YDir','reverse')
set(gca,'YDir','reverse')
set(gca,'XDir','reverse')
xlabel('Profile [m]','FontSize',16)
ylabel('Bougeur Anomaly','FontSize',16)
thetitle = 'Effect of a Sphere';
title(thetitle,'FontSize',16);

답변 (4개)

Star Strider
Star Strider 2021년 3월 28일

0 개 추천

If you have R2016a or later, use the yyaxis function. For earlier versions, use plotyy.

댓글 수: 3

First, the ylim call must be:
ylim([45 70])
With that change, the code in this Comment should work.
nicholas moran
nicholas moran 2021년 3월 29일
Thank you
Star Strider
Star Strider 2021년 3월 29일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

댓글을 달려면 로그인하십시오.

nicholas moran
nicholas moran 2021년 3월 28일

0 개 추천

I will give it a go
nicholas moran
nicholas moran 2021년 3월 28일

0 개 추천

So far I can't get the third variable;
%% Program for reading data from txt file
clear all;
close all;
main_path = 'C:\Users\user\\';
print_check = 1;
data_path= strcat(main_path, 'Data\gobs_elevation_2021.txt');
scriptname = mfilename( 'fullpath' );
%% Read data file
[Profile Elevation gobs]=textread(data_path,'%f %f %f', 'headerlines', 1);
FreeAir = 3.086*Elevation;
Bouguer = 0.4192*2.65*Elevation;
gLat = -8.108*sind(2*52);
gBAnom = gobs+gLat+(FreeAir-Bouguer);
%%Simple Plot
fig1=figure('Position',[10 10 550 550])
hold on
x = Profile;
y = Bouguer;
yyaxis left
plot(Profile,Bouguer)
z = Elevation;
yyaxis right
plot(x,z)
ylim([70 45])
% hold on
% plot(depth,fit,'Linewidth', 2,'LineStyle','--','Color',[0.17 0.3 0.34])
grid on
%xlim([0 100])
%ylim([-10 10])
%yylim9([45 75])
%set(gca,'YDir','reverse')
set(gca,'YDir','reverse')
set(gca,'XDir','reverse')
xlabel('Profile [m]','FontSize',16)
ylabel('Bougeur Anomaly','FontSize',16)
yylabel('Elevation''FontSize',16)
thetitle = 'Effect of a Sphere';
title(thetitle,'FontSize',16);
Error using ylim (line 31)
Limits must be a 2-element vector of increasing numeric values.
Error in Test (line 32)
ylim([70 45])
nicholas moran
nicholas moran 2021년 3월 28일
편집: nicholas moran 2021년 3월 28일

0 개 추천

Trying to achieve this but with Elevation on the other Yaxis:

카테고리

도움말 센터File Exchange에서 Integration with Online Platforms에 대해 자세히 알아보기

질문:

2021년 3월 28일

댓글:

2021년 3월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by