Discussion:
[Scilab-users] Plots on second Y axis
Samuel Enibe
2013-02-08 14:30:09 UTC
Permalink
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 Enibe
University of Nigeria, Nsukka, Nigeria
Tel: +2348063646798
Email: ***@unn.edu.ng
***@yahoo.com
s***@free.fr
2013-02-08 16:14:41 UTC
Permalink
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
Samuel Enibe
2013-02-23 21:19:31 UTC
Permalink
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
Post by s***@free.fr
Hello,
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
http://lists.scilab.org/mailman/listinfo/users
--
Stefan Du Rietz
2013-02-23 21:25:22 UTC
Permalink
http://help.scilab.org/docs/5.4.0/en_US/drawaxis.html

/Stefan
Post by Samuel Enibe
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
Hello,
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
http://lists.scilab.org/mailman/listinfo/users
--
_______________________________________________
users mailing list
http://lists.scilab.org/mailman/listinfo/users
Samuel Enibe
2013-02-23 22:02:15 UTC
Permalink
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
http://help.scilab.org/docs/5.**4.0/en_US/drawaxis.html<http://help.scilab.org/docs/5.4.0/en_US/drawaxis.html>
/Stefan
Post by Samuel Enibe
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
Hello,
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<http://help.scilab.org/docs/5.4.0/en_US/newaxes.html>
Regards
Samuel
______________________________**_________________
users mailing list
http://lists.scilab.org/**mailman/listinfo/users<http://lists.scilab.org/mailman/listinfo/users>
--
______________________________**_________________
users mailing list
http://lists.scilab.org/**mailman/listinfo/users<http://lists.scilab.org/mailman/listinfo/users>
______________________________**_________________
users mailing list
http://lists.scilab.org/**mailman/listinfo/users<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: ***@unn.edu.ng
***@yahoo.com
Stefan Du Rietz
2013-02-23 22:46:01 UTC
Permalink
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
Post by Samuel Enibe
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
http://help.scilab.org/docs/5.__4.0/en_US/drawaxis.html
<http://help.scilab.org/docs/5.4.0/en_US/drawaxis.html>
/Stefan
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
Hello,
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
<http://help.scilab.org/docs/5.4.0/en_US/newaxes.html>
Regards
Samuel
_________________________________________________
users mailing list
http://lists.scilab.org/__mailman/listinfo/users
<http://lists.scilab.org/mailman/listinfo/users>
--
_________________________________________________
users mailing list
http://lists.scilab.org/__mailman/listinfo/users
<http://lists.scilab.org/mailman/listinfo/users>
_________________________________________________
users mailing list
http://lists.scilab.org/__mailman/listinfo/users
<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
_______________________________________________
users mailing list
http://lists.scilab.org/mailman/listinfo/users
Dang, Christophe
2013-02-25 09:09:03 UTC
Permalink
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.
bjarne12
2016-08-25 16:53:03 UTC
Permalink
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




--
View this message in context: http://mailinglists.scilab.org/Scilab-users-Plots-on-second-Y-axis-tp4025895p4034490.html
Sent from the Scilab users - Mailing Lists Archives mailing list archive at Nabble.com.
mathias_magdowski
2017-03-28 07:51:46 UTC
Permalink
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<y2axismax-y2axismin
// left y axis hat weniger optimum rounded divider als right y
axis
if addupper==1 &amp; y1axismax~=-1
// move the upper limit of the left y axis up
y1axismax=y1axismax+1;
end
if addupper==-1 &amp; y1axismin~=1
// move the lower limit of the left y axis down
y1axismin=y1axismin+1;
end
// toggle variable
addupper=adduper*(-1);
end
// rescale axes
// lower limit of the left y axis
a1.data_bounds(1,2)=y1axismin*division1;
// lower limit of the left y axis
a1.data_bounds(2,2)=y1axismax*division1;
// lower limit of the left y axis
a2.data_bounds(1,2)=y2axismin*division2;
// lower limit of the left y axis
a2.data_bounds(2,2)=y2axismax*division2;
else
// at least one of the axes goes over zero, or starts or ends at
zero
// find joint lower limit of both axes -> 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.
p***@free.fr
2017-03-28 08:21:23 UTC
Permalink
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
Post by mathias_magdowski
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".
// Function for plotting a diagram with two y axes
// x - x values -> vector
// y1 - y values for the for the left y axis -> vector
// y2 - y values for the right y axis -> vector
// a1, a2 - axes -> handle
function [a1,a2]=plotyy(x,y1,y2)
// example for a diagram with two y axes
https://commons.wikimedia.org/wiki/File:Trace_ln_sqrt_1_2_deux_echelles_scilab.svg
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<y2axismax-y2axismin
// left y axis hat weniger optimum rounded divider als right y
axis
if addupper==1 &amp; y1axismax~=-1
// move the upper limit of the left y axis up
y1axismax=y1axismax+1;
end
if addupper==-1 &amp; y1axismin~=1
// move the lower limit of the left y axis down
y1axismin=y1axismin+1;
end
// toggle variable
addupper=adduper*(-1);
end
// rescale axes
// lower limit of the left y axis
a1.data_bounds(1,2)=y1axismin*division1;
// lower limit of the left y axis
a1.data_bounds(2,2)=y1axismax*division1;
// lower limit of the left y axis
a2.data_bounds(1,2)=y2axismin*division2;
// lower limit of the left y axis
a2.data_bounds(2,2)=y2axismax*division2;
else
// at least one of the axes goes over zero, or starts or ends at
zero
// find joint lower limit of both axes -> 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
// axismin: minimum of the data to display -> scalar
// axismax: maximum of the data to display -> scalar
// 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
--
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
http://lists.scilab.org/mailman/listinfo/users
JLan
2017-03-29 11:31:00 UTC
Permalink
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



--
View this message in context: http://mailinglists.scilab.org/Scilab-users-Plots-on-second-Y-axis-tp4025895p4036077.html
Sent from the Scilab users - Mailing Lists Archives mailing list archive at Nabble.com.
mathias_magdowski
2017-03-29 11:54:03 UTC
Permalink
Post by JLan
Using the [a1 a2] handles afterwards was not quite successful for me.
This worked fine for me:
[a1,a2]=plotyy(t,uL2,i3)
legend(a1,"uL2",2);
legend(a2,"i3",1);
ylabel(a1,'Spannung, u in V','fontsize',3);
xlabel(a2,'Zeit, t in s','fontsize',3);
ylabel(a1,'Strom, i in A','fontsize',3);

Also see my example at Twitter
<https://twitter.com/LehrstuhlEMV/status/846635089882701824> .
Post by JLan
What about adding some optional labels in the function?
This is for sure a solution, but I try to avoid having a function with an
unreasonable high number of input arguments if there are more flexible
solutions.



--
View this message in context: http://mailinglists.scilab.org/Scilab-users-Plots-on-second-Y-axis-tp4025895p4036078.html
Sent from the Scilab users - Mailing Lists Archives mailing list archive at Nabble.com.
Dang Ngoc Chan, Christophe
2017-03-28 08:45:07 UTC
Permalink
Hello,

Thanks for sharing.
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.
Continue reading on narkive:
Loading...