0

Here is my code

clear all;clc
x = linspace(0, 10, 100);

axes('Position', [.075,.075,.9,.2], ...
     'XColor',   'k', ...
     'YColor',   [0 0 0]);
fill(x, circshift(sin(x), 50), 'red', 'EdgeColor','None');
ylabel('Fancy Sine', 'FontSize', 11);

% Adding this the upper vanishes and the y axes is on left not right side
axes('Position',      [.075,.075,.9,.2], ...
     'XColor',        'k', ...
     'YAxisLocation', 'Right', ...
     'Color',         'none');
plot(x, x, 'Color', [.2 .4 .8]);
ylabel('Line Graph', 'FontSize', 11);

xlabel('X', 'FontSize', 11);

The single axis works fine

enter image description here

But when I want to add the second axis, the first disappears and the new axis is not on the right, but left side..

enter image description here

But both plots should be in one axis and the second y axis should be on the right side.

How to achieve this?

embert
  • 7,336
  • 10
  • 49
  • 78

1 Answers1

2

plot automatically sets the axis properties to the default. Use hold to stop this or specify the axis properties after your plot call.

An example of the former:

clear all;clc
x = linspace(0, 10, 100);

ax1 = axes('Position', [.075,.075,.9,.2], ...
     'XColor',   'k', ...
     'YColor',   [0 0 0]);
p1 = fill(x, circshift(sin(x), 50), 'red', 'EdgeColor','None','Parent',ax1);
ylabel('Fancy Sine', 'FontSize', 11);

% Adding this the upper vanishes and the y axes is on left not right side
ax2 = axes('Position',      [.075,.075,.9,.2], ...
     'XColor',        'k', ...
     'YAxisLocation', 'Right', ...
     'Color',         'none');
hold on
p2 = plot(ax2,x, x, 'Color', [.2 .4 .8]);
hold off
ylabel('Line Graph', 'FontSize', 11);
xlabel('X', 'FontSize', 11);

Edit1: The reason you don't need to do this for your fill call is because it creates a patch object in your axes and does not invoke a plot command.

sco1
  • 12,154
  • 5
  • 26
  • 48