How to create a multiple y-axes plot in Scilab

When dealing with data we often need to compare different sets/series between themselves. For this we need to plot several y-axes function of the same, common x-axis. Scilab is very versatile at plotting multiple y-axes on the same graphical window.

Scilab video tutorial

This tutorial will teach you how to plot 2 or more y-axis plot in the same graphical window using Scilab.

In this example we are going to plot 3 function y1(x), y2(2) and y3(x) function of x, overlapping the curves, each with its own y-axis. The functions are defined as:

\[ \begin{split}
y_{1}(x) &= \sin(x)\\
y_{2}(x) &= e^{\frac{x}{6}}\cdot \left [y_{1}(x)+2 \right ]\\
y_{3}(x) &= 1+x^{2}
\end{split} \]

Step 1. Define the x-axis and the functions.

// Preparing data
clear()
clf()
x=linspace(1,30,200);
y1=sin(x);
y2=exp(x/6).*(y1+2);
y3=1+x.^2;

Before defining the data we are erasing all the variables in the Scilab Variable Browser with the function clear(). Also, in order to make sure that we don’t miss any warning or error message, we are clearing the Scilab console with the function clf(). The x-axis contains 200 points, between 1 and 30, and is defined with the function linspace(). After the x-axis definition, the y functions are defined: y1, y2 and y3. After running these Scilab instructions, an empty graphical window should be generated and the x and y1, y2 and y3 variables should appear in the Scilab Variable Browser.

Step 2. Plot the function y1(x).

// Axis y1
c=color("slategray");
plot2d(x,y1,style=c)
h1=gca();
h1.font_color=c;
h1.children(1).children(1).thickness = 2;
xlabel("$x$","FontSize",3)
ylabel("$y_{1}(x)=\sin(x)$","FontSize",3)
title("x-engineer.org","color","blue")

First we create a colour variable c, which we are going to use as a setting for the axes and polyline (function curve). The function y1(x)  is plotted with the instruction plot2d(). With gca() we read the current axes parameters and we assign them to the variable h1 for editing. Next instructions are setting the colour of the axis font and the thickness of the line. After, we use xlabel() and ylabel() to set the axes labels and title() for a title. Note that we used Latex notations for the y-axis. For more information on Scilab plots and Latex please read the article How to add Latex formatted text in a Scilab plot.

After running the Scilab instructions in a script file (*.sce) or in the Scilab Console, we get the following graphical window.

Function y1(x) Scilab plot

Image: Function y1(x) Scilab plot

Step 3. Plot the function y2(x).

// Axis y2
c=color("red");
h2=newaxes();
h2.font_color=c;
plot2d(x,y2,style=c)
h2.filled="off";
h2.axes_visible(1)="off";
h2.y_location="right";
h2.children(1).children(1).thickness=2;
ylabel("$y_{2}(x)=e^{\frac{x}{6}}\cdot \left [y_{1}(x)+2 \right ]$","FontSize",3,"color",c)

For the second y axis we are going to use the color red for font and polyline. Also, using the axes handle variable h2, we are locating it on the right side of the plot. We also make the background transparent (off) to allow the previous y-axis to be visible and hide the x-axis (off) to avoid overlapping with the previous one.

After running the Scilab instructions in a script file (*.sce) or in the Scilab Console, we get the following graphical window.

Function y1(x) and y2(x) Scilab plot

Image: Function y1(x) and y2(x) Scilab plot

Step 4. Plot the function y3(x).

// Axis y3
c=color("purple");
h3=newaxes();
h3.font_color=c;
plot2d(x,y3,style=c)
h3.children(1).children(1).thickness=2;
h3.filled="off";
h3.axes_visible(1)="off";
h3.axes_reverse(2)="on";
h3.y_location="middle";
h3.log_flags="nln";
ylabel("$y_{3}(x)=1+x^{2}$","FontSize",3,"color",c)

For the third y-axis we use the colour purple. We set it up in the same way as second y axis, with a few exceptions. The axis position will be in the middle of the plot, the values will be reversed (on), minimum at the top and maximum at the bottom, and the scale (numbers) will be displayed as logarithmic (nln).

After running the Scilab instructions in a script file (*.sce) or in the Scilab Console, we get the following graphical window.

Function y1(x), y2(x) and y3(x) Scilab plot

Image: Function y1(x), y2(x) and y3(x) Scilab plot

To get the final plot in one go, copy all the Scilab instructions defined in the steps above in a single Scilab script file (*.sce) and run it.

Scilab is very versatile for multiple y-axes plots. To get a handle of it, you can try another example with different functions and different settings.

Leave a Reply

Ad Blocker Detected

Dear user, Our website provides free and high quality content by displaying ads to our visitors. Please support us by disabling your Ad blocker for our site. Thank you!

Refresh