Difference between revisions of "Gnuplot/C2/Plotting-functions/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 2: Line 2:
 
||'''Visual Cue'''
 
||'''Visual Cue'''
 
||'''Narration'''
 
||'''Narration'''
 +
 +
 +
 +
 +
{|
 +
|-
  
 
|-
 
|-
| '''Slide Number 1'''
+
||'''Slide Number 1'''
 
'''Title Slide '''
 
'''Title Slide '''
 
'''Plotting functions'''
 
'''Plotting functions'''
|| Welcome to the tutorial on '''Plotting functions'''
+
|| Welcome to the tutorial on '''Plotting functions in gnuplot'''.
  
 
|-
 
|-
Line 13: Line 19:
 
'''Learning Objectives'''
 
'''Learning Objectives'''
 
|| In this tutorial we will learn,
 
|| In this tutorial we will learn,
 
+
* '''Plot''' and '''replot commands''' for '''2D''' plotting
* Plot & replot commands for 2D plotting
+
* Plot three '''functions: sin x, cos x '''and''' sin x plus cos x'''
* Plot three functions '''sin x, cos x '''and''' sin x plus cos x'''
+
* Draw grid and set limits  
* Draw grid and set limits
+
* Zoom in and zoom out of a plot
* Zoom into a graph
+
  
  
Line 23: Line 28:
 
|| '''Slide Number 3'''
 
|| '''Slide Number 3'''
 
'''Learning Objectives'''
 
'''Learning Objectives'''
||  
+
||
* Add axis labels and graph title
+
* Add axis labels and graph title
* Change font style and size
+
* Change font '''style''' and size
* Add '''Greek''' '''symbols'''
+
* Add '''Greek symbols'''
* Set axis tick marks
+
* Set axis''' tick marks ''' and
* Write data points to a table
+
* Save the data points to a file.
  
  
 
|-
 
|-
|| '''Slide Number 4'''
+
|| '''Slide Number 3'''
 
'''System and Software Requirement'''
 
'''System and Software Requirement'''
 +
 +
 +
 
|| To record this tutorial, I am using
 
|| To record this tutorial, I am using
* Debian Linux 9.3
+
 
* Gedit text editor 3.22.0 and
+
 
* gnuplot 5.2.5 installed
+
* '''Debian Linux''' 9.3
 +
* '''gnuplot''' 5.2.6
 +
* '''gedit text editor''' 3.22
  
 
|-
 
|-
|| '''Slide Number 5'''
+
|| '''Slide Number 4'''
 
'''Pre-requisites'''
 
'''Pre-requisites'''
|| To follow this tutorial, learners must be familiar with,
+
|| To follow this tutorial,  
* Basic computer and internet skills
+
 
* Concept of graphing
+
* Learners must be familiar with high school level Mathematics.
and
+
* For pre-requisite gnuplot tutorials, please visit this site.
* High school level Mathematics skills
+
  
 
|-
 
|-
|| '''Ctrl alt t'''
+
|| Press '''Ctrl Alt T'''
|| Open a terminal by pressing control alt t keys together
+
|| Open a '''terminal''' by pressing '''Ctrl, Alt''' and''' T''' keys simultaneously.
A terminal opens
+
  
 
|-
 
|-
|| '''>gnuplot'''.
+
|| Type cd Desktop and press '''Enter'''
|| Enter command '''gnuplot''', to open '''gnuplot'''
+
|| A terminal opens.
 +
Change directory to '''Desktop''' as seen.
  
 
|-
 
|-
|| '''control shift k'''
+
|| '''>gnuplot'''
|| The terminal prompt changes to '''gnuplot''' when program runs
+
Press '''Enter'''
I will clear the screen and bring terminal prompt to top of the screen for clarity.  
+
|| In the '''terminal''', type '''gnuplot''' and press '''Enter'''.
  
 
|-
 
|-
|| >'''plot sin (x)''' and press enter
+
|| Hover mouse near gnuplot prompt
|| Type, '''plot space sin x'''
+
|| The '''prompt''' changes to '''gnuplot prompt.'''
Note that angle '''X''' is within braces and press enter
+
 
 +
To execute a '''command''' in the '''terminal''', press '''Enter''' after typing it.
 +
I will not mention this separately.
  
 
|-
 
|-
||  
+
|| Press''' Ctrl shift K'''
|| A graphical window with plot of '''x, sin x''' appear on screen
+
|| I will clear the '''terminal.'''
  
 
|-
 
|-
|| Hover mouse on graph, axes
+
|| Type '''plot sin (x)'''
|| Limit of '''y axis''' is from -1 to plus 1 and '''x axis''' is from -10 to 10
+
Press '''Enter'''
 +
|| Now type '''plot space sin (x)'''
 +
Note that angle '''x''' is within parentheses.
  
 
|-
 
|-
|| Click on '''grid''' icon
+
|| Point to graphical window
|| Click on the '''grid''' icon. This draws grid in the '''graph'''.
+
|| A graphical window with plot of '''sin(x) '''versus''' x '''appears on the screen.
  
 
|-
 
|-
|| Hold '''control''' and zoom in with mouse '''scroll'''
+
|| Hover mouse on graph, axes.
|| Hold down '''control''' key and zoom in around origin, '''0, 0''', with scroll mouse
+
|| The limit of the '''Y''' axis is from minus 1 to plus 1.
 +
And the '''X '''axis is from -10 to 10.
  
 
|-
 
|-
||  
+
|| Type '''set grid'''
|| Notice, sin x is zero when '''x''' is zero
+
Press '''Enter'''
 +
|| Come back to the '''terminal''' and type the command''' set space grid.'''
  
 
|-
 
|-
|| Show zoom in and out
+
|| Type '''replot'''
|| Using scroll wheel, zoom in and out in the graph.
+
Press '''Enter'''
 +
|| Then type''' replot''' to see the grid in the graph.
  
 
|-
 
|-
|| Type '''reset''', enter
+
|| Hold Ctrl and zoom in with mouse scroll
Type '''replot''', enter
+
|| Hold down the Control key and move the mouse scroll wheel to zoom in and zoom out.
|| To '''reset''', enter command '''reset''' in terminal, followed by '''replot'''
+
Changes in graph becomes visible only after '''replot''' command
+
  
 
|-
 
|-
||  
+
|| Point to '''sin x '''and '''x'''
|| This resets limits of axis as before
+
|| Observe that '''sin x''' is zero when '''x''' is zero.
  
 
|-
 
|-
||  
+
|| Show zoom in and out.
|| Notice the legend in the graph is not clear and runs through the graph
+
|| Let us '''reset''' the limits of the axes as before.
  
 
|-
 
|-
|| Point mouse on legend
+
|| Type '''reset'''
|| Let's increase '''y'''-axis range, so that graph line does not run through legend.
+
Press '''Enter'''
 +
|| To '''reset''', type the '''command reset''' in the '''terminal''', followed by '''replot.'''
  
 
|-
 
|-
|| '''>set yrange [-2:2]'''
+
|| Type '''replot'''
|| Enter command '''set space yrange space''' '''-2 colon 2''' within '''square''' braces
+
Press '''Enter'''
 +
|| Changes in the graph become visible only after executing the '''replot '''command'''.'''
  
 
|-
 
|-
|| >'''set xrange [-20:20]'''
+
|| Point to the '''legend'''
|| Change x-range with command '''set space xrange space -20 colon 20 in square braces'''
+
|| Notice that the '''legend''' is on the top right side in the graph.
 +
It is not clear and intersects the graph.
  
 
|-
 
|-
|| replot
+
|| Point mouse on the '''legend'''.
|| Enter '''replot ''' command to see the updated graph.
+
|| Let's increase '''y axis range''', so that the graph line does not intersect the '''legend.'''
 +
 
 +
|-
 +
|| Type '''set yrange [-2:2]'''
 +
Press '''Enter'''
 +
|| Type '''set space yrange space -2 colon 2''' within square brackets.
 +
 
 +
|-
 +
|| Type '''set xrange [-20:20]'''
 +
Press '''Enter'''
 +
|| I will also change '''x '''axis range'''.'''
 +
Enter command, '''set space xrange space -20 colon 20 '''within square brackets.
 +
 
 +
|-
 +
|| Type '''replot'''
 +
Press '''Enter'''
 +
|| Type '''replot '''to see the updated graph.
  
 
|-
 
|-
Line 126: Line 159:
  
 
|-
 
|-
||  
+
|| Point to the '''legend'''.
|| Legend has moved upward and does not run through the data line.
+
|| The '''legend''' has moved up and does not intersect the data line anymore.
  
 
|-
 
|-
|| '''>set key top left'''
+
|| Type '''set key top left'''
|| Command '''set space key space top space left''' will move legend to top left side.
+
Press '''Enter'''
 +
|| Type '''set space key space top space left''' to move the '''legend''' to the top left side.
  
 
|-
 
|-
|| '''>replot'''
+
|| '''Type replot'''
 +
Press '''Enter'''
 
|| Do not forget to '''replot''' to see the result.
 
|| Do not forget to '''replot''' to see the result.
  
 
|-
 
|-
||  
+
|| Hover mouse next to axis font.
|| Next, let's make the axis fonts larger.
+
|| Next, let's make the axes fonts larger.
I want to set them at size 14 and in '''Verdana''' style, a bit larger than present
+
I want to set them at size 14 and in '''Verdana style'''.
  
 
|-
 
|-
|| '''>set tics font “Verdana, 14”'''  
+
|| Type '''set tics font “Verdana, 14”'''  
'''>replot'''
+
Press '''Enter'''
|| Enter command '''set space tics space font''' '''space''' '''Verdana''' '''comma 14''' in double quotes
+
|| Type this '''command''' as shown here, to change the font size and '''style'''.
 
'''Replot''' to see the result.
 
'''Replot''' to see the result.
  
 
|-
 
|-
||  
+
|| Hover mouse next to axis font.
|| Notice the larger font size in '''Verdana''' style.
+
|| Notice the larger font size in '''Verdana style'''.
  
 
|-
 
|-
|| '''set xlabel 'x (angle)' font ‘Verdna, 14’'''
+
|| Type '''set xlabel 'x (angle)' font “Verdana, 14”'''
|| Let's set x axis label as '''x angle''' next.  
+
Press '''Enter'''
Please follow the commands as seen in the video
+
|| Let's set '''x axis label''' as '''x angle'''.  
Here we are specifying the font style and size in the same command line
+
Type the '''command''' as shown here.
 +
Here we are specifying the '''label''', '''font''' '''style''' and size in a single '''command.'''
  
 
|-
 
|-
|| '''set ylabel 'sin (x)' font ‘Verdana, 14’'''
+
|| Type '''set ylabel 'sin (x)' font “Verdana, 14”'''
|| To set y-axis label as '''sin x''', type command, as seen in the video
+
Press '''Enter'''
 +
|| To set '''y axis label''' as '''sin x''', type the '''command''', as shown here.
  
 
|-
 
|-
||  
+
|| Only narration
|| I will set the same font style and size for y axis too.
+
|| We are using the same font size and style for y axis too.
Changes in graph will become visible only after '''replot''' command.  
+
  
 
|-
 
|-
|| '''>replot'''
+
|| Type '''replot'''
|| Hence type '''replot''' and enter.  
+
Press '''Enter'''
 +
|| Type '''replot''' and press '''Enter'''.  
  
 
|-
 
|-
|| Type '''set key font ",14"'''
+
|| Only narration
|| '''Set space key space font space comma 14 in double quotes''' command sets font size to 14.  
+
|| Now, let’s see how to change the font in the '''legend'''.
 +
 
 +
|-
 +
|| Type '''set key font ",16"'''
 +
Press '''Enter'''
 +
|| Type
 +
'''set space key space font space '''within double quotes '''comma 16 '''
 +
This '''command''' sets the '''legend '''font size to 16.
 +
 
 +
|-
 +
|| Press '''Ctrl+Alt+K'''
 +
|| I will clear the terminal.
  
 
|-
 
|-
 
|| Type''' help fonts'''
 
|| Type''' help fonts'''
|| To access help documentation on fonts, type '''help space fonts'''
+
Press '''Enter'''
 +
|| To access '''help''' documentation on fonts, type '''help space fonts'''.
  
 
|-
 
|-
 
|| Point mouse to '''cairo''', '''gd''' and '''postscript'''
 
|| Point mouse to '''cairo''', '''gd''' and '''postscript'''
|| Help command returns with 3 subtopics '''cairo''', '''gd''' and '''postscript'''
+
|| Help command returns with 3 subtopics '''cairo''', '''gd''' and '''postscript.'''
 +
|-
 +
|| Type '''cairo'''
 +
Press '''Enter'''
 +
|| I will type '''cairo''' to see the details and available options.
  
 
|-
 
|-
|| '''>cairo'''
+
|| Only narration
|| I will type '''cairo''' to see the details and available options
+
|| Similarly you may choose to use '''help''' for any '''command''' when using '''gnuplot'''.
You may pause the video, read & understand the details given in the help section
+
  
 
|-
 
|-
|| '''ctrl c'''
+
|| Mouse in '''Help section'''
'''ctrl l'''
+
|| You may pause the video and read the details given in the '''help''' section.
|| Similarly you may choose to use help for any command when using '''gnuplot'''.
+
Press '''control c''' to exit help section
+
I will clear the screen and bring prompt to top of the screen for clarity.
+
  
 
|-
 
|-
|| '''>set title 'My first gnuplot''''
+
|| Press '''Ctrl C'''
|| I will also add a title, reading, '''my first gnuplot'''.
+
|| Once done, press '''Control '''and '''C''' keys to exit the '''help section.'''
To add title, enter command, '''set space title''' as seen in the '''video'''
+
  
 
|-
 
|-
|| '''>set title font ‘verdana, 18’'''
+
|| '''Ctrl L in Ubuntu'''
'''replot'''
+
'''Ctrl Shift K in Debian'''
|| To set title font, use the command as seen.
+
|| I will clear the screen.
Enter '''replot''' command to see the updated graph
+
I wish to add a title to my graph.
  
 
|-
 
|-
||  
+
|| Type '''set title 'My first gnuplot''''
|| Note that, x axis has dimensions of angle this plot
+
Press '''Enter'''
Hence we may like to label x-axis, in terms of angle, pi
+
|| To add a title, type '''set title '''command, as shown here.
 +
Followed by the title of the graph in single quotes.
  
 
|-
 
|-
|| Point at the terminal
+
|| Type '''set title font "verdana, 18"'''
Highlight the following command and hover mouse over {/symbol p} and -2*pi
+
Press '''Enter'''
'''set xtics ('-4{/Symbol p}' -4*pi,'-3{/Symbol p}' -3*pi,'-2{/Symbol p}' -2*pi, '-{/Symbol p}' -pi, 0, '{/Symbol p}' pi,'2{/Symbol p}' 2*pi, '3{/Symbol p}' 3*pi, '4{/Symbol p}' 4*pi,)'''
+
|| To set the title font, type the command as shown here.
|| To change '''xtics''' to angle, type the following command as seen on screen
+
Here we are setting Greek pi alphabet and set it's position.
+
  
 
|-
 
|-
|| Point mouse to '''('-2{/Symbol p}'''' and then to '''-2*pi'''
+
|| '''Point to x axis'''
|| This command placed label 2 '''pi '''at position 6.28 at x-axis.
+
|| In the '''sinx versus x''' plot, dimensions of '''x axis''' is angle.
 +
Hence we may want to label '''X''' axis in terms of angle, '''π.'''
  
 
|-
 
|-
|| replot
+
|| In the terminal, type,
|| '''X'''-tics is labeled, '''Greek''' symbol '''pi''' is placed at value of 3.14
+
'''set xtics ('-4{/Symbol p}' -4*pi,'-3{/Symbol p}' -3*pi,'-2{/Symbol p}' -2*pi, '-{/Symbol p}' -pi, 0, '{/Symbol p}' pi,'2{/Symbol p}' 2*pi, '3{/Symbol p}' 3*pi, '4{/Symbol p}' 4*pi,)'''
Enter '''replot''' command to see the updated graph
+
'''>enter'''
 +
|| To change '''X ticks''' to angle, type the following '''command''' as shown on the screen.
 +
Here we are using the '''Greek''' alphabet''' pi '''and setting its position.
  
 +
|-
 +
|| Point mouse to '''('2{/Symbol p}'''' and then to '''2*pi'''
 +
|| This '''command''' places, '''Greek symbol pi''', at the value of 3.14.
 +
Label 2'''π''' is at position 6.28 on the '''X''' axis.
 +
 +
|-
 +
|| Point to 2'''π''' and 4'''π'''.
 +
|| Multiples of pi are placed at their respective positions.
 
|-
 
|-
 
|| Type '''help set xtics'''
 
|| Type '''help set xtics'''
'''ctrl-c'''
+
Press '''Enter'''
|| For more help on formats, enter command, '''help space set space xtics'''.
+
|| For more help on the formats of '''X tics''', type the command '''help space set space xtics.'''
Pause the video and explore more if you wish to do so.
+
  
 
|-
 
|-
|| '''ctrl c'''
+
|| Press '''Ctrl C'''
|| Press '''control c''' to exit help
+
|| Press '''Ctrl''' and''' C''' keys to exit the '''help '''section.
It is also possible to add multiple functions on the same graph
+
  
 
|-
 
|-
||  
+
|| I will clear the terminal.
|| To plot '''sin''' x and '''cos''' x on same graph, separate the functions by a '''comma'''
+
  
 
|-
 
|-
|| '''plot sin (x), cos (x)'''
+
|| Only narration
|| Enter command, '''Plot space sin x comma cos x'''  
+
|| It is also possible to add multiple '''functions''' on the same graph.
Make sure, the angle '''x''', is typed within braces
+
Notice the two functions plotted together
+
  
 
|-
 
|-
||  
+
|| Type '''plot sin (x), cos (x) '''
|| Next, add '''sin x''' plus '''cos x''' function to the graph
+
Press '''Enter'''
 +
|| To plot '''sin x '''and '''cos x '''on the same graph, separate the '''functions''' by a comma.
 +
Type '''plot space sin x comma cos x'''
 +
Make sure that the angle''' x''', is within parentheses .
  
 
|-
 
|-
|| '''plot sin(x), cos(x), sin(x) + cos(x)'''
+
|| Point to the graph window
|| For this, type '''plot space sin x comma cos x comma sin x plus cos x'''
+
|| Notice that the two '''functions''' are plotted together now.
Notice the three functions plotted together
+
  
 
|-
 
|-
||  
+
|| Only narration
|| Say, next I want to get '''sin x''' data points''' in a file'''
+
|| Next, we will add '''sin (x) plus cos (x) function''' to the same graph.
  
 
|-
 
|-
|| '''set table'''
+
|| Type '''plot sin(x), cos(x), sin(x) + cos(x) '''
|| To get an '''ascii''' column format output, '''set table''' command can be used
+
Press '''Enter'''
First enter command '''set space table'''
+
|| For this, type '''plot space sin(x) comma cos(x) comma sin(x) plus cos(x)''' .
  
 
|-
 
|-
|| '''plot sin(x)'''
+
|| Point to the graph window
|| Type, '''plot space sinx'''
+
|| Notice that all 3 '''functions''' are plotted together on the same graph now.
An '''x y''' column data for '''sin x''' is generated on the screen
+
Next, let’s learn how to get '''sin(x) data points '''into a file.
  
 
|-
 
|-
|| x y column data is generated on the screen
+
|| Only narration
|| First column is '''x''' data and second column is '''y''' data points of '''x sinx''' graph
+
|| To get an '''ASCII column format''' output, '''set table command '''can be used.
  
 
|-
 
|-
|| Clear screen , ctrl shift k
+
|| Type '''set space table'''
|| We can pipe or 'direct' the output to a text file.
+
Press '''Enter'''
I will clear the screen and bring prompt to top of the screen for clarity.
+
|| First type '''set space table'''.
  
 
|-
 
|-
|| >'''set table "table-sinx.txt"'''  
+
|| '''Enter command plot sin(x) '''and press '''Enter'''
|| For this, type, '''set space table space in double quotes table hyphen sinx dot txt'''
+
|| Then type '''plot space sin(x)'''.
  
 
|-
 
|-
|| '''plot sin(x)'''
+
|| Point to the screen
|| Type '''plot space sin x'''
+
|| An '''x y column data''' for '''sin(x)''' is generated on the screen.
Here, the output is directed to file name '''table hyphen sin x dot txt'''
+
  
 
|-
 
|-
||  
+
|| Highlight first column
|| The file is stored in the directory from where we opened '''gnuplot''' in terminal.
+
Highlight second column
We can remove tabular outputs with unset table command
+
|| The first column is '''x data''' and the second column is''' y data'''.
 +
They are the '''points''' of '''sin''' '''x '''versus '''x''' graph.
  
 
|-
 
|-
|| >'''unset table'''
+
|| Press '''Ctrl+Alt+K'''
|| Enter command '''unset space table''' to unset '''table''' mode
+
|| I will clear the terminal.
 +
We can save the output as a text file.
 +
 
 +
|-
 +
|| Type '''set table "table-sinx.txt"'''.
 +
Press '''Enter'''
 +
|| For this, type '''set space table space '''within double quotes''' sinx dot txt'''
 +
 
 +
 
 +
Here '''sinx dot txt '''is the output filename.
 +
|-
 +
|| Type '''plot sin(x)'''
 +
Press '''Enter'''
 +
 
 +
 
 +
 
 +
|| Then, type '''plot space sin(x)'''.
 +
The output is saved to the file '''sin x dot txt'''.
 +
The data is not seen on the screen,
 +
 
 +
|-
 +
|| Show screenshot of '''Desktop''' showing the file '''sinx.txt'''.
 +
|| The file is stored in the '''directory''' from where we opened '''gnuplot''' in the '''terminal'''.
 +
 
 +
|-
 +
|| Type '''unset table'''
 +
Press '''Enter'''
 +
|| Type '''unset space table''' to exit the tabular output mode.
 +
 
 +
|-
 +
|| Enter '''q'''
 +
|| Then type '''quit''' or '''q''' to quit '''gnuplot.'''
  
 
|-
 
|-
 
|| '''Slide Number 6'''
 
|| '''Slide Number 6'''
 
'''Summary'''
 
'''Summary'''
|| To summarize, in this tutorial, we  
+
|| Let’s summarize.
Learned to
+
In this tutorial, we plotted '''2D''' graphs of '''functions.'''
* Plot 2D graphs of simple functions
+
 
 +
We also
 
* Added grid and set limits
 
* Added grid and set limits
* Zoomed and viewed graph
+
* Zoomed in and zoomed out of the graph
* Added axis labels, graph title  
+
* Added axis labels and graph title  
* Change font style, size
+
* Changed font style and size and  
and
+
* Saved the '''data points''' to a file
* Wrote data points to table
+
 
  
 
|-
 
|-
 
|| '''Slide Number 7'''
 
|| '''Slide Number 7'''
 
'''Assignment'''
 
'''Assignment'''
|| For assignment, plot the following functions,
+
|| As an assignment, plot the following '''functions''':
* tan x  
+
* '''tan x '''
* Legendre polynomial
+
* '''x squared''' and
* a parabola
+
* '''x minus three squared '''
and
+
 
* Open parabola in four different quadrants
+
|-
 +
|| '''Completed assignment'''
 +
|| Your completed assignment should look like this.
  
 
|-
 
|-
 
|| '''Slide Number 8'''
 
|| '''Slide Number 8'''
 
'''Spoken Tutorial Project'''
 
'''Spoken Tutorial Project'''
||This video summarises the Spoken Tutorial Project
+
|| This video summarises the Spoken Tutorial Project  
 
Please download and watch it.
 
Please download and watch it.
  
Line 330: Line 419:
 
|| '''Slide Number 9'''
 
|| '''Slide Number 9'''
 
'''Spoken Tutorial workshops'''
 
'''Spoken Tutorial workshops'''
|| We conduct workshops and give certificates.
+
|| We conduct workshops using spoken tutorials and give certificates.  
 
Please write to us.
 
Please write to us.
  
 
|-
 
|-
 
|| '''Slide Number 10'''
 
|| '''Slide Number 10'''
 +
 
'''Forum for specific questions:'''
 
'''Forum for specific questions:'''
 
|| Post your timed queries in the forum.
 
|| Post your timed queries in the forum.
Line 340: Line 430:
 
|-
 
|-
 
|| '''Slide Number 11'''
 
|| '''Slide Number 11'''
'''Acknowledgement’’’
+
'''AcknowledgemenT'''
 
|| Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
 
|| Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
  
Line 346: Line 436:
 
||  
 
||  
 
|| This is Rani from IIT, Bombay. Thank you for joining.
 
|| This is Rani from IIT, Bombay. Thank you for joining.
 +
 +
|-
 +
|}
  
 
|-
 
|-
 
|}
 
|}

Revision as of 17:31, 25 September 2019

Visual Cue Narration



Slide Number 1

Title Slide Plotting functions

Welcome to the tutorial on Plotting functions in gnuplot.
Slide Number 2

Learning Objectives

In this tutorial we will learn,
  • Plot and replot commands for 2D plotting
  • Plot three functions: sin x, cos x and sin x plus cos x
  • Draw grid and set limits
  • Zoom in and zoom out of a plot


Slide Number 3

Learning Objectives

* Add axis labels and graph title
  • Change font style and size
  • Add Greek symbols
  • Set axis tick marks and
  • Save the data points to a file.


Slide Number 3

System and Software Requirement


To record this tutorial, I am using


  • Debian Linux 9.3
  • gnuplot 5.2.6
  • gedit text editor 3.22
Slide Number 4

Pre-requisites

To follow this tutorial,
  • Learners must be familiar with high school level Mathematics.
  • For pre-requisite gnuplot tutorials, please visit this site.
Press Ctrl Alt T Open a terminal by pressing Ctrl, Alt and T keys simultaneously.
Type cd Desktop and press Enter A terminal opens.

Change directory to Desktop as seen.

>gnuplot

Press Enter

In the terminal, type gnuplot and press Enter.
Hover mouse near gnuplot prompt The prompt changes to gnuplot prompt.

To execute a command in the terminal, press Enter after typing it. I will not mention this separately.

Press Ctrl shift K I will clear the terminal.
Type plot sin (x)

Press Enter

Now type plot space sin (x)

Note that angle x is within parentheses.

Point to graphical window A graphical window with plot of sin(x) versus x appears on the screen.
Hover mouse on graph, axes. The limit of the Y axis is from minus 1 to plus 1.

And the X axis is from -10 to 10.

Type set grid

Press Enter

Come back to the terminal and type the command set space grid.
Type replot

Press Enter

Then type replot to see the grid in the graph.
Hold Ctrl and zoom in with mouse scroll Hold down the Control key and move the mouse scroll wheel to zoom in and zoom out.
Point to sin x and x Observe that sin x is zero when x is zero.
Show zoom in and out. Let us reset the limits of the axes as before.
Type reset

Press Enter

To reset, type the command reset in the terminal, followed by replot.
Type replot

Press Enter

Changes in the graph become visible only after executing the replot command.
Point to the legend Notice that the legend is on the top right side in the graph.

It is not clear and intersects the graph.

Point mouse on the legend. Let's increase y axis range, so that the graph line does not intersect the legend.
Type set yrange [-2:2]

Press Enter

Type set space yrange space -2 colon 2 within square brackets.
Type set xrange [-20:20]

Press Enter

I will also change x axis range.

Enter command, set space xrange space -20 colon 20 within square brackets.

Type replot

Press Enter

Type replot to see the updated graph.
Now the changed graph limits are visible.
Point to the legend. The legend has moved up and does not intersect the data line anymore.
Type set key top left

Press Enter

Type set space key space top space left to move the legend to the top left side.
Type replot

Press Enter

Do not forget to replot to see the result.
Hover mouse next to axis font. Next, let's make the axes fonts larger.

I want to set them at size 14 and in Verdana style.

Type set tics font “Verdana, 14”

Press Enter

Type this command as shown here, to change the font size and style.

Replot to see the result.

Hover mouse next to axis font. Notice the larger font size in Verdana style.
Type set xlabel 'x (angle)' font “Verdana, 14”

Press Enter

Let's set x axis label as x angle.

Type the command as shown here. Here we are specifying the label, font style and size in a single command.

Type set ylabel 'sin (x)' font “Verdana, 14”

Press Enter

To set y axis label as sin x, type the command, as shown here.
Only narration We are using the same font size and style for y axis too.
Type replot

Press Enter

Type replot and press Enter.
Only narration Now, let’s see how to change the font in the legend.
Type set key font ",16"

Press Enter

Type

set space key space font space within double quotes comma 16 This command sets the legend font size to 16.

Press Ctrl+Alt+K I will clear the terminal.
Type help fonts

Press Enter

To access help documentation on fonts, type help space fonts.
Point mouse to cairo, gd and postscript Help command returns with 3 subtopics cairo, gd and postscript.
Type cairo

Press Enter

I will type cairo to see the details and available options.
Only narration Similarly you may choose to use help for any command when using gnuplot.
Mouse in Help section You may pause the video and read the details given in the help section.
Press Ctrl C Once done, press Control and C keys to exit the help section.
Ctrl L in Ubuntu

Ctrl Shift K in Debian

I will clear the screen.

I wish to add a title to my graph.

Type set title 'My first gnuplot'

Press Enter

To add a title, type set title command, as shown here.

Followed by the title of the graph in single quotes.

Type set title font "verdana, 18"

Press Enter

To set the title font, type the command as shown here.
Point to x axis In the sinx versus x plot, dimensions of x axis is angle.

Hence we may want to label X axis in terms of angle, π.

In the terminal, type,

set xtics ('-4{/Symbol p}' -4*pi,'-3{/Symbol p}' -3*pi,'-2{/Symbol p}' -2*pi, '-{/Symbol p}' -pi, 0, '{/Symbol p}' pi,'2{/Symbol p}' 2*pi, '3{/Symbol p}' 3*pi, '4{/Symbol p}' 4*pi,) >enter

To change X ticks to angle, type the following command as shown on the screen.

Here we are using the Greek alphabet pi and setting its position.

Point mouse to ('2{/Symbol p}' and then to 2*pi This command places, Greek symbol pi, at the value of 3.14.

Label 2π is at position 6.28 on the X axis.

Point to 2π and 4π. Multiples of pi are placed at their respective positions.
Type help set xtics

Press Enter

For more help on the formats of X tics, type the command help space set space xtics.
Press Ctrl C Press Ctrl and C keys to exit the help section.
I will clear the terminal.
Only narration It is also possible to add multiple functions on the same graph.
Type plot sin (x), cos (x)

Press Enter

To plot sin x and cos x on the same graph, separate the functions by a comma.

Type plot space sin x comma cos x Make sure that the angle x, is within parentheses .

Point to the graph window Notice that the two functions are plotted together now.
Only narration Next, we will add sin (x) plus cos (x) function to the same graph.
Type plot sin(x), cos(x), sin(x) + cos(x)

Press Enter

For this, type plot space sin(x) comma cos(x) comma sin(x) plus cos(x) .
Point to the graph window Notice that all 3 functions are plotted together on the same graph now.

Next, let’s learn how to get sin(x) data points into a file.

Only narration To get an ASCII column format output, set table command can be used.
Type set space table

Press Enter

First type set space table.
Enter command plot sin(x) and press Enter Then type plot space sin(x).
Point to the screen An x y column data for sin(x) is generated on the screen.
Highlight first column

Highlight second column

The first column is x data and the second column is y data.

They are the points of sin x versus x graph.

Press Ctrl+Alt+K I will clear the terminal.

We can save the output as a text file.

Type set table "table-sinx.txt".

Press Enter

For this, type set space table space within double quotes sinx dot txt


Here sinx dot txt is the output filename.

Type plot sin(x)

Press Enter


Then, type plot space sin(x).

The output is saved to the file sin x dot txt. The data is not seen on the screen,

Show screenshot of Desktop showing the file sinx.txt. The file is stored in the directory from where we opened gnuplot in the terminal.
Type unset table

Press Enter

Type unset space table to exit the tabular output mode.
Enter q Then type quit or q to quit gnuplot.
Slide Number 6

Summary

Let’s summarize.

In this tutorial, we plotted 2D graphs of functions.

We also

  • Added grid and set limits
  • Zoomed in and zoomed out of the graph
  • Added axis labels and graph title
  • Changed font style and size and
  • Saved the data points to a file


Slide Number 7

Assignment

As an assignment, plot the following functions:
  • tan x
  • x squared and
  • x minus three squared
Completed assignment Your completed assignment should look like this.
Slide Number 8

Spoken Tutorial Project

This video summarises the Spoken Tutorial Project

Please download and watch it.

Slide Number 9

Spoken Tutorial workshops

We conduct workshops using spoken tutorials and give certificates.

Please write to us.

Slide Number 10

Forum for specific questions:

Post your timed queries in the forum.
Slide Number 11

AcknowledgemenT

Spoken Tutorial Project is funded by NMEICT, MHRD, Government of India.
This is Rani from IIT, Bombay. Thank you for joining.

Contributors and Content Editors

Madhurig, Ranipv076