# [Scilab-users] Plots on second Y axis

15 messages
Open this post in threaded view
|

## [Scilab-users] Plots on second Y axis

 Dear Sir,Could you please let me know the SCILAB command to use in making a plot on the SECOND (right) Y axis.Thank you very much.-- Samuel Ogbonna EnibeUniversity of Nigeria, Nsukka, Nigeria Tel: +2348063646798Email: [hidden email]     [hidden email] _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Plots on second Y axis

 Hello, according to your needs, you may do either: plot2d() ax = gca(); ax.y_location = 'right'; or demo_gui() // then Graphics => 2D & 3D => plotyy | plotyyy // and look at the code, using help newaxes // http://help.scilab.org/docs/5.4.0/en_US/newaxes.htmlRegards Samuel _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Plots on second Y axis

 Thanks, Spougeon.This has partly solved the problem. Suppose I have two plots on the same graph, one is to use the left Y-axis, while the second is to use the right Y-axis. How should I handle it with gca() or any other command? Samuel Ogbonna EnibeUniversity of Nigeria, Nsukka, NigeriaOn Fri, Feb 8, 2013 at 8:14 AM, wrote: Hello, according to your needs, you may do either: plot2d() ax = gca(); ax.y_location = 'right'; or demo_gui() // then Graphics => 2D & 3D => plotyy | plotyyy // and look at the code, using help newaxes // http://help.scilab.org/docs/5.4.0/en_US/newaxes.html Regards Samuel _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users -- _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Plots on second Y axis

 http://help.scilab.org/docs/5.4.0/en_US/drawaxis.html/Stefan On 2013-02-23 22:19, Samuel Enibe wrote: > Thanks, Spougeon. > This has partly solved the problem. Suppose I have two plots on the same > graph, one is to use the left Y-axis, while the second is to use the > right Y-axis. How should I handle it with gca() or any other command? > > Samuel Ogbonna Enibe > University of Nigeria, Nsukka, Nigeria > > > On Fri, Feb 8, 2013 at 8:14 AM, <[hidden email] > > wrote: > >     Hello, > >     according to your needs, you may do either: > >     plot2d() >     ax = gca(); >     ax.y_location = 'right'; > >     or > >     demo_gui() // then Graphics => 2D & 3D => plotyy | plotyyy >     // and look at the code, using >     help newaxes // http://help.scilab.org/docs/5.4.0/en_US/newaxes.html> >     Regards >     Samuel >     _______________________________________________ >     users mailing list >     [hidden email] >     http://lists.scilab.org/mailman/listinfo/users> > > > > -- > > > > _______________________________________________ > users mailing list > [hidden email] > http://lists.scilab.org/mailman/listinfo/users> _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Plots on second Y axis

 Thanks, Stephan.Can you help me a little further.Suppose I have the datax = [0:0.1:1]y = sin(x)plot(x, y)xtitle("My Title", "Value of x", "Sin (x)") y2 = 2 * sin(x)//y2label = "2 * sin(x)"How do I plot y2 so that it uses the right Y-axis with its y2label properly specified On Sat, Feb 23, 2013 at 1:25 PM, Stefan Du Rietz wrote: http://help.scilab.org/docs/5.4.0/en_US/drawaxis.html /Stefan On 2013-02-23 22:19, Samuel Enibe wrote: Thanks, Spougeon. This has partly solved the problem. Suppose I have two plots on the same graph, one is to use the left Y-axis, while the second is to use the right Y-axis. How should I handle it with gca() or any other command? Samuel Ogbonna Enibe University of Nigeria, Nsukka, Nigeria On Fri, Feb 8, 2013 at 8:14 AM, <[hidden email] > wrote:     Hello,     according to your needs, you may do either:     plot2d()     ax = gca();     ax.y_location = 'right';     or     demo_gui() // then Graphics => 2D & 3D => plotyy | plotyyy     // and look at the code, using     help newaxes // http://help.scilab.org/docs/5.4.0/en_US/newaxes.html     Regards     Samuel     _______________________________________________     users mailing list     [hidden email]     http://lists.scilab.org/mailman/listinfo/users -- _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users -- Samuel Ogbonna EnibeBEng (Nig), MSc (Reading, England), PhD (Nig)Professor of Mechanical EngineeringDirector, National Centre for Equipment  Maintenance & Development University of Nigeria, Nsukka, NigeriaTel: +2348063646798Email: [hidden email]     [hidden email] _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Plots on second Y axis

 Hi Samuel. you can have different y_tick.labels on the left and right y axes and multiply the y-data of the line with the right y axis with a factor. In your case the left y axis could have y_tick.labels from -1 to +1 and the right y axis from -2 to +2. But because both lines are drawn in the same axes (with the same y_tick.locations from -1 to +1), you have to multiply 2*sin(x) by 0.5 to make it correspond to the right y axis labels that are multiplied by 2 (1/0.5). However, in your case both lines will look be the same. But if you change the second to 2*cos(x) and give it another color or line style (which is somewhere noted to correspond to the right y axis) it could work. /Stefan On 2013-02-23 23:02, Samuel Enibe wrote: > Thanks, Stephan. > Can you help me a little further. > Suppose I have the data > x = [0:0.1:1] > y = sin(x) > plot(x, y) > xtitle("My Title", "Value of x", "Sin (x)") > > y2 = 2 * sin(x) > //y2label = "2 * sin(x)" > > How do I plot y2 so that it uses the right Y-axis with its y2label > properly specified > > On Sat, Feb 23, 2013 at 1:25 PM, Stefan Du Rietz <[hidden email] > > wrote: > >     http://help.scilab.org/docs/5.__4.0/en_US/drawaxis.html>     > >     /Stefan > > >     On 2013-02-23 22:19, Samuel Enibe wrote: > >         Thanks, Spougeon. >         This has partly solved the problem. Suppose I have two plots on >         the same >         graph, one is to use the left Y-axis, while the second is to use the >         right Y-axis. How should I handle it with gca() or any other >         command? > >         Samuel Ogbonna Enibe >         University of Nigeria, Nsukka, Nigeria > > >         On Fri, Feb 8, 2013 at 8:14 AM, <[hidden email] >         >         >> wrote: > >              Hello, > >              according to your needs, you may do either: > >              plot2d() >              ax = gca(); >              ax.y_location = 'right'; > >              or > >              demo_gui() // then Graphics => 2D & 3D => plotyy | plotyyy >              // and look at the code, using >              help newaxes // >         http://help.scilab.org/docs/5.__4.0/en_US/newaxes.html>         > >              Regards >              Samuel >              _________________________________________________ >              users mailing list >         [hidden email] >         __> >         http://lists.scilab.org/__mailman/listinfo/users>         > > > > >         -- > > > > >         _________________________________________________ >         users mailing list >         [hidden email] >         http://lists.scilab.org/__mailman/listinfo/users>         > > >     _________________________________________________ >     users mailing list >     [hidden email] >     http://lists.scilab.org/__mailman/listinfo/users>     > > > > > -- > Samuel Ogbonna Enibe > BEng (Nig), MSc (Reading, England), PhD (Nig) > Professor of Mechanical Engineering > Director, National Centre for Equipment  Maintenance & Development > University of Nigeria, Nsukka, Nigeria > Tel: +2348063646798 > Email: [hidden email] > [hidden email] > > > _______________________________________________ > users mailing list > [hidden email] > http://lists.scilab.org/mailman/listinfo/users> _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Plots on second Y axis

 In reply to this post by Samuel Enibe Hello, > Suppose I have two plots on the same graph, > one is to use the left Y-axis, > while the second is to use the right Y-axis. > How should I handle it with gca() or any other command? You might have a look at http://commons.wikimedia.org/wiki/File:Trace_ln_sqrt_1_2_deux_echelles_scilab.svg(picture + source code) This link was already posted Feb 11, 2013; 11:29am, see http://mailinglists.scilab.org/Scilab-users-Plot-command-tt4025898.html-- Christophe Dang Ngoc Chan Mechanical calculation engineer This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users -- Christophe Dang Ngoc Chan Mechanical calculation engineer
Open this post in threaded view
|

## Re: Plots on second Y axis

 This post has NOT been accepted by the mailing list yet. I use https://commons.wikimedia.org/wiki/File:Trace_ln_sqrt_1_2_deux_echelles_scilab.svg to plot with two y-axes, one on left, one on right. My questions is how to better handle the legends? That post creates 2 separate legends, one for each plot, and places them at different corners to avoid them to overlap. It does not look good. What if I have 5 legends? there is not place to put them without overlap? How can I combine all legends into one?
Open this post in threaded view
|

## Re: Plots on second Y axis

 This is one way which is based on the description in https://commons.wikimedia.org/wiki/File:Trace_ln_sqrt_1_2_deux_echelles_scilab.svg, but with some changes. Draw the second graph first (the one for the right y-axis). Then in the second plot, draw the first graph for the left axis and the same graph again in the same plot, but with the visual characteristics of the second graph. So if the first graph is a blue line and the second is made of red dots, first draw the first graph as a blue line and then again as red dots. Then set the red graph as invisible. This way the legend command will add both graphs. I reckon it will work for more graphs as well, but I haven't tested it. Here is some example code: a1 = newaxes(); //make right y-axis a1.filled = 'off'; a1.axes_visible(1) = 'off'; a1.tight_limits = 'on'; a1.y_location = 'right'; a1.auto_ticks = ['off', 'on', 'on']; //remove ticks on first x-axis a1.x_ticks.locations = []; plot(x, y2, lin2); //plot graph 2 ylabel('add label', 'fontsize', 3); //add label to right y-axis a2 = newaxes(); //make left y-axis a2.filled = 'off'; a2.tight_limits = 'on'; plot(x, y1, lin1); //plot graph 1 plot(x, y1, lin2); //plot invisible graph 1 with colours of graph 2 (so a legend can be added) y = gca(); LinE = y.children.children(1); LinE.visible = 'off'; xlabel('add label', 'fontsize', 3); //add label to x-axis ylabel('add label, 'fontsize', 3); //add label to left y-axis
Open this post in threaded view
|

## Re: Plots on second Y axis

 I have extended the given example in a way that the left and right y axis get some kind of intelligent equal scaling, so that a joined grid for both axes would fit. If one of the axes includes the value of y = 0, both axes will also share the same "base line". Here is the function: // Function for plotting a diagram with two y axes // Input: //  x  - x values -> vector //  y1 - y values for the  for the left y axis -> vector //  y2 - y values for the right y axis -> vector // Output: //  a1, a2         - axes -> handle function [a1,a2]=plotyy(x,y1,y2)     // example for a diagram with two y axes     // see: https://commons.wikimedia.org/wiki/File:Trace_ln_sqrt_1_2_deux_echelles_scilab.svg    // and:   http://mailinglists.scilab.org/Scilab-users-Plots-on-second-Y-axis-td4025895.html    // generation of the right y axis     a1=newaxes();     a1.tight_limits=["on","off","off"];     // plot of the first function     plot(x,y1);     // generation of the right y axis     a2=newaxes();     // no background for this plot     a2.filled="off";     // hide second x axis     a2.axes_visible(1)="off";     a2.y_location = "right";     a2.tight_limits = "on";     // plot of the second function     plot(x,y2,"r");     // set font size     a1.font_size=3;     a2.font_size=3;     // delete tick marks of the first x axis     a1.x_ticks=tlist(["ticks","locations","labels"],[],[])     // add a grid     a1.grid=[1,1];     a2.grid=[1,1];     // end of plotting     // algorithm to equally scale the axes     // minimum of the first function -> scalar     y1min=min(y1);     // maximum of the first function -> scalar     y1max=max(y1);     // minimum of the second function -> scalar     y2min=min(y2);     // maximum of the second function -> scalar     y2max=max(y2);     // divider for the first function -> scalar     division1=find_division(y1min,y1max);     // divider for the second function -> scalar     division2=find_division(y2min,y2max);     // lower axis left y axis the first function (normalized) -> scalar     y1axismin=floor(y1min/division1)     // upper axis left y axis the first function (normalized) -> scalar     y1axismax=ceil(y1max/division1)     // lower axis left y axis the second function (normalized) -> scalar     y2axismin=floor(y2min/division2)     // upper axis left y axis the second function (normalized) -> scalar     y2axismax=ceil(y2max/division2)     // distinction of cases     if y1axismin*y1axismax>0 & y2axismin*y2axismax>0 then         // both axes don't go over the value of zero         // preset variable         addupper=1;         // start a loop, until both axes have the same number of dividers         while y1axismax-y1axismin>y2axismax-y2axismin             // left y axis has more dividers than the right y axis             if addupper==1 & y2axismax~=-1                 // move the upper limit of the right y axis up                 y2axismax=y2axismax+1;             end             if addupper==-1 & y2axismin~=1                 // move the lower limit of the right y axis down                 y2axismin=y2axismin+1;             end             // toggle variable             addupper=adduper*(-1);         end         // preset variable         addupper=1;         // start a loop, until both axes have the same number of dividers         while y1axismax-y1axismin scalar         ymin=min(y1axismin,y2axismin);         // find joint upper limit of both axes -> scalar         ymax=max(y1axismax,y2axismax);         // rescale axes         // lower limit of the left y axis         a1.data_bounds(1,2)=ymin*division1;         // lower limit of the left y axis         a1.data_bounds(2,2)=ymax*division1;         // lower limit of the left y axis         a2.data_bounds(1,2)=ymin*division2;         // lower limit of the left y axis         a2.data_bounds(2,2)=ymax*division2;     end endfunction // Function to find a good division for the grid of a diagram // Input: //  axismin: minimum of the data to display -> scalar //  axismax: maximum of the data to display -> scalar // Output: //  division: optimum rounded divider to divide the axis into 5 to 10 parts function division=find_division(axismin,axismax)     // distance between maximum and minimum -> scalar     distance=axismax-axismin;     // preset exponent -> scalar     exponent=0;     // loop, until the distance is between 1 and 10     while 1         if distance>10 then             // decrease distance -> scalar             distance=distance/10;             // increase exponent -> scalar             exponent=exponent+1;         elseif distance<1 then             // increase distance -> scalar             distance=distance*10;             // decrease exponent -> scalar             exponent=exponent-1;         else             // exit loop             break;         end     end     // distinction of cases, so that there will be 5 to 10 dividers at the end     if distance==1 then         // optimum rounded divider -> scalar         division=0.1;     elseif distance<=2 then         // optimum rounded divider -> scalar         division=0.2;     elseif distance<=5 then         // optimum rounded divider -> scalar         division=0.5;     else         // optimum rounded divider -> scalar         division=1;     end     // adjust the scaling -> scalar     division=division*10^exponent; endfunction
Open this post in threaded view
|

## Re: Plots on second Y axis

 Thanks for sharing it.   I've a suggestion I commonly use for such plots (I'm speaking in a general way): the color of the dedicated ordinate axis is the same than the curve in order to immediatly know what are the units and where to look to ... ... just my point of view   Paul     Le 2017-03-28 09:51, mathias_magdowski a écrit : I have extended the given example in a way that the left and right y axis get some kind of intelligent equal scaling, so that a joined grid for both axes would fit. If one of the axes includes the value of y = 0, both axes will also share the same "base line". Here is the function: // Function for plotting a diagram with two y axes // Input: //  x  - x values -> vector //  y1 - y values for the  for the left y axis -> vector //  y2 - y values for the right y axis -> vector // Output: //  a1, a2         - axes -> handle function [a1,a2]=plotyy(x,y1,y2)     // example for a diagram with two y axes     // see: https://commons.wikimedia.org/wiki/File:Trace_ln_sqrt_1_2_deux_echelles_scilab.svg     // and:   http://mailinglists.scilab.org/Scilab-users-Plots-on-second-Y-axis-td4025895.html     // generation of the right y axis     a1=newaxes();     a1.tight_limits=["on","off","off"];     // plot of the first function     plot(x,y1);     // generation of the right y axis     a2=newaxes();     // no background for this plot     a2.filled="off";     // hide second x axis     a2.axes_visible(1)="off";     a2.y_location = "right";     a2.tight_limits = "on";     // plot of the second function     plot(x,y2,"r");     // set font size     a1.font_size=3;     a2.font_size=3;     // delete tick marks of the first x axis     a1.x_ticks=tlist(["ticks","locations","labels"],[],[])     // add a grid     a1.grid=[1,1];     a2.grid=[1,1];     // end of plotting     // algorithm to equally scale the axes     // minimum of the first function -> scalar     y1min=min(y1);     // maximum of the first function -> scalar     y1max=max(y1);     // minimum of the second function -> scalar     y2min=min(y2);     // maximum of the second function -> scalar     y2max=max(y2);     // divider for the first function -> scalar     division1=find_division(y1min,y1max);     // divider for the second function -> scalar     division2=find_division(y2min,y2max);     // lower axis left y axis the first function (normalized) -> scalar     y1axismin=floor(y1min/division1)     // upper axis left y axis the first function (normalized) -> scalar     y1axismax=ceil(y1max/division1)     // lower axis left y axis the second function (normalized) -> scalar     y2axismin=floor(y2min/division2)     // upper axis left y axis the second function (normalized) -> scalar     y2axismax=ceil(y2max/division2)     // distinction of cases     if y1axismin*y1axismax>0 & y2axismin*y2axismax>0 then         // both axes don't go over the value of zero         // preset variable         addupper=1;         // start a loop, until both axes have the same number of dividers         while y1axismax-y1axismin>y2axismax-y2axismin             // left y axis has more dividers than the right y axis             if addupper==1 & y2axismax~=-1                 // move the upper limit of the right y axis up                 y2axismax=y2axismax+1;             end             if addupper==-1 & y2axismin~=1                 // move the lower limit of the right y axis down                 y2axismin=y2axismin+1;             end             // toggle variable             addupper=adduper*(-1);         end         // preset variable         addupper=1;         // start a loop, until both axes have the same number of dividers         while y1axismax-y1axismin scalar         ymin=min(y1axismin,y2axismin);         // find joint upper limit of both axes -> scalar         ymax=max(y1axismax,y2axismax);         // rescale axes         // lower limit of the left y axis         a1.data_bounds(1,2)=ymin*division1;         // lower limit of the left y axis         a1.data_bounds(2,2)=ymax*division1;         // lower limit of the left y axis         a2.data_bounds(1,2)=ymin*division2;         // lower limit of the left y axis         a2.data_bounds(2,2)=ymax*division2;     end endfunction // Function to find a good division for the grid of a diagram // Input: //  axismin: minimum of the data to display -> scalar //  axismax: maximum of the data to display -> scalar // Output: //  division: optimum rounded divider to divide the axis into 5 to 10 parts function division=find_division(axismin,axismax)     // distance between maximum and minimum -> scalar     distance=axismax-axismin;     // preset exponent -> scalar     exponent=0;     // loop, until the distance is between 1 and 10     while 1         if distance>10 then             // decrease distance -> scalar             distance=distance/10;             // increase exponent -> scalar             exponent=exponent+1;         elseif distance<1 then             // increase distance -> scalar             distance=distance*10;             // decrease exponent -> scalar             exponent=exponent-1;         else             // exit loop             break;         end     end     // distinction of cases, so that there will be 5 to 10 dividers at the end     if distance==1 then         // optimum rounded divider -> scalar         division=0.1;     elseif distance<=2 then         // optimum rounded divider -> scalar         division=0.2;     elseif distance<=5 then         // optimum rounded divider -> scalar         division=0.5;     else         // optimum rounded divider -> scalar         division=1;     end     // adjust the scaling -> scalar     division=division*10^exponent; endfunction -- View this message in context: http://mailinglists.scilab.org/Scilab-users-Plots-on-second-Y-axis-tp4025895p4036055.html Sent from the Scilab users - Mailing Lists Archives mailing list archive at Nabble.com. _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Plots on second Y axis

 In reply to this post by Samuel GOUGEON Hello, Thanks for sharing. Just a general comment: > De : mathias_magdowski > Envoyé : mardi 28 mars 2017 09:52 > >   while 1 >        if distance>10 then > [...] >        elseif distance<1 then >[... ] >        else >            // exit loop >            break; >        end > end I can admit that break() can spare a lot of unnecessary code lines. Nevertheless, I think it should be avoided when unnecessary and would recommend something like: // ********** testvalue = %t; while testvalue     if distance>10 then         [...]     elseif distance<1 then         [...]     else         testvalue = %f;     end end // ********** Regards -- Christophe Dang Ngoc Chan Mechanical calculation engineer This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error), please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users -- Christophe Dang Ngoc Chan Mechanical calculation engineer
Open this post in threaded view
|

## Re: Plots on second Y axis

 In reply to this post by mathias_magdowski mathias_magdowski wrote I have extended the given example.. Good, although it can get a bit messed up when using the "reframe to contents" button. Using the [a1 a2] handles afterwards was not quite successful for me.  What about adding some optional  labels  in the function? function [a1,a2]=plotyyl(x,y1,y2,t,x_l,y1_l,y2_l)     if ~exists("t") t=""; end     if ~exists("x_l") x_l="X"; end     if ~exists("y1_l") y1_l="Y1"; end     if ~exists("y2_l") y2_l="Y2"; end     .... .... // plot of the first function     plot(x,y1);     gca().title.text=t;     gca().y_label.font_foreground=2;     gca().y_label.text=y1_l; .... ....    // plot of the second function     plot(x,y2,"r");     gca().y_label.font_foreground=5;     gca().y_label.text=y2_l;     gca().x_label.text=x_l; .... J