Scilab/C2/Plotting-2D-graphs/English

From Script | Spoken-Tutorial
Revision as of 19:35, 27 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Plotting 2D Graphs

Author: Anuradha Amrutkar

Keywords: Plotting, 2D Graphs, plot, plot2d


Visual Cue
Narration
Slide Welcome to the spoken tutorial on Plotting 2D graphs with scilab.
Slide Assuming that Scilab is installed on your computer, we will discuss plots in Scilab.
Narration Scilab offers many ways to create and customize various types of 2D and 3D plots.
Narration The several common charts Scilab can create are: x-y plots, contour plots, 3D plots, histograms, bar charts, etc...
Demonstration Now open your Scilab console window.
Demonstration I will use Plotting.sce file to cut and paste the commands.
Narration In order to Plot, we need a set of points. Let us create a sequence of equally spaced points.
Narration This can be done by the linspace command which creates a linearly equally spaced vector.
Demonstration For Example
 --> x=linspace(1,10,5)

x is a row vector with 5 points with linearly equally spaced between 1 and 10

Demonstration Similarly y is a row vector with linearly equally spaced 5 points between 1 and 20
 --> y=linspace(1,20,5); 
Narration More information on linspace command can be obtained from the Help documentation.
Narration We will now plot a graph with the arguments x and y using the Plot function.
Demonstration This is similar to the one used in matlab.
 --> plot(x,y)

creates a graph of x verses y as you see.

Demonstration Notice that the graphics window is labeled as '0' We will open another graphic window using the
 xset('window',1)

function. I will close this cut the xset function paste in scilab hit enter.

Demonstration You will see a graphic window number 1. Note that two arguments are passed to this function namely the window and 1.
Demonstration The next graph will be plotted on this window.
Demonstration For scilab plot 2d is the native function used to plot 2d graphs.
 --> plot2d(x,y,style=3)
Demonstration plot2d command plots a graph of x verses y as you see.
Demonstration Notice that there is a third argument called style.
Narration Style argument is optional.It is used to customize the appearance of the plot.
Demonstration For positive values of style the curve is a plain with different colours like green for 3 in our case.
Narration The default value of style is 1.
Narration Try plotting graphs for negative values and see the difference in appearance for yourself.
Demonstration Also we can set the start points and end points for x and y axis by passing the fourth argument.It is called rect.
 --> plot2d(x,y,style=3,rect=[1,1,10,20])
Demonstration We have x axis starting from 1 to 10 and y axis from 1 to 20.
Narration The order of argument in the rect command is xmin,ymin,xmax and ymax.
Narration Let us now learn about Title,Axis and Legends
Demonstration To configure labels to the axis and title to the plot we can use the commands
 --> title("My title")
 --> xlabel("X"); and
 --> ylabel("Y");
Demonstration I will cut this set of commands and paste in the console. Hit enter
Demonstration You will see that the graph has been labeled x to the x axis, y to the y axis and the title of the graph is my title.
Demonstration You may want to configure the title and axis of the plot in a single command instead of 3 for this purpose we use the xtitle
 --> xtitle ( " My title " , " X axis " , " Y axis " );

command with all the 3 arguments.

Demonstration I will cut this command paste in scilab enter.
Narration Now you see that the x axis label is X axis , Y axis and the title is My title.
Demonstration The clf() function that i am typing now will clear the graphic window as you see.
Narration It is useful while plotting different graph on the same graphic window.
Demonstration I will close this window.
Narration Sometimes we need to compare two sets of data in the same plot, that is, one set of x data and two sets of y data.
Narration Let us see an example for this I will scroll down
Demonstration We will define the x axis points in a row vector x using the linspace command
Demonstration Let us define a function

y1 = x^2

plot x verses y1

Demonstration define another function

y2 = 2*x^2

plot x verses y2

Demonstration we will also give label and title to our graph
Demonstration Notice that we have additionally passed ”o-” and ”+-” commands to the plot function, to change the appearance of the curve
Demonstration --> x = linspace ( 1 , 10 , 50 );
 --> y1 = x^2;
 --> plot (x ,y1 , "o-")
 --> y2 = 2*x ^2;
 --> plot (x, y2, "+-")
 --> xtitle ("My title" , "X axis" , "Y axis" );
Demonstration These arguments are not a part of the plot2d function.
Narration They can be used only with the plot function
Demonstration I will copy these set of commands and paste in the scilab console.
Demonstration You see the graph
Narration Wouldn't it be of great help to know which curve is associated with which function?
Demonstration This can be achieved using the legend command as you see
 --> legend ( " x ^ 2 " , " 2* x ^ 2 " );
Demonstration "o-" curve represents function y1=x^2 function and "+-" curve represents function y2=2*x^2
Demonstration I will close this graphic window
[[[ File:title.jpeg]]]
Narration We will now discuss about plot2d demos and subplot function
Narration Scilab provides demos for all its major functions Demos of plot2d can be viewed through demonstration tab.
Demonstration click on Graphics,click plot2d_3d plots and select a demo out the various demos provided. I will click on plot2d.
Demonstration You will see the demo graph
Demonstration The code for this graph can also be seen by clicking the view code button here.
Narration This link does not open in Mac OS but works in windows and linux
Demonstration Nevertheless in Mac the code can be viewed through the directory.
Demonstration Let us go to the terminal.
Demonstration currently i am in demos directory of scilab 5.2
Demonstration The full path to this directory is shown here.
Demonstration we will type
 ls

to see the list of demos available as you see here.

Demonstration then we will select the 2d_3d_plots directory and hit enter type ls again to see various demo code available in the sce files
Demonstration we will view the code for the demo which we have seen earlier. type and hit enter.
 more plot2d.dem.sce
Demonstration Here you will see the code for the demo graph of plot2d function
Demonstration I will close the terminal I will close the demo graph and the demos window
Demonstration Similarly you can go through the other demos and explore scilab
Narration Let us now discuss about Subplot function
Narration The subplot() function divides a graphics window into a matrix of sub-windows.
Narration To explain this function we will use demos for plotting 2D graphs in scilab.
Demonstration for eg. type
 -->plot2d()

in your console and see the demo plot for this function

[[[ File:plot2d.jpg]]]
Demonstration I will close this window.
Narration The subplot command breaks the graphics window into a 2 by 2 matrix of sub-windows represented by the first two arguments in the subplot command the third argument denotes the current window in which the plot will be plotted
Demonstration I will execute this whole set of commands by copying in the scilab console
Demonstration -->subplot(221)
 -->plot2d()
 -->subplot(222)
 -->plot2d2()
 -->subplot(2,2,3)
 -->plot2d3()
 -->subplot(2,2,4)
 -->plot2d4()
Demonstration [[[ File:subplot2.GIF]]]
Demonstration You can see 4 plots in a single plot window
Demonstration The plot obtained can be saved as a image on your computer
Demonstration Click on the graphic window, go to File menu select export to .
Demonstration Give a suitable title to your plot, select a destination folder to save your file select the file format in which you want your image to appear.
Demonstration I will select the JPEG format and Click Save.
Demonstration Browse through the directory to open the image and verify yourself whether it has been saved or not.
Narration This brings us to the end of this spoken tutorial on Plotting in Scilab.
Slide There are many other functions in Scilab which will be covered in other spoken tutorials.
Slide Keep watching the Scilab links.
Slide Spoken Tutorials are part of the Talk to a Teacher project, supported by the National Mission on Education through ICT.
Slide More information on the same is available at http://spoken-tutorial.org/NMEICT-Intro.

Contributors and Content Editors

Chandrika