Sei sulla pagina 1di 17

www.openeering.

com
powered by
PLOTTING IN SCILAB
In this Scilab tutorial we make a collection of the most important plots arising in
scientific and engineering applications and we show how straightforward it is to
create smart charts with Scilab.
Level


This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.



Plotting in Scilab www.openeering.com page 2/17
Step 1: The purpose of this tutorial
The purpose of this Scilab tutorial is to provide a collection of plotting
examples that can be used in Scilab to show data.

Here, on the right, we report some definitions used when plotting data on
figures.









Step 2: Roadmap
Examples refer to 1D, 2D, vector fields and 3D problems. In this Scilab
tutorial, the reader will discover some basics commands on how to add
annotations in LaTex, manage axis, change plotting properties such as
colors, grids, marker size, font size, and so on.

This tutorial can be considered as a quick kick-start guide for engineers
and scientists for data visualization in Scilab.




Descriptions Steps
One dimensional plot 1-7
Bi-dimensional plot 8-12
Tri-dimensional plot 13-14
Animated plot 15



Plotting in Scilab www.openeering.com page 3/17
Step 1: Basic plot with LaTex annotations
Here, we plot the function:
y =
1
1 +x
2

on the interval |-S,S].


/ / Cl ose al l opened f i gur es and cl ear wor kspace
xdel ( wi nsi d( ) ) ;
cl ear ;
cl c;

/ / Fi gur e #1: Basi c pl ot wi t h LaTex annot at i ons
/ / - - - - - - - - - -
/ / Dat a
x = l i nspace( - 5, 5, 51) ;
y = 1 . / ( 1+x. ^2) ;

/ / Pl ot
scf ( 1) ;
cl f ( 1) ;
pl ot ( x, y, ' o- b' ) ;
xl abel ( " $- 5\ l e x\ l e 5$" , " f ont si ze" , 4, " col or " , " r ed" ) ;
yl abel ( " $y( x) =\ f r ac{1}{1+x^2}$" , " f ont si ze" , 4, " col or " , " r ed" ) ;
t i t l e( " Runge f unct i on ( #Poi nt s =
" +st r i ng( l engt h( x) ) +" ) . " , " col or " , " r ed" , " f ont si ze" , 4) ;
l egend( " Funct i on eval uat i on" ) ;







Plotting in Scilab www.openeering.com page 4/17
Step 2: Multiple plot and axis setting
In this example we plot two functions on the same figure using the
command plot twice.

Then, we use the command legend to add an annotation to the figure.
With the command gca we get the handle to the current axes with which it
is possible to set axis bounds.


/ / Fi gur e #2: Mul t i pl e pl ot and axi s set t i ng
/ / - - - - - - - - - -
/ / Dat a
x = l i nspace( - 5. 5, 5. 5, 51) ;
y = 1 . / ( 1+x. ^2) ;
/ / Pl ot
scf ( 2) ;
cl f ( 2) ;
pl ot ( x, y, ' r o- ' ) ;
pl ot ( x, y. ^2, ' bs: ' ) ;
xl abel ( [ " x axi s" ; " ( i ndependent var i abl e) " ] ) ;
yl abel ( " y axi s" ) ;
t i t l e( " Funct i ons" ) ;
l egend( [ " Funct i ons #1" ; " Funct i ons #2" ] ) ;
set ( gca( ) , " dat a_bounds" , mat r i x( [ - 6, 6, - 0. 1, 1. 1] , 2, - 1) ) ;










Plotting in Scilab www.openeering.com page 5/17
Step 3: Change axis origin and add grid
In this example the function is plotted over a grid with axis are located
inside the figure.
The command gca is used to get an handle to the figure axis and, hence,
to access the axis fields.




/ / Fi gur e #3 : Change axi s or i gi n and add gr i d
/ / - - - - - - - - - - -
/ / Dat a
x = l i nspace( - 2, 6, 51) ;
y = 1 . / ( 1+x. ^2) ;
/ / Pl ot
scf ( 3) ;
cl f ( 3) ;
pl ot ( x, y, ' r o- ' ) ;
set ( gca( ) , " gr i d" , [ 1 1] ) ;
a = gca( ) ; / / get t he cur r ent axes
a. x_l ocat i on = " or i gi n" ;
a. y_l ocat i on = " or i gi n" ;
set ( gca( ) , " dat a_bounds" , mat r i x( [ - 2, 6, - 0. 2, 1. 2] , 2, - 1) ) ;
xt i t l e( " My t i t l e" , " X axi s" , " Y axi s" ) ;









Plotting in Scilab www.openeering.com page 6/17
Step 4: Another example of multiple plot
This is another way to manage multiple plot. Please notice that the color
black is denoted by k since the letter b is for the color blue.

To MATLAB

users this command may recall hold on and hold off ,


just be careful that the concept of on and off are here reversed.




/ / Fi gur e #4 : Anot her exampl e of mul t i pl e pl ot
/ / - - - - - - - - - - -
/ / Dat a
x = l i nspace( - 2, 6, 51) ;
y = 1 . / ( 1+x. ^2) ;
/ / Pl ot
scf ( 4) ;
cl f ( 4) ;
set ( gca( ) , " aut o_cl ear " , " of f " )
pl ot ( x, y, ' r o- ' ) ;
pl ot ( x, sqr t ( y) , ' bs- ' ) ;
pl ot ( x, y. ^2, ' k: d' ) ;
set ( gca( ) , " aut o_cl ear " , " on" )
xt i t l e( " My t i t l e" , " X axi s" , " Y axi s" ) ;








Plotting in Scilab www.openeering.com page 7/17
Step 5: Semilogy plot
When you have small values to show (e.g. errors, convergence data) a
semilogy plot is mandatory.

The log axis is assigned with the command pl ot 2d( "nl ", ) . The string
"nl " indicates that the first axis is normal and the second axis is
logarithmic.
If the string is reversed ("l n") we have a plot with a logarithmic scale in
the x and a normal scale in the y.

/ / Fi gur e #5 : Semi l ogy pl ot
/ / - - - - - - - - - - -
/ / Dat a
i t er = l i nspace( 0, 10, 11) ;
er r = 10. ^( - i t er ) ;
/ / Pl ot
scf ( 5) ;
cl f ( 5) ;
pl ot 2d( " nl " , i t er , er r , st yl e=2) ;
p = get ( " hdl " ) ;
p. chi l dr en. mar k_mode = " on" ;
p. chi l dr en. mar k_st yl e = 9;
p. chi l dr en. t hi ckness = 3;
p. chi l dr en. mar k_f or egr ound = 2;
xt i t l e( " Semi l ogy" , " I t er at i ons" , " Er r or " ) ;
set ( gca( ) , " gr i d" , [ - 1 1] ) ;








Plotting in Scilab www.openeering.com page 8/17
Step 6: Loglog plot
This plot is logarithmic on both axis as the reader can recognize from the
figure on the right (see labels and ticks) and from the command
pl ot 2d( "l l ", ) .
This kind of charts is widely used in electrical engineering (e.g. for radio
frequency) when plotting gain and filter losses and whenever decibels
(dB) versus frequencies should be plotted. A famous log-log plot is the
Bode diagram.

/ / Fi gur e #6 : Logl og pl ot
/ / - - - - - - - - - - -
/ / Dat a
i nd = l i nspace( 0, 6, 7) ;
i t er = 10. ^i nd;
er r 1 = 10. ^( - i nd) ;
er r 2 = ( 10. ^( - i nd) ) . ^2;
/ / Pl ot
scf ( 6) ;
cl f ( 6) ;
pl ot 2d( " l l " , i t er , er r 1, st yl e=2) ;
p = get ( " hdl " ) ;
p. chi l dr en. mar k_mode = " on" ;
p. chi l dr en. mar k_st yl e = 9;
p. chi l dr en. t hi ckness = 3;
p. chi l dr en. mar k_f or egr ound = 2;

pl ot 2d( " l l " , i t er , er r 2, st yl e=3) ;
p = get ( " hdl " ) ;
p. chi l dr en. mar k_mode = " on" ;
p. chi l dr en. mar k_st yl e = 4;
p. chi l dr en. t hi ckness = 3;
p. chi l dr en. mar k_f or egr ound = 1;

xt i t l e( " Logl og" , " I t er at i ons" , " Er r or " ) ;
set ( gca( ) , " gr i d" , [ 5 5] ) ;
/ / l egend( [ ' er r or 1' ; ' er r or 2' ] , " i n_upper _l ef t " ) ;
l egend( [ ' er r or 1' ; ' er r or 2' ] , " i n_l ower _l ef t " ) ;




Plotting in Scilab www.openeering.com page 9/17
Step 7: Subplot with real and imaginary part
In this figure we have two plots in the same chart: real and complex.
This kind of plotting is particularly useful in signal processing, control
theory and many other fields. For example, with this chart we can plot
magnitude and phase of a Fast Fourier Transform (FFT) analysis.



/ / Fi gur e #7 : Subpl ot wi t h r eal and i magi nar y par t
/ / - - - - - - - - - - -
/ / Dat a
t = l i nspace( 0, 1, 101) ;
y1 = exp( %i *t ) ;
y2 = exp( %i *t . ^2) ;
/ / Pl ot
scf ( 7) ;
cl f ( 7) ;
subpl ot ( 2, 1, 1) ;
pl ot ( t , r eal ( y1) , ' r ' ) ;
pl ot ( t , r eal ( y2) , ' b' ) ;
xt i t l e( " Real par t " ) ;
subpl ot ( 2, 1, 2) ;
pl ot ( t , i mag( y1) , ' r ' ) ;
pl ot ( t , i mag( y2) , ' b' ) ;
xt i t l e( " I mage par t " ) ;









Plotting in Scilab www.openeering.com page 10/17
Step 8: Radial plot
Radial charts are important for displaying phenomena characterized by
direction and distance from a fixed point, such as temperature distribution
in the Earth. These phenomena have a cyclic structure in some
directions.
A typical application occurs in Radio Frequency (RF) engineering where
the Smith chart for scattering parameters, and for impedance transmission
coefficients is widely used.
/ / Fi gur e #8 : Radi al pl ot
/ / - - - - - - - - - - -
/ / Dat a
t het a = 0: . 01: 2*%pi ;
a = 1. 7; r 1 = a^2*cos( 3*t het a) ;
a = 2; r 2 = a^2*cos( 4*t het a) ;
/ / Pl ot
scf ( 8) ; cl f ( 8) ;
subpl ot ( 2, 1, 1) ;
pol ar pl ot ( t het a, r 1, [ 2, 2] ) ;
pol ar pl ot ( t het a, r 2, [ 5, 2] ) ;

/ / Smi t h Char t
subpl ot ( 2, 1, 2) ;
t = l i nspace( 0, 2*%pi , 101) ;
x = cos( t ) ; y = si n( t ) ;
pl ot ( x, y) ;
k = [ . 25 . 5 . 75] ' ;
x1 = k*ones( t ) + ( 1 - k) *cos( t ) ;
y1 = ( 1 - k) *si n( t ) ;
pl ot ( x1' , y1' , ' k' ) ;
kt = [ 2. 5 %pi 3. 79 4. 22] ; k = [ . 5 1 2 4] ;
f or i = 1: l engt h( kt )
t = l i nspace( kt ( i ) , 1. 5*%pi , 51) ;
a = 1 + k( i ) * cos( t ) ;
b = k( i ) + k( i ) * si n( t ) ;
pl ot ( a, b, ' k: ' , a, - b, ' k: ' ) ;
end
a = gca( ) ; a. i sovi ew = ' on' ;
xt i t l e( " Smi t h Char t " ) ;
set ( gca( ) , " dat a_bounds" , mat r i x( [ - 1, 1, - 1, 1] , 2, - 1) ) ;








Plotting in Scilab www.openeering.com page 11/17
Step 9: Polygon plot
This chart represents a customization of the famous bubble chart in
which bubbles are substituted by a polygon (in this case an hexagon).
The function that describes the polygon may be re-written to form any kind
of parametric shape.
As for bubble charts, these polygon plots are particularly useful to
represent three distinct parameters on a two-dimensional chart.

/ / Fi gur e #8 : Pol ygon pl ot
/ / - - - - - - - - - - -
/ / Dat a
def f ( ' [ x, y] =hexagon( c, r ) ' , [ ' x = c( 1) + r *si n( 2*%pi *( 0: 5) / 6) ; ' , ' y
= c( 2) + r *cos( 2*%pi *( 0: 5) / 6) ; ' ] ) ;
n = 10;
xy = r ound( ( r and( n, 2) - 0. 5) *10) ;
r r = r ound( r and( n, 1) *100) / 100;
/ / Pl ot
scf ( 9) ;
cl f ( 9) ;
f or i =1: l engt h( r r )
c = xy( i , : ) ;
r = r r ( i ) ;
[ x, y] =hexagon( c, r ) ;
xpol y( x, y, " l i nes" , 1) ;
e=gce( ) ; / / get t he cur r ent ent i t y
e. f i l l _mode =" on"
e. f or egr ound = 2;
e. backgr ound = 3;
end
pl ot 2d( xy( : , 1) , xy( : , 2) , - 1) ;
set ( gca( ) , " dat a_bounds" , mat r i x( [ - 6, 6, - 6, 6] , 2, - 1) ) ;







Plotting in Scilab www.openeering.com page 12/17
Step 10: Mesh example
In finite elements method (FEM) plotting a mesh is essential. This code
show a simple regular mesh with its node and triangular enumerations.

/ / Fi gur e #9 : Mesh exampl e
/ / - - - - - - - - - - -
/ / Dat a: Node coor di nat e mat r i x
coor d_l x = [ 0 1 2 3 4] ; coor d_x = r epmat ( coor d_l x, 1, 4) ;
coor d_l y = [ 0 0 0 0 0] ; coor d_y = [ ] ;
f or i =1: 4, coor d_y = [ coor d_y, coor d_l y+i - 1] ; end
coor d = [ coor d_x; coor d_y] ' ;
/ / Dat a: Connect i vi t y mat r i x
i npoel = zer os( 4*3*2, 3) ; i ndex = 1;
f or j =1: 3,
f or i =1: 4,
i nd1 = i +( j - 1) *5; i nd2 = ( i +1) +( j - 1) *5;
i nd3 = ( i +1) +j *5; i nd4 = i +j *5;
i npoel ( i ndex, : ) = [ i nd1, i nd3, i nd4] ;
i npoel ( i ndex+1, : ) = [ i nd1, i nd2, i nd3] ;
i ndex = i ndex + 2;
end
end
/ / Dat a: some der i ved dat a
np = si ze( coor d, 1) ; nt = si ze( i npoel , 1) ;
xt n = coor d( i npoel , 1) ; yt n = coor d( i npoel , 2) ;
xn = coor d( : , 1) ; yn = coor d( : , 2) ;
xt n = mat r i x( xt n, nt , l engt h( xt n) / nt ) ' ;
xt r i mesh = [ xt n; xt n( $, : ) ] ;
yt n = mat r i x( yt n, nt , l engt h( yt n) / nt ) ' ;
yt r i mesh = [ yt n; yt n( $, : ) ] ;
xbar = mean( xt n, ' r ' ) ; ybar = mean( yt n, ' r ' ) ;
/ / Pl ot
scf ( 10) ; cl f ( 10) ;
xf pol ys( xt r i mesh, yt r i mesh, r epmat ( 7, 1, nt ) ) ;
pl ot ( xn, yn, ' bo' ) ;
xst r i ng( xn, yn, st r i ng( 1: np) ) ;
xst r i ng( xbar , ybar , st r i ng( 1: nt ) , 0, 1) ;
set ( gca( ) , " dat a_bounds" , mat r i x( [ - 1, 5, - 1, 4] , 2, - 1) ) ;
xt i t l e( " A mesh exampl e" ) ;



Plotting in Scilab www.openeering.com page 13/17
Step 11: Another mesh example
/ / Fi gur e #10 : Anot her mesh exampl e
/ / - - - - - - - - - - - -
xp = [ 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4] ;
yp = [ 3 3 3 3 3 2 2 2 2 2 1 1 1 1 1 0 0 0 0 0] ;

coor d = [ xp' , yp' ] ;
i nt ma = [ 1 6 2; 2 7 3; 3 8 4; 4 9 5;
6 11 7; 7 12 8; 8 13 9; 9 14 10;
11 16 12; 12 17 13; 13 18 14; 14 19 15;
6 7 2; 7 8 3; 8 9 4; 9 10 5;
11 12 7; 12 13 8; 13 14 9; 14 15 10;
16 17 12; 17 18 13; 18 19 14; 19 20 15; ] ;
/ / bar i cent r i c coor di nat es
i t 1 = i nt ma( : , 1) ; i t 2 = i nt ma( : , 2) ; i t 3 = i nt ma( : , 3) ;
xbar = ( xp( i t 1) +xp( i t 2) +xp( i t 3) ) / 3;
ybar = ( yp( i t 1) +yp( i t 2) +yp( i t 3) ) / 3;
np = l engt h( xp) ;
nt = si ze( i nt ma, 1) ;
/ / pl ot mesh
ver t ex = coor d;
f ace = i nt ma;
xvf =
mat r i x( ver t ex( f ace, 1) , si ze( f ace, 1) , l engt h( ver t ex( f ace, 1) ) / si ze( f a
ce, 1) ) ' ;
yvf =
mat r i x( ver t ex( f ace, 2) , si ze( f ace, 1) , l engt h( ver t ex( f ace, 1) ) / si ze( f a
ce, 1) ) ' ;
zvf =
mat r i x( zer os( ver t ex( f ace, 2) ) , si ze( f ace, 1) , l engt h( ver t ex( f ace, 1) ) /
si ze( f ace, 1) ) ' ;
/ / Pl ot t i ng
t col or = r epmat ( [ 0. 5 0. 5 0. 5] , nt , 1) ;
scf ( 11) ; cl f ( 11) ;
pl ot 3d( xvf , yvf , l i st ( zvf , t col or ) ) ;
xt i t l e( " A t r i angl e mesh" ) ;
a = gca( ) ;
a. vi ew = " 2d" ;
a. dat a_bounds=[ mi n( xp) - 1, mi n( yp) - 1; max( xp) +1, max( yp) +1] ;
/ / pl ot node
pl ot ( xp, yp, ' . ' )
/ / xst r i ng( xp, yp, st r i ng( ( 1: np) ' ) ) ;
xnumb( xp, yp, 1: np) ; xnumb( xbar , ybar , 1: nt , [ 1] ) ;





Plotting in Scilab www.openeering.com page 14/17
Step 12: Vector field and contour plot
A vector field displays velocity vectors as arrows with components (fx,fy)
at the points (x,y).
Vector field charts are typically used in fluid-dynamics where it is
necessary to visualized the velocity field.
In fluid-dynamics we encounter vector field combined with a contour plot
for showing the velocity field and the pressure derived from solving
Navier-Stokes equations.

/ / Fi gur e #14: Vect or f i el d and cont our pl ot
/ / Dat a
x = - 1: 0. 1: 1; y = - 1: 0. 1: 1;
[ X, Y] = meshgr i d( x, y) ;
Z = X. ^2 + Y. ^2;
/ / Pl ot
scf ( 12) ;
cl f ( 12) ;
cont our ( x, y, Z, 4) ;
champ( x, y, - X, Y, r ect =[ - 1, - 1, 1, 1] ) ;
a = gca( ) ;
a. i sovi ew = ' on' ;













Plotting in Scilab www.openeering.com page 15/17
Step 13: Scilab mesh function
This chart is an example on how to plot a surface on a 3D chart. The
command meshgrid is used to create a bi-dimensional grid. The function
is then computed in that grid and finally plot with the command mesh.
/ / Fi gur e #12 : Mesh
/ / - - - - - - - - - - - -
/ / Dat a
x = - 1: 0. 1: 1;
y = - 1: 0. 1: 1;
[ X, Y] = meshgr i d( x, y) ;
Z = X. ^2 + Y. ^2;
/ / Pl ot
scf ( 13) ;
cl f ( 13) ;
mesh( X, Y, Z) ;
xl abel ( ' X' ) ; yl abel ( ' Y' ) ; zl abel ( ' Z' ) ;



Step 14: Surface with a colormap
In this example we use the command surf with a color map.

/ / Fi gur e #11 : Sur f ace wi t h a col or map
/ / - - - - - - - - - - - -
/ / Dat a
x = - 1: 0. 1: 1;
y = - 1: 0. 1: 1;
[ X, Y] = meshgr i d( x, y) ;
Z = X. ^2 + Y. ^2;
/ / Pl ot
scf ( 14) ;
cl f ( 14) ;
xset ( " col or map" , j et col or map( 64) ) ;
sur f ( X, Y, Z) ;
xl abel ( ' X' ) ; yl abel ( ' Y' ) ; zl abel ( ' Z' ) ;





Plotting in Scilab www.openeering.com page 16/17
Step 15: Creating a video in Scilab
With the following command we produce a sequence of png images with
can then be combined to generate a video or an animated png image.

/ / Dat a
x = l i nspace( - 1, 1, 101) ;
kval = l i nspace( 0, 1, 21) ;
def f ( ' [ y] =mymor ph( x, k) ' , ' y=( 1- k) *si n( 2*%pi *2*x) + k*x. ^3' ) ;
/ / I t er at i ve pl ot
f or i =1: l engt h( kval )
k = kval ( i )
y = mymor ph( x, k) ;
scf ( 15) ;
cl f ( 15) ;
dr awl at er ( ) ;
pl ot ( x, y, ' b- ' ) ;
t i t l e( " i t er # " +st r i ng( i ) , " f ont si ze" , 4) ;
xl abel ( " x" , " f ont si ze" , 4) ;
yl abel ( " y" , " f ont si ze" , 4) ;
set ( gca( ) , " dat a_bounds" , mat r i x( [ - 1, 1, - 1, 1] , 2, - 1) ) ;
dr awnow( ) ;
/ / Save f i gur e i n png f or mat
xs2png( gcf ( ) , spr i nt f ( " exampl e_%03d. png" , i ) ) ;
sl eep( 100) ;
end

The animated png movie can be done outside of this Scilab script starting
from all saved images. To produce the movie, the reader can use his/her
preferred available tool.
To create our animated png image we used the program JapngEditor,
which is free of charge and available for download (see reference).












Plotting in Scilab www.openeering.com page 17/17
Step 16: Concluding remarks and References
In this tutorial we have collected and explained a series of figures that are
available in Scilab for plotting any charts that scientists and engineers
may need in their daily work.


1. Scilab Web Page: Available: www.scilab.org.

2. Openeering: www.openeering.com.

3. J apngEditor : https://www.reto-hoehener.ch/japng/.

Step 17: Software content
To report a bug or suggest some improvement please contact Openeering
team at the web site www.openeering.com.





Thank you for your attention,
Manolo Venturin

- - - - - - - - - - - - - -
Mai n di r ect or y
- - - - - - - - - - - - - -
pl ot t i ng. sce : A col l ect i on of pl ot .
ex1_ani mat ed. png : Exampl e of ani mat ed png i mage
l i cense. t xt : The l i cense f i l e

Potrebbero piacerti anche