Basic Data Handling and Plotting With SCILAB: January 2013
Basic Data Handling and Plotting With SCILAB: January 2013
net/publication/321998542
CITATIONS READS
0 2,110
1 author:
Howard Njoku
University of Nigeria
33 PUBLICATIONS 74 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
Performance Evaluation of Models for Estimating Solar Radiation on Tilted Surfaces View project
Modelling of solar water heater in order to reduce the peak demand- Sunyani as a case study View project
All content following this page was uploaded by Howard Njoku on 22 December 2017.
CHAPTER 11
BASIC DATA HANDLING AND PLOTTING WITH SCILAB
by
H.O. NJOKU
Department of Mechanical Engineering
University of Nigeria, Nsukka
[email protected]; +234 (0)809 148 9064
sheets = readxls(‘/home/sis_uche/Desktop/Data.xls’)
typeof(sheets)
161
162 NJOKU: Plotting with Scilab
• To assign the contents of the 1st sheet to the variable ‘s1’, enter the command:
s1 = sheets(1)
• To get the numeric values stored in s1 and assign them to a variable ‘A’, enter the command:
A = s1.value
This command and the previous one can be carried out in one step as follows:
A = sheets(1).value
• The variable A holds a matrix on which any of the standard matrix operations can be performed.
To perform a math operation on matrix A:
B = 3 * A
C = log(A(2:5,4:8))
• Let us generate a 5 by 5 matrix holding some random data and assign it to the variable ‘D’:
D = rand(5,5)*15
• In order to write the data to disk as a .csv file, we use the write_csv command in any of the
forms listed below:
// create equal space (tab) seperated .csv file
2 write_csv(D,’/home/sis_uche/Desktop/Data.csv’,’\t’,’.’)
The command on line 1 generates a row matrix of 21 equally space elements having 0 (zero) and π as
the first and last elements, and assigns it to the variable x. The command on line 2 defines our simple
trigonometric function, performing an operation on x and assigning the result to y.
164 NJOKU: Plotting with Scilab
scf(1) and clf(1) on lines 3 and 4 mean ‘set current figure’ and ‘clear current figure’, respec-
tively. By these commands, we ensure that any plots on the current figure space (set equal to 1) is
cleared and the subsequent plot to be generated appears on this space. When no number is specified,
the clf() command clears all open figures, while the scf command sets the current figure to zero.
A plot of y against x is generated by the plot(x,y,‘o-b’) command on line 5.
The basic command for single line plots in Scilab is plot(x,y). This will generate a black conti-
nous line with no markers. By including the optional argument ‘o-b’, we obtain the default solid
line (-), of blue colour (b) and with a circle line marker (o). Other options are specified as follows.
‘r’ for red, ’g‘ for green, ‘b’ for blue, ‘c’ for cyan, ‘m’ for magenta, ‘y’ for yellow, ‘k’ for black
(which is the default), and ‘w’ for white.
• Line markers: Other line markers can be specified with
‘+’ for plus sign, ‘o’ for circle, ‘*’ for asterisk, ‘.’ for point, ‘x’ for cross, ‘s’ or ‘square’
for square, ‘d’ or ‘diamond’ for diamond, ‘ˆ’ for upward pointing triangle, ‘v’ for downward
pointing triangle, ‘<’ for left pointing triangle, ‘>’ for right pointing triangle, ‘pentagram’ for
star, and ‘none’ for no marker (which is the default).
Axis labels: We use the commands ‘xlabel’, ‘ylabel’ (on lines 6 and 7, respectively), and
‘zlabel’ (in the case of 3-d plots) for adding labels to the x-, y- and z-axes of our plots.
Plot titles: The ‘title’ command is used to give a title to the plot.
Legends: The ‘legend’ command is used to display legend, especially when there are multiple lines
on a plot.
Annotation properties: The axis label, plot title and legend commands mentioned above require only
one compulsory arguement which is the quoted text we wish to appear as the axis label, plot title or
legend. However, the final appearance of the included text, such as its colour, fontsize, position, etc
can be easily modified using any of the keywords and options listed in Table 11.2, which must be
enclosed in quotes.
In order to modify the appearance of the axes of our plots, we first get the handle on current axes,
using the gca() command, viz.:
props = gca()
166 NJOKU: Plotting with Scilab
Thereafter, with
props.axis_properties = option
props.x_location = ‘top’
props.data_bounds = [xmin, ymin; xmax, ymax]
Grid lines can be included at specified specified intervals with the command:
The next set of commands incorporates all that has been discussed so far, and the output is shown in
Fig. 11.2.
x = linspace(0,%pi,21);
2 y = sin(2*x + 1);
scf(1);
4 clf(1);
plot(x,y,’o-b’);
6 xlabel(’$0 \leq x \leq 3$’,’fontsize’,4,’color’,’red’);
ylabel(’$ y(x)=sin(2*x+1)$’,’fontsize’,4,’color’,’red’);
8 title(’Trig. function (21 points)’,’color’,’red’,’fontsize’,4,’position’,[0.75
-1.8]);
legend(’Trig. Function Evaluation’);
10 props = gca()
props.x_location = ’top’
12 props.data_bounds = [0, -1.5; 3, 1.5]
props.grid = [0.5 0.5]
plot(x,sqrt(y),’*:g’);
8 xlabel(’$0 \leq x \leq \pi$’,’fontsize’,4,’color’,’red’);
ylabel(’$ y(x),\, 0.5 y,\, \sqrt{y}$’,’fontsize’,4,’color’,’red’);
10 title(’Function evaluation (21 points)’,’color’,’red’,’fontsize’,4);
legend(’$y(x)$’,’$0.5 y$’,’$\sqrt{y}$’);
We see that modifying the legend is also as straightforward as specifying appropriate text for each
additional line, thus:
legend(‘$y(x)$’,‘$0.5 y$’,‘$\sqrt{y}$’)
subplot(i,j,k)
where i is the number of plot rows, j, the number of plot columns, and k, the location of the current
plot, counting left-to-right from the first plot location. The use of the subplot() command is
explored in a later section.
168 NJOKU: Plotting with Scilab
where x is a real scalar or vector of size N and y is a real scalar, vector of size N or a matrix of size
N * M. The value of the optional arguement width lies between 0 and 1, color may be specified
as earlier stated, and style may be either ‘grouped’ or ‘stacked’.
Let’s try out the following:
clf();
2 x = [31, 59, 90];
y = [2, 10, 12; 6, 4, 14; 8, -6, 16];
4 bar(x,y,0.6,’grouped’);
xtitle(’1st Quater Expenses’, ’Day number’, ’Amount (NGN)’);
6 legend(’Accomodation’, ’Gifts’, ’Feeding’, "in_lower_left");
Instead of a vertical bar chart, our desire could instead be to create a horizontal bar chart. This will
be accomplished with the command:
whose output is shown in Fig. 11.5. Note that the legend generated in the horizontal bar chart does
not convey much information. This reminds us that when using Scilab for creating plots you’ll have
to determine which kind of plot is most appropriate for the data you have. Note also that that an
a.y_ticks.labels command has been used to specify text tick labels on the y-axis. The same
can be done on the x-axis with an a.x_ticks.labels command.
170 NJOKU: Plotting with Scilab
pie(x[,sp, txt])
where x is a scalar or vector of positive real numbers. The optional sp is a real scalar or vector of
real numbers while txt is a cell or vector of strings corresponding to the elements of x.
Let’s try the following:
clf();
2 x = [31, 1.1, 6, 5.2, 8, 4.5, 22];
pie(x,[1,0,0,0,1,1,1],[’Defence’, ’Science & Tech’,’Education’, ’Health’, ’
Culture’, ’Tourism’, ’Power and Steel’]);
4 title(’Sectoral Expenditure for 2018’,’color’,’red’,’fontsize’,4);
By the command on line 1, we read the .xls file and assign it to the variable sheets. Next on line
2, we assign the first sheet to a variable (s1), and in the subsequent commands, perform the contour
plotting operations. The output is shown in Fig. 11.7.
command generates coloured surfaces. The mesh and surf commands’ syntax is as follows:
mesh(x,y,z)
surf(x,y,z)
where x and y are two vectors whose dimensions (M and N, respectively) correspond to those of z,
which is a real M*N matrix which defines the surface height.
Let’s try out the following:
clf();
2 x = [0:0.3:2*%pi]’;
y = [0:0.3:2*%pi];
4 z = sin(x)*cos(y);
scf(0);
6 mesh(x,y,z);
scf(1);
8 surf(x,y,z);
The mesh command output is shown in Fig. 11.8(a), while the output of the surf command is shown
in Fig. 11.8(b).
Both plots can be jointly generated using the subplot command:
clf();
2 x = [0:0.3:2*%pi]’;
y = [0:0.3:2*%pi];
4 z = sin(x)*cos(y);
subplot(211);
6 mesh(x,y,z);
subplot(212);
8 surf(x,y,z);
11.4 Conclusion
So very much more can be done with Scilab in terms of graph plotting which can be explored in the
Scilab help environment and numerous online resources. This brief discussion is aimed at arousing
interest in this direction.
174 NJOKU: Plotting with Scilab
(a) Mesh.
(b) Surf.