Scilab plot tutorial – simple example (1)

Data and signals are very easy to analyze in Scilab. The default Scilab package comes with a variety of embedded function for plots. The most common and easy to use function is the 2-D line plot, which is called using the embedded Sclab function plot().

Scilab plot function syntax

Image: Scilab plot function syntax

where:

x – vector or matrix containing real numbers;
y – vector or matrix containing real numbers;
LineSpecifications – string variable used to specify line properties like: style, color and marker type;
GlobalSpecifications – string variables used to specify figure properties

The arguments specified between < and > are optional. Even if they are not specified, the plot() function will return a graphic window.

The x argument, if not specified, will be replaced with a vector 1:N, where N is the length of the y parameter.

How to install Scilab

The LineSpecifications are specific to each line plot, while the GlobalSpecifications are specific to the graphic window and common to all line plots within the same axes.

Let’s plot the sine function for all the angles between 0 and , with an increment of 0.01. To create the line plot we need to enter at the Scilab console:

-->plot(sin([0:0.01:2*%pi]))

which will return:

Scilab line plot with only one argument

Image: Scilab line plot with only one argument

The default line color is blue. If the x parameter is not specified, the values of the sine function are plotted against the index, which is a vector from 1 to N (number of elements of y parameter).

Let’s define a variable x as the angle from 0 to , and two functions y and z as:

\[y=sin(x),\text{     }z=cos(x)\]

Enter in the Scilab console:

-->x = [0:0.01:2*%pi];
-->y = sin(x);
-->z = cos(x);

First we are going to plot y function of x, with an increased line width:

-->plot(x,y,'LineWidth',3)

will return:

Scilab line plot with increased line width

Image: Scilab line plot with increased line width

The line thickness/width is specified with LineWidth option. The number 3 specifies the thickness/width of the line, higher values meaning thicker lines.

Since we have specified the x parameter, on the horizontal axis we’ll have the values of the angle and not its index.

If we enter the plot() command again, we are going to keep the same graphic figure for another line plot:

-->plot(x,z,'r','LineWidth',3)

For the second line plot we have specified the color with the string r (red).

Multiple line plot on the same axes in Scilab

Image: Multiple line plot on the same axes in Scilab

With every plot() function we add, new line plots are created on the same graphic window.

To have a complete plot we need to add a grid, labels for both axes, a title and a legend. For this we need to enter at the Scilab console the following instructions:

-->xgrid
-->xlabel('x')
-->ylabel('sin(x), cos(x)')
-->title('Plot of sin(x) and cos(x)')
-->legend('sin(x)','cos(x)',3)

The grid is displayed with the function xgrid(). The default setting for the grid are: black color and dashed lines.

The axes labels are added with xlabel() and ylabel() functions. The arguments are string variables representing the text to be displayed.

For the title of the plot we use the title() function. It has the same syntax as the labels, and displays the string argument as a centered text above the axes.

If we have multiple line plots on the same axes, we need to add also a legend, in order to distinguish between the lines. The arguments of the legend() function are string variables for each line plot, and a number which represents the position where the legend is going to be displayed on the graphic window.

After entering the above Scilab instruction at the console, we’ll get an update of the plot:

Scilab line plot example

Image: Scilab line plot example

In order to explore different settings, we can add all the above instructions into a Scilab script file (*.sce).

x = [0:0.01:2*%pi];
y = sin(x);
z = cos(x);
plot(x,y,'LineWidth',3)
plot(x,z,'r','LineWidth',3)
xgrid
xlabel('x')
ylabel('sin(x), cos(x)')
title('Plot of sin(x) and cos(x)')
legend('sin(x)','cos(x)',3)

This tutorial is a quick introduction into the 2-D line plot function. In the following articles we are going to explore more advanced features and properties.

For any questions or observations regarding this article please use the comment form below.

Don’t forget to Like, Share and Subscribe!

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