Octave: Add secondary y axis to existing plot












3















I am using Octave 4.2.1 portable under Win 10.



I have several plots in a single chart, with a single y axis, each one created with a plot(...) sentence.
I want to add a plot in a secondary y axis to this existing plot, not creating from the beginning the two axes with plotyy, (EDIT) and being able to work all the same, e.g., adding a legend, etc.



What is the correct usage for this?



If I execute



plotyy(x, ysec) ;


or



ax = gca ;
plotyy(ax, x, ysec) ;


I get



error: Invalid call to plotyy.  Correct usage is:    
-- plotyy (X1, Y1, X2, Y2)
-- plotyy (..., FUN)
-- plotyy (..., FUN1, FUN2)
-- plotyy (HAX, ...)
-- [AX, H1, H2] = plotyy (...)


This shows something similar for Matlab, but I am not sure all code that works with a secondary axis that would be created with plotyy, will work with an axis created this way as well.










share|improve this question




















  • 1





    Do you want to preserve the formatting for the first axes? If not, it would be simple to replace the existing plot by using plotyy with the data already in the axes and the new data.

    – Cris Luengo
    Jan 2 at 7:56











  • @CrisLuengo - In principle, I mean to keep formatting. But if nothing else shows up, I might try to recreate such formatting if reset. So if you have a solution, I would like to try it.

    – sancho.s
    Jan 3 at 2:01


















3















I am using Octave 4.2.1 portable under Win 10.



I have several plots in a single chart, with a single y axis, each one created with a plot(...) sentence.
I want to add a plot in a secondary y axis to this existing plot, not creating from the beginning the two axes with plotyy, (EDIT) and being able to work all the same, e.g., adding a legend, etc.



What is the correct usage for this?



If I execute



plotyy(x, ysec) ;


or



ax = gca ;
plotyy(ax, x, ysec) ;


I get



error: Invalid call to plotyy.  Correct usage is:    
-- plotyy (X1, Y1, X2, Y2)
-- plotyy (..., FUN)
-- plotyy (..., FUN1, FUN2)
-- plotyy (HAX, ...)
-- [AX, H1, H2] = plotyy (...)


This shows something similar for Matlab, but I am not sure all code that works with a secondary axis that would be created with plotyy, will work with an axis created this way as well.










share|improve this question




















  • 1





    Do you want to preserve the formatting for the first axes? If not, it would be simple to replace the existing plot by using plotyy with the data already in the axes and the new data.

    – Cris Luengo
    Jan 2 at 7:56











  • @CrisLuengo - In principle, I mean to keep formatting. But if nothing else shows up, I might try to recreate such formatting if reset. So if you have a solution, I would like to try it.

    – sancho.s
    Jan 3 at 2:01
















3












3








3








I am using Octave 4.2.1 portable under Win 10.



I have several plots in a single chart, with a single y axis, each one created with a plot(...) sentence.
I want to add a plot in a secondary y axis to this existing plot, not creating from the beginning the two axes with plotyy, (EDIT) and being able to work all the same, e.g., adding a legend, etc.



What is the correct usage for this?



If I execute



plotyy(x, ysec) ;


or



ax = gca ;
plotyy(ax, x, ysec) ;


I get



error: Invalid call to plotyy.  Correct usage is:    
-- plotyy (X1, Y1, X2, Y2)
-- plotyy (..., FUN)
-- plotyy (..., FUN1, FUN2)
-- plotyy (HAX, ...)
-- [AX, H1, H2] = plotyy (...)


This shows something similar for Matlab, but I am not sure all code that works with a secondary axis that would be created with plotyy, will work with an axis created this way as well.










share|improve this question
















I am using Octave 4.2.1 portable under Win 10.



I have several plots in a single chart, with a single y axis, each one created with a plot(...) sentence.
I want to add a plot in a secondary y axis to this existing plot, not creating from the beginning the two axes with plotyy, (EDIT) and being able to work all the same, e.g., adding a legend, etc.



What is the correct usage for this?



If I execute



plotyy(x, ysec) ;


or



ax = gca ;
plotyy(ax, x, ysec) ;


I get



error: Invalid call to plotyy.  Correct usage is:    
-- plotyy (X1, Y1, X2, Y2)
-- plotyy (..., FUN)
-- plotyy (..., FUN1, FUN2)
-- plotyy (HAX, ...)
-- [AX, H1, H2] = plotyy (...)


This shows something similar for Matlab, but I am not sure all code that works with a secondary axis that would be created with plotyy, will work with an axis created this way as well.







matlab plot octave matlab-figure yaxis






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 8:39







sancho.s

















asked Jan 2 at 7:43









sancho.ssancho.s

6,47583794




6,47583794








  • 1





    Do you want to preserve the formatting for the first axes? If not, it would be simple to replace the existing plot by using plotyy with the data already in the axes and the new data.

    – Cris Luengo
    Jan 2 at 7:56











  • @CrisLuengo - In principle, I mean to keep formatting. But if nothing else shows up, I might try to recreate such formatting if reset. So if you have a solution, I would like to try it.

    – sancho.s
    Jan 3 at 2:01
















  • 1





    Do you want to preserve the formatting for the first axes? If not, it would be simple to replace the existing plot by using plotyy with the data already in the axes and the new data.

    – Cris Luengo
    Jan 2 at 7:56











  • @CrisLuengo - In principle, I mean to keep formatting. But if nothing else shows up, I might try to recreate such formatting if reset. So if you have a solution, I would like to try it.

    – sancho.s
    Jan 3 at 2:01










1




1





Do you want to preserve the formatting for the first axes? If not, it would be simple to replace the existing plot by using plotyy with the data already in the axes and the new data.

– Cris Luengo
Jan 2 at 7:56





Do you want to preserve the formatting for the first axes? If not, it would be simple to replace the existing plot by using plotyy with the data already in the axes and the new data.

– Cris Luengo
Jan 2 at 7:56













@CrisLuengo - In principle, I mean to keep formatting. But if nothing else shows up, I might try to recreate such formatting if reset. So if you have a solution, I would like to try it.

– sancho.s
Jan 3 at 2:01







@CrisLuengo - In principle, I mean to keep formatting. But if nothing else shows up, I might try to recreate such formatting if reset. So if you have a solution, I would like to try it.

– sancho.s
Jan 3 at 2:01














1 Answer
1






active

oldest

votes


















1














Here are two options. I've tested these in MATLAB, but I'm pretty sure it'll work in Octave the same way.



Let's start with some random data plotted normally:



% Initial graph
x1 = linspace(0,1,100);
y1 = randn(size(x1));
clf
plot(x1,y1,'k');

% New data
x2 = x1;
y2 = rand(size(x2));


Replot the figure, adding new data in second axis



Here we retrieve the data from the current axes (it would be better if you preserved the axes handle from when you made your first plot, of course). We then plot a new figure using plotyy that contains the old data and the new data.



ax = gca;
h0 = get(ax,'children'); % This is the handle to the plotted line
x1 = get(h0,'xdata'); % Get data for line
y1 = get(h0,'ydata');
cla(ax) % Clear axes
plotyy(ax,x1,y1,x2,y2); % Plot old and new data


plot with left and right axis



Preserve existing axes and plot, add a second axis with the new data



Here we use hold on to prevent the current data from being deleted, then plot the new data with plotyy that also adds a dummy plot to the left axis (a single point 0,0). We then delete this dummy plot.



It turns out that adding this dummy plot still causes the left axis to change. So this code first preserves the location of the tick marks and the limits, and then applies those again after plotting. It also makes the left axis the same color as the line that was already there.



ax = gca;
yl = get(ax,'ylim');
yt = get(ax,'ytick');
h0 = get(ax,'children');
hold on
[ax,h1,h2] = plotyy(ax,0,0,x2,y2);
delete(h1)
set(ax(1),'ycolor',get(h0,'color'),'ylim',yl,'ytick',yt)


plot with left and right axis






share|improve this answer
























  • Please note that: 1) I have several plots on the primary y-axis, 2) I would like to work "normally", i.e., add a suitable legend, etc. Would I be able to add additional plots on the primary/secondary axes on separate code lines (as I am doing right now)?

    – sancho.s
    Jan 3 at 8:36











  • @sancho.s: If you have multiple plots in the first figure, use the second method. You’d have to adapt how you set the color of the left y axis. Maybe you can not change it at all, hopefully it will still be clear which plots belong to which axis. I’d be careful with putting too many plots in one figure. The more lines, the harder it is to see what is going on in the data! I find small multiples a better option for showing different lines together.

    – Cris Luengo
    Jan 3 at 14:08













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002845%2foctave-add-secondary-y-axis-to-existing-plot%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Here are two options. I've tested these in MATLAB, but I'm pretty sure it'll work in Octave the same way.



Let's start with some random data plotted normally:



% Initial graph
x1 = linspace(0,1,100);
y1 = randn(size(x1));
clf
plot(x1,y1,'k');

% New data
x2 = x1;
y2 = rand(size(x2));


Replot the figure, adding new data in second axis



Here we retrieve the data from the current axes (it would be better if you preserved the axes handle from when you made your first plot, of course). We then plot a new figure using plotyy that contains the old data and the new data.



ax = gca;
h0 = get(ax,'children'); % This is the handle to the plotted line
x1 = get(h0,'xdata'); % Get data for line
y1 = get(h0,'ydata');
cla(ax) % Clear axes
plotyy(ax,x1,y1,x2,y2); % Plot old and new data


plot with left and right axis



Preserve existing axes and plot, add a second axis with the new data



Here we use hold on to prevent the current data from being deleted, then plot the new data with plotyy that also adds a dummy plot to the left axis (a single point 0,0). We then delete this dummy plot.



It turns out that adding this dummy plot still causes the left axis to change. So this code first preserves the location of the tick marks and the limits, and then applies those again after plotting. It also makes the left axis the same color as the line that was already there.



ax = gca;
yl = get(ax,'ylim');
yt = get(ax,'ytick');
h0 = get(ax,'children');
hold on
[ax,h1,h2] = plotyy(ax,0,0,x2,y2);
delete(h1)
set(ax(1),'ycolor',get(h0,'color'),'ylim',yl,'ytick',yt)


plot with left and right axis






share|improve this answer
























  • Please note that: 1) I have several plots on the primary y-axis, 2) I would like to work "normally", i.e., add a suitable legend, etc. Would I be able to add additional plots on the primary/secondary axes on separate code lines (as I am doing right now)?

    – sancho.s
    Jan 3 at 8:36











  • @sancho.s: If you have multiple plots in the first figure, use the second method. You’d have to adapt how you set the color of the left y axis. Maybe you can not change it at all, hopefully it will still be clear which plots belong to which axis. I’d be careful with putting too many plots in one figure. The more lines, the harder it is to see what is going on in the data! I find small multiples a better option for showing different lines together.

    – Cris Luengo
    Jan 3 at 14:08


















1














Here are two options. I've tested these in MATLAB, but I'm pretty sure it'll work in Octave the same way.



Let's start with some random data plotted normally:



% Initial graph
x1 = linspace(0,1,100);
y1 = randn(size(x1));
clf
plot(x1,y1,'k');

% New data
x2 = x1;
y2 = rand(size(x2));


Replot the figure, adding new data in second axis



Here we retrieve the data from the current axes (it would be better if you preserved the axes handle from when you made your first plot, of course). We then plot a new figure using plotyy that contains the old data and the new data.



ax = gca;
h0 = get(ax,'children'); % This is the handle to the plotted line
x1 = get(h0,'xdata'); % Get data for line
y1 = get(h0,'ydata');
cla(ax) % Clear axes
plotyy(ax,x1,y1,x2,y2); % Plot old and new data


plot with left and right axis



Preserve existing axes and plot, add a second axis with the new data



Here we use hold on to prevent the current data from being deleted, then plot the new data with plotyy that also adds a dummy plot to the left axis (a single point 0,0). We then delete this dummy plot.



It turns out that adding this dummy plot still causes the left axis to change. So this code first preserves the location of the tick marks and the limits, and then applies those again after plotting. It also makes the left axis the same color as the line that was already there.



ax = gca;
yl = get(ax,'ylim');
yt = get(ax,'ytick');
h0 = get(ax,'children');
hold on
[ax,h1,h2] = plotyy(ax,0,0,x2,y2);
delete(h1)
set(ax(1),'ycolor',get(h0,'color'),'ylim',yl,'ytick',yt)


plot with left and right axis






share|improve this answer
























  • Please note that: 1) I have several plots on the primary y-axis, 2) I would like to work "normally", i.e., add a suitable legend, etc. Would I be able to add additional plots on the primary/secondary axes on separate code lines (as I am doing right now)?

    – sancho.s
    Jan 3 at 8:36











  • @sancho.s: If you have multiple plots in the first figure, use the second method. You’d have to adapt how you set the color of the left y axis. Maybe you can not change it at all, hopefully it will still be clear which plots belong to which axis. I’d be careful with putting too many plots in one figure. The more lines, the harder it is to see what is going on in the data! I find small multiples a better option for showing different lines together.

    – Cris Luengo
    Jan 3 at 14:08
















1












1








1







Here are two options. I've tested these in MATLAB, but I'm pretty sure it'll work in Octave the same way.



Let's start with some random data plotted normally:



% Initial graph
x1 = linspace(0,1,100);
y1 = randn(size(x1));
clf
plot(x1,y1,'k');

% New data
x2 = x1;
y2 = rand(size(x2));


Replot the figure, adding new data in second axis



Here we retrieve the data from the current axes (it would be better if you preserved the axes handle from when you made your first plot, of course). We then plot a new figure using plotyy that contains the old data and the new data.



ax = gca;
h0 = get(ax,'children'); % This is the handle to the plotted line
x1 = get(h0,'xdata'); % Get data for line
y1 = get(h0,'ydata');
cla(ax) % Clear axes
plotyy(ax,x1,y1,x2,y2); % Plot old and new data


plot with left and right axis



Preserve existing axes and plot, add a second axis with the new data



Here we use hold on to prevent the current data from being deleted, then plot the new data with plotyy that also adds a dummy plot to the left axis (a single point 0,0). We then delete this dummy plot.



It turns out that adding this dummy plot still causes the left axis to change. So this code first preserves the location of the tick marks and the limits, and then applies those again after plotting. It also makes the left axis the same color as the line that was already there.



ax = gca;
yl = get(ax,'ylim');
yt = get(ax,'ytick');
h0 = get(ax,'children');
hold on
[ax,h1,h2] = plotyy(ax,0,0,x2,y2);
delete(h1)
set(ax(1),'ycolor',get(h0,'color'),'ylim',yl,'ytick',yt)


plot with left and right axis






share|improve this answer













Here are two options. I've tested these in MATLAB, but I'm pretty sure it'll work in Octave the same way.



Let's start with some random data plotted normally:



% Initial graph
x1 = linspace(0,1,100);
y1 = randn(size(x1));
clf
plot(x1,y1,'k');

% New data
x2 = x1;
y2 = rand(size(x2));


Replot the figure, adding new data in second axis



Here we retrieve the data from the current axes (it would be better if you preserved the axes handle from when you made your first plot, of course). We then plot a new figure using plotyy that contains the old data and the new data.



ax = gca;
h0 = get(ax,'children'); % This is the handle to the plotted line
x1 = get(h0,'xdata'); % Get data for line
y1 = get(h0,'ydata');
cla(ax) % Clear axes
plotyy(ax,x1,y1,x2,y2); % Plot old and new data


plot with left and right axis



Preserve existing axes and plot, add a second axis with the new data



Here we use hold on to prevent the current data from being deleted, then plot the new data with plotyy that also adds a dummy plot to the left axis (a single point 0,0). We then delete this dummy plot.



It turns out that adding this dummy plot still causes the left axis to change. So this code first preserves the location of the tick marks and the limits, and then applies those again after plotting. It also makes the left axis the same color as the line that was already there.



ax = gca;
yl = get(ax,'ylim');
yt = get(ax,'ytick');
h0 = get(ax,'children');
hold on
[ax,h1,h2] = plotyy(ax,0,0,x2,y2);
delete(h1)
set(ax(1),'ycolor',get(h0,'color'),'ylim',yl,'ytick',yt)


plot with left and right axis







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 5:03









Cris LuengoCris Luengo

22k52253




22k52253













  • Please note that: 1) I have several plots on the primary y-axis, 2) I would like to work "normally", i.e., add a suitable legend, etc. Would I be able to add additional plots on the primary/secondary axes on separate code lines (as I am doing right now)?

    – sancho.s
    Jan 3 at 8:36











  • @sancho.s: If you have multiple plots in the first figure, use the second method. You’d have to adapt how you set the color of the left y axis. Maybe you can not change it at all, hopefully it will still be clear which plots belong to which axis. I’d be careful with putting too many plots in one figure. The more lines, the harder it is to see what is going on in the data! I find small multiples a better option for showing different lines together.

    – Cris Luengo
    Jan 3 at 14:08





















  • Please note that: 1) I have several plots on the primary y-axis, 2) I would like to work "normally", i.e., add a suitable legend, etc. Would I be able to add additional plots on the primary/secondary axes on separate code lines (as I am doing right now)?

    – sancho.s
    Jan 3 at 8:36











  • @sancho.s: If you have multiple plots in the first figure, use the second method. You’d have to adapt how you set the color of the left y axis. Maybe you can not change it at all, hopefully it will still be clear which plots belong to which axis. I’d be careful with putting too many plots in one figure. The more lines, the harder it is to see what is going on in the data! I find small multiples a better option for showing different lines together.

    – Cris Luengo
    Jan 3 at 14:08



















Please note that: 1) I have several plots on the primary y-axis, 2) I would like to work "normally", i.e., add a suitable legend, etc. Would I be able to add additional plots on the primary/secondary axes on separate code lines (as I am doing right now)?

– sancho.s
Jan 3 at 8:36





Please note that: 1) I have several plots on the primary y-axis, 2) I would like to work "normally", i.e., add a suitable legend, etc. Would I be able to add additional plots on the primary/secondary axes on separate code lines (as I am doing right now)?

– sancho.s
Jan 3 at 8:36













@sancho.s: If you have multiple plots in the first figure, use the second method. You’d have to adapt how you set the color of the left y axis. Maybe you can not change it at all, hopefully it will still be clear which plots belong to which axis. I’d be careful with putting too many plots in one figure. The more lines, the harder it is to see what is going on in the data! I find small multiples a better option for showing different lines together.

– Cris Luengo
Jan 3 at 14:08







@sancho.s: If you have multiple plots in the first figure, use the second method. You’d have to adapt how you set the color of the left y axis. Maybe you can not change it at all, hopefully it will still be clear which plots belong to which axis. I’d be careful with putting too many plots in one figure. The more lines, the harder it is to see what is going on in the data! I find small multiples a better option for showing different lines together.

– Cris Luengo
Jan 3 at 14:08






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002845%2foctave-add-secondary-y-axis-to-existing-plot%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter