# 2nd Y Axis problem - All works until I add the 2nd data set

8 Ansichten (letzte 30 Tage)
10B am 20 Apr. 2016
Kommentiert: 10B am 21 Apr. 2016
Hello Community,
I am having difficulty with plotting to a second axis. Everything works fine until I actually try to add the 2nd Y data. I have tried various combinations of 'plotyy' and 'yyaxis right' etc. but the code below is the closest I have got to make this work. I think an added complication is that I am trying to plot 4 lines on X1,Y1 and points on X2, Y2 - but these points also relate to the same X - so in effect X1 and X2 are actually the same. My plotting code is as follows:
figure, plot(pih(:,1),'k'), hold on, plot(pih(:,2),'r'), plot(pih(:,3),'m'), plot(pih(:,4),'g'),
xlabel('Index')
ylabel('ORM')
set(gca,'Box','off');
axesPosition = get(gca,'Position');
hNewAxes = axes('Position',axesPosition,...
'Color','none',...
'YLim',[0 4],...
'YAxisLocation','right',...
'XTick',[],...
'Box','off');
ylabel(hNewAxes,'Group');
and that gives the 'OK' figure above - happy with this so far, but when I try to add the data for the Y2 with this:
plot(gca,df4CAT,'*')
The result is this 'problem' figure which has overwritten.
I think that there are 2 problems - firstly, that I am not handling the 'X' axis situation properly, and the second of course that the Y2 data is not writing to the figure using the correct axis, resulting in the overwrite.
Could anyone help with these problems please?
Regards,
10B.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Mike Garrity am 20 Apr. 2016
I think that the problem is that the plot function resets a lot of the axes properties that you set when you created the 2nd axes. Could you set those after the call to plot? Like so:
figure
plot(randi(10,[1 100]),'k')
hold on
plot(randi(10,[1 100]),'r')
plot(randi(10,[1 100]),'m')
plot(randi(10,[1 100]),'g')
xlabel('Index')
ylabel('ORM')
set(gca,'Box','off')
% Create 2nd axes
axesPosition = get(gca,'Position');
hNewAxes = axes('Position',axesPosition);
% Plot into it
plot(hNewAxes,randn(1,100))
% Customize 2nd axes
set(hNewAxes,'Color','none',...
'YLim',[0 4],...
'YAxisLocation','right',...
'XTick',[],...
'Box','off');
ylabel(hNewAxes,'Group')
The other option would be to set hold on after you create the axes:
hold(hNewAxes,'on')
and before the call to plot. The plot function doesn't reset a lot of properties if hold is on.
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
10B am 21 Apr. 2016
Excellent - Thanks very much Mike. I have used the first option as I get it to plot the way I want. I hadn't realised that the problem plot I did was essentially unfinished and if I carry on then I end with the plot I need.
Thanks again.
10B.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Axis Labels finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by