Gnuplot/C2/Plotting-functions/English

From Script | Spoken-Tutorial
Revision as of 11:34, 21 February 2019 by Ranipv076 (Talk | contribs)

Jump to: navigation, search
Visual Cue Narration
Slide Number 1

Title Slide Plotting functions

Welcome to the tutorial on Plotting functions
Slide Number 2

Learning Objectives

In this tutorial we will learn,
  • Plot & replot commands for 2D plotting
  • Plot three functions sin x, cos x and sin x plus cos x
  • Draw grid and set limits
  • Zoom into a graph


Slide Number 3

Learning Objectives

  • Add axis labels and graph title
  • Change font style and size
  • Add Greek symbols
  • Set axis tick marks
  • Write data points to a table


Slide Number 4

System and Software Requirement

To record this tutorial, I am using
  • Debian Linux 9.3
  • Gedit text editor 3.22.0 and
  • gnuplot 5.2.5 installed
Slide Number 5

Pre-requisites

To follow this tutorial, learners must be familiar with,
  • Basic computer and internet skills
  • Concept of graphing

and

  • High school level Mathematics skills
Ctrl alt t Open a terminal by pressing control alt t keys together

A terminal opens

>gnuplot. Enter command gnuplot, to open gnuplot
control shift k The terminal prompt changes to gnuplot when program runs

I will clear the screen and bring terminal prompt to top of the screen for clarity.

>plot sin (x) and press enter Type, plot space sin x

Note that angle X is within braces and press enter

A graphical window with plot of x, sin x appear on screen
Hover mouse on graph, axes Limit of y axis is from -1 to plus 1 and x axis is from -10 to 10
Click on grid icon Click on the grid icon. This draws grid in the graph.
Hold control and zoom in with mouse scroll Hold down control key and zoom in around origin, 0, 0, with scroll mouse
Notice, sin x is zero when x is zero
Show zoom in and out Using scroll wheel, zoom in and out in the graph.
Type reset, enter

Type replot, enter

To reset, enter command reset in terminal, followed by replot

Changes in graph becomes visible only after replot command

This resets limits of axis as before
Notice the legend in the graph is not clear and runs through the graph
Point mouse on legend Let's increase y-axis range, so that graph line does not run through legend.
>set yrange [-2:2] Enter command set space yrange space -2 colon 2 within square braces
>set xrange [-20:20] Change x-range with command set space xrange space -20 colon 20 in square braces
replot Enter replot command to see the updated graph.
Now the changed graph limits are visible.
Legend has moved upward and does not run through the data line.
>set key top left Command set space key space top space left will move legend to top left side.
>replot Do not forget to replot to see the result.
Next, let's make the axis fonts larger.

I want to set them at size 14 and in Verdana style, a bit larger than present

>set tics font “Verdana, 14”

>replot

Enter command set space tics space font space Verdana comma 14 in double quotes

Replot to see the result.

Notice the larger font size in Verdana style.
set xlabel 'x (angle)' font ‘Verdna, 14’ Let's set x axis label as x angle next.

Please follow the commands as seen in the video Here we are specifying the font style and size in the same command line

set ylabel 'sin (x)' font ‘Verdana, 14’ To set y-axis label as sin x, type command, as seen in the video
I will set the same font style and size for y axis too.

Changes in graph will become visible only after replot command.

>replot Hence type replot and enter.
Type set key font ",14" Set space key space font space comma 14 in double quotes command sets font size to 14.
Type help fonts 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
>cairo I will type cairo to see the details and available options

You may pause the video, read & understand the details given in the help section

ctrl c

ctrl l

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' I will also add a title, reading, my first gnuplot.

To add title, enter command, set space title as seen in the video

>set title font ‘verdana, 18’

replot

To set title font, use the command as seen.

Enter replot command to see the updated graph

Note that, x axis has dimensions of angle this plot

Hence we may like to label x-axis, in terms of angle, pi

Point at the terminal

Highlight the following command and hover mouse over {/symbol p} and -2*pi 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 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 This command placed label 2 pi at position 6.28 at x-axis.
replot X-tics is labeled, Greek symbol pi is placed at value of 3.14

Enter replot command to see the updated graph

Type help set xtics

ctrl-c

For more help on formats, enter command, help space set space xtics.

Pause the video and explore more if you wish to do so.

ctrl c Press control c to exit help

It is also possible to add multiple functions on the same graph

To plot sin x and cos x on same graph, separate the functions by a comma
plot sin (x), cos (x) Enter command, Plot space sin x comma cos x

Make sure, the angle x, is typed within braces Notice the two functions plotted together

Next, add sin x plus cos x function to the graph
plot sin(x), cos(x), sin(x) + cos(x) For this, type plot space sin x comma cos x comma sin x plus cos x

Notice the three functions plotted together

Say, next I want to get sin x data points in a file
set table To get an ascii column format output, set table command can be used

First enter command set space table

plot sin(x) Type, plot space sinx
An x y column data for sin x is generated on the screen 
x y column data is generated on the screen First column is x data and second column is y data points of x sinx graph
Clear screen , ctrl shift k We can pipe or 'direct' the output to a text file.

I will clear the screen and bring prompt to top of the screen for clarity.

>set table "table-sinx.txt" For this, type, set space table space in double quotes table hyphen sinx dot txt
plot sin(x) Type plot space sin x

Here, the output is directed to file name table hyphen sin x dot txt

The file is stored in the directory from where we opened gnuplot in terminal.

We can remove tabular outputs with unset table command

>unset table Enter command unset space table to unset table mode
Slide Number 6

Summary

To summarize, in this tutorial, we

Learned to

  • Plot 2D graphs of simple functions
  • Added grid and set limits
  • Zoomed and viewed graph
  • Added axis labels, graph title
  • Change font style, size

and

  • Wrote data points to table
Slide Number 7

Assignment

For assignment, plot the following functions,
  • tan x
  • Legendre polynomial
  • a parabola

and

  • Open parabola in four different quadrants
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 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