xsmb xsmn Kết quả xổ số miền bắcProducing plots and graphics is a very common task for analysing data and creating reports. Scilab oﬀers many ways to create and customize various types of plots and charts. In this section, we present how to create 2D plots and contour plots. Then we customize the title and the legend of our graphics. We ﬁnally export the plots so that we can use it in a report.
Scilab can produce many types of 2D and 3D plots. It can create x-y plots with the plot function, contour plots with the contour function, 3D plots with the surf function, histograms with the histplot function and many other types of plots. The most commonly used plot functions are presented in ﬁgure 19.
plot | 2D plot |
surf | 3D plot |
contour | contour plot |
pie | pie chart |
histplot | histogram |
bar | bar chart |
barh | horizontal bar chart |
hist3d | 3D histogram |
polarplot | plot polar coordinates |
Matplop | 2D plot of a matrix using colors |
Sgrayplot | smooth 2D plot of surface using colors |
grayplot | 2D plot of a surface using colors |
Figure 19: Scilab plot functions
xsmb xsmn Kết quả xổ số miền bắcIn order to get an example of a 3D plot, we can simply type the statement surf() in the Scilab console.
-->surf()
xsmb xsmn Kết quả xổ số miền bắc
During the creation of a plot, we use several functions in order to create the data or to conﬁgure the plot. The functions presented in ﬁgure 20 will be used in the examples of this section.
linspace | linearly spaced vector |
feval | evaluates a function on a grid |
legend | conﬁgure the legend of the current plot |
title | conﬁgure the title of the current plot |
xtitle | conﬁgure the title and the legends of the current plot |
Figure 20: Scilab functions used when creating a plot.
In this section, we present how to produce a simple x-y plot. We emphasize the use of vectorized functions, which produce matrices of data in one function call.
We begin by deﬁning the function which is to be plotted. The myquadratic function squares the input argument x with the ”ˆ”operator.
function f = myquadratic ( x )
f = x^2
endfunction
We can use the linspace function in order to produce 50 values in the interval [1,10].
xdata = linspace ( 1 , 10 , 50 );
The xdata variable now contains a row vector with 50 entries, where the ﬁrst value is equal to 1 and the last value is equal to 10. We can pass it to the myquadratic function and get the function value at the given points.
ydata = myquadratic ( xdata );
This produces the row vector ydata, which contains 50 entries. We ﬁnally use the plot function so that the data is displayed as an x-y plot.
plot ( xdata , ydata )
xsmb xsmn Kết quả xổ số miền bắc
Figure 21 presents the associated x-y plot.
xsmb xsmn Kết quả xổ số miền bắcFigure 21: A simple x-y plot.
Notice that we could have produced the same plot without generating the intermediate array ydata. Indeed, the second input argument of the plot function can be a function, as in the following session.
plot ( xdata , myquadratic )
xsmb xsmn Kết quả xổ số miền bắc
When the number of points to manage is large, using functions directly saves signiﬁcant amount of memory space, since it avoids generating the intermediate vector ydata.
In this section, we present the contour plots of a multivariate function and make use of the contour function. This type of graphic is often used in the context of numerical optimization because it draws functions of two variables in a way that shows the location of the optimum.
The Scilab function contour plots contours of a function f. The contour function has the following syntax
contour(x,y,z,nz)
where
• x (resp. y) is a row vector of x (resp. y) values with size n1 (resp. n2),
• z is a real matrix of size (n1,n2), containing the values of the function or a Scilab function which deﬁnes the surface z=f(x,y),
xsmb xsmn Kết quả xổ số miền bắc• nz the level values or the number of levels.
xsmb xsmn Kết quả xổ số miền bắcIn the following Scilab session, we use a simple form of the contour function, where the function myquadratic is passed as an input argument. The myquadratic function takes two input arguments x1 and x2 and returns f(x1,x2) = x1^2 + x2^2. The linspace function is used to generate vectors of data so that the function is analyzed in the range [−1,1]^2.
function f = myquadratic2arg ( x1 , x2 )
f = x1**2 + x2**2;
endfunction
xdata = linspace ( -1 , 1 , 100 );
ydata = linspace ( -1 , 1 , 100 );
contour ( xdata , ydata , myquadratic2arg , 10)
xsmb xsmn Kết quả xổ số miền bắc
This produces the contour plot presented in ﬁgure 22.
Figure 22: Contour plot of the function f(x1,x2) = x1^2 + x2^2.
xsmb xsmn Kết quả xổ số miền bắcIn practice, it may happen that our function has the header z = myfunction ( x ), where the input variable x is a row vector. The problem is that there is only one single input argument, instead of the two arguments required by the contour function. There are two possibilities to solve this little problem:
xsmb xsmn Kết quả xổ số miền bắc • provide the data to the contour function by making two nested loops,
xsmb xsmn Kết quả xổ số miền bắc• provide the data to the contour function by using feval,
• deﬁne a new function which calls the ﬁrst one.
xsmb xsmn Kết quả xổ số miền bắcThese three solutions are presented in this section. The ﬁrst goal is to let the reader choose the method which best ﬁts the situation. The second goal is to show that performances issues can be avoided if a consistent use of the functions provided by Scilab is done.
In the following Scilab naive session, we deﬁne the function myquadratic1arg, which takes one vector as its single input argument. Then we perform two nested loops to compute the zdata matrix, which contains the z values. The z values are computed for all the combinations of points (x(i),y(j)) ∈ R^2, for i = 1,2,…,nx and j = 1,2,…,ny, where nx and ny are the number of points in the x and y coordinates. In the end, we call the contour function, with the list of required levels (instead of the previous number of levels). This gets exactly the levels we want, instead of letting Scilab compute the levels automatically.
function f = myquadratic1arg ( x )
f = x(1)**2 + x(2)**2;
endfunction
xdata = linspace ( -1 , 1 , 100 );
ydata = linspace ( -1 , 1 , 100 );
// Caution ! Two nested loops , this is bad.
for i = 1:length(xdata)
for j = 1:length(ydata)
x = [xdata(i) ydata(j)].’;
zdata ( i , j ) = myquadratic1arg ( x );
end
end
contour ( xdata , ydata , zdata , [0.1 0.3 0.5 0.7])
The contour plot is presented in ﬁgure 23.
Figure 23: Contour plot of the function f(x1,x2) = x1^2+x2^2 – The levels are explicitly conﬁgured.
The previous script works perfectly. Still, it is not eﬃcient because it uses two nested loops and this should be avoided in Scilab for performance reasons. Another issue is that we had to store the zdata matrix, which might consume a lot of memory space when the number of points is large. This method should be avoided since it is a bad use of the features provided by Scilab.
xsmb xsmn Kết quả xổ số miền bắcIn the following script, we use the feval function, which evaluates a function on a grid of values and returns the computed data. The generated grid is made of all the combinations of points (x(i),y(j)) ∈ R^2. We assume here that there is no possibility to modify the function myquadratic1arg, which takes one input argument. Therefore, we create an intermediate function myquadratic3, which takes 2 input arguments. Once done, we pass the myquadratic3 argument to the feval function and generate the zdata matrix.
function f = myquadratic1arg ( x )
f = x(1)**2 + x(2)**2;
endfunction
function f = myquadratic3 ( x1 , x2 )
f = myquadratic1arg ( [x1 x2] )
endfunction
xdata = linspace ( -1 , 1 , 100 );
ydata = linspace ( -1 , 1 , 100 );
zdata = feval ( xdata , ydata , myquadratic3 );
contour ( xdata , ydata , zdata , [0.1 0.3 0.5 0.7])
The previous script produces, of course, exactly the same plot as previously. This method should be avoided when possible, since it requires storing the zdata matrix, which has size 100×100.
xsmb xsmn Kết quả xổ số miền bắcFinally, there is a third way of creating the plot. In the following Scilab session, we use the same intermediate function myquadratic3 as previously, but we pass it directly to the contour function.
function f = myquadratic1arg ( x )
f = x(1)**2 + x(2)**2;
endfunction
function f = myquadratic3 ( x1 , x2 )
f = myquadratic1arg ( [x1 x2] )
endfunction
xdata = linspace ( -1 , 1 , 100 );
ydata = linspace ( -1 , 1 , 100 );
contour ( xdata , ydata , myquadratic3 , [0.1 0.3 0.5 0.7])
The previous script produces, of course, exactly the same plot as previously. The major advantage is that we did not produce the zdata matrix. We have brieﬂy outlined how to produce simple 2D plots.
We are now interested in the conﬁguration of the plot, so that the titles, axis and legends corresponds to our data.
xsmb xsmn Kết quả xổ số miền bắcIn this section, we present the Scilab graphics features which conﬁgure the title, axes and legends of an x-y plot.
xsmb xsmn Kết quả xổ số miền bắcIn the following example, we deﬁne a quadratic function and plot it with the plot function.
function f = myquadratic ( x )
f = x.^2
endfunction
xdata = linspace ( 1 , 10 , 50 );
ydata = myquadratic ( xdata );
plot ( xdata , ydata )
We now have the plot which is presented in ﬁgure 21.
xsmb xsmn Kết quả xổ số miền bắcScilab graphics system is based on graphics handles. The graphics handles provide an object-oriented access to the ﬁelds of a graphics entity. The graphics layout is decomposed into sub-objects such as the line associated with the curve, the x and y axes, the title, the legends, and so forth. Each object can be in turn decomposed into other objects if required. Each graphics object is associated with a collection of properties, such as the width or color of the line of the curve. These properties can be queried and conﬁgured simply by getting or setting their values, like any other Scilab variables. Managing handles is easy and very eﬃcient.
But most basic plot conﬁgurations can be done by simple function calls and, in this section, we will focus in these basic features.
In the following script, we use the title function in order to conﬁgure the title of our plot.
title ( "My title" );
We may want to conﬁgure the axes of our plot as well. For this purpose, we use the xtitle function in the following script.
xtitle ( "My title" , "X axis" , "Y axis" );
Figure 24 presents the produced plot.
xsmb xsmn Kết quả xổ số miền bắcFigure 24: The x-y plot of a quadratic function – This is the same plot as in ﬁgure 21, with title and x-y axes conﬁgured.
It may happen that we want to compare two sets of data in the same 2D plot, that is, one set of x data and two sets of y data. In the following script, we deﬁne the two functions f(x) = x^2 and f(x) = 2*x^2 and plot the data on the same x-y plot. We additionally use the ”+-” and ”o-” options of the plot function, so that we can distinguish the two curves f(x) = x^2 and f(x) = 2*x^2.
function f = myquadratic ( x )
f = x^2
endfunction
function f = myquadratic2 ( x )
f = 2 * x^2
endfunction
xdata = linspace ( 1 , 10 , 50 );
ydata = myquadratic ( xdata );
plot ( xdata , ydata , "+-" )
ydata2 = myquadratic2 ( xdata );
plot ( xdata , ydata2 , "o-" )
xtitle ( "My title" , "X axis" , "Y axis" );
Moreover, we must conﬁgure a legend so that we can know what curve is associated with f(x) = x^2 and what curve is associated with f(x) = 2*x^2. For this purpose, we use the legend function in order to print the legend associated with each curve.
legend ( "x^2" , "2x^2" );
Figure 25 presents the produced x-y plot.
xsmb xsmn Kết quả xổ số miền bắcxyplot_legend
Figure 25: The x-y plot of two quadratic functions – We have conﬁgured the legend so that we can distinguish the two functions f(x) = x^2 and f(x) = 2*x^2.
We now know how to create a graphics plot and how to conﬁgure it. If the plot is suﬃciently interesting, it may be worth putting it into a report. To do so, we can export the plot into a ﬁle, which is the subject of the next section.
In this section, we present ways of exporting plots into ﬁles, either interactively or automatically with Scilab functions.
Bitmap
Vectorial | |
xs2png | export into PNG |
xs2pdf | export into PDF |
xs2svg | export into SVG |
xs2eps | export into Encapsulated Postscript |
xs2ps | export into Postscript |
xs2emf | export into EMF (only for Windows) |
Bitmap | |
xs2fig | export into FIG |
xs2gif | export into GIF |
xs2jpg | export into JPG |
xs2bmp | export into BMP |
xs2ppm | export into PPM |
Figure 26: Export functions.
We can alternatively use the xs2* functions, presented in ﬁgure 26. All these functions are based on the same calling sequence:
xs2png ( window_number , filename )
xsmb xsmn Kết quả xổ số miền bắc
where window_number is the number of the graphics window and filename is the name of the ﬁle to export. For example, the following session exports the plot which is in the graphics window number 0, which is the default graphics window, into the ﬁle foo.png.
xs2png ( 0 , "foo.png" )
xsmb xsmn Kết quả xổ số miền bắc
If we want to produce higher quality documents, the vectorial formats are preferred. For example, LATEX documents may use Scilab plots exported into PDF ﬁles to improve their readability, whatever the size of the document.