Plot with Multiple x-Axes and y-Axes using the fill function

1 view (last 30 days)
Markus on 7 Apr 2019
Answered: A. Sawas on 8 Apr 2019
Following this example:
I understand how to plot with multiple axes. But when I add the fill function to display a confidence interval to each line the second fill function messes up the axes properties. My code is:
figure
x1 = 0:0.1:40;
y1 = 4.*cos(x1)./(x1+2);
fill([x1 fliplr(x1)],[y1+0.1 fliplr(y1-0.1)],'b')
alpha(0.2)
line(x1,y1,'Color','k')
ax1 = gca; % current axes
ax1.XColor = 'r';
ax1.YColor = 'r';
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
x2 = 1:0.2:20;
y2 = x2.^2./x2.^3;
fill([x2 fliplr(x2)],[y2+0.1 fliplr(y2-0.1)],'r','Parent',ax2)
alpha(0.2)
line(x2,y2,'Parent',ax2,'Color','k')
If I comment out the second fill function it works nicely upon getting annoying tickmarks on ax2 from ax1. Where is my problem? Thanks in advance!
/Markus

A. Sawas on 8 Apr 2019
It seems Matlab is resetting ax2 settings to default after the fill command.
add this line after the fill command to set them again.
set(ax2,'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');