# Issue with saving a plot with two y axis

5 views (last 30 days)
Masoud Taleb on 20 Dec 2020
Commented: Masoud Taleb on 20 Dec 2020
Hello
I have written a code that gives me a plot with two y axis like this:
But when I save it, something goes wrong and gives me following image? Is there a setting or piece of code that I should add to my code?
Thanks in advance for possible solutions.
clear
clc
close all
col = jet(6);
xa = A(2:end,1);
y_all= A(1,2:end);
za = A(2:end,2:end) ;
k = [140;150;163;175;186;198;210;221;233;245];
r1 = [35;35;33;31;29;28;27;26;24;24];
r2 = [38;39;37;34;33;32;31;29;27;26];
r3 = [51;50;49;49;42;45;45;44;42;42];
r4 = [54;54;53;52;48;47;48;47;45;44];
for B = 1:10;
g = k(B,1);
w(B) = xa(g,1).*10^-9;
E = (((6.626*10^-34)*(2.9979*10^+8)/(1.602*10^-19))./w);
m1 = r1(B,1);
m2 = r2(B,1);
m3 = r3(B,1);
m4 = r4(B,1);
% zz1 is the Line number of W to be plotted
zz1 = k(B,1);
ya = y_all(1,m1:m2);
zc = za(zz1,m1:m2);
% plot (ya,za(zz1,34:38));
% hold on
[fitresult, gof] = createFit1(ya, zc);
hold on
x= linspace(min(xa),max(xa),100);
% plot (xa,fitresult(xa),'linewidth',2);
% hold on
a_first(B) = fitresult.a1;
b_first(B)= fitresult.b1;
c_first(B) = fitresult.c1;
sigma_first(B) = c_first(B)./2;
width_first(B) = 2.355.*sigma_first(B);
yaa = y_all(1,m3:m4);
zcc = za(zz1,m3:m4);
% plot (yaa,za(zz1,48:54));
% hold on
[fitresult, gof] = createFit11(yaa, zcc);
hold on
x= linspace(min(xa),max(xa),100);
% plot (xa,fitresult(xa),'linewidth',2);
hold on
a_second(B) = fitresult.a1;
b_second(B) = fitresult.b1;
c_second(B) = fitresult.c1;
sigma_second(B) = c_second(B)./2;
width_second(B) = 2.355.*sigma_second(B);
D(B)= b_second(B)-b_first(B);
Max_error (B) = abs((width_first(B)+width_second(B))./(2.*10^3));
Min_error (B)= abs((width_first(B)-width_second(B))./(2.*10^3));
end
n = (2.*pi.*10^3)./[D]
errorbar(n,E,Min_error,Max_error,'d','horizontal')
scatter(n, E,30,'k','filled','d');
ylabel ('Photon Energy / eV');
% Create second Y axes on the right.
a2 = axes('YAxisLocation', 'Right');
% Hide second plot.
set(a2, 'color', 'none');
set(a2, 'XTick', []);
% Set scala for second Y.
set(a2, 'YLim', [550.71 640.61]);
set(gca, 'YDir','reverse');
ylabel('wavelength / nm');
clc
Masoud Taleb on 20 Dec 2020
I fixed my problem by deleting last 10 lines and putting following code:
yyaxis right
plot(n, W,'color', 'none');
set(gca, 'YDir','reverse');
ylabel('wavelength / nm');
set(gca,'YColor','k');
clc
Now, everything works fine and all the fig can be saved.