Gnuplot/C4/Matrices-and-Contours/English

From Script | Spoken-Tutorial
Revision as of 19:08, 8 December 2020 by Ranipv076 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
Slide Number 1

Title Slide

Welcome to the tutorial on Matrices and Contours
Slide Number 2

Learning Objectives

In this tutorial, we will
  • Plot a 2D matrix heatmap
  • Read file headers for axis tics
  • Plot a heat map for the matrix
  • Add contours in a 3D plot and
  • Label the contours

Slide Number 3

System and Software Requirement

To record this tutorial, I am using
  • Ubuntu Linux v 16.04 OS
  • gnuplot 5.2.6 and
  • Gedit 3.18

Slide Number 4

Pre-requisites

https://www.spoken-tutorial.org

To follow this tutorial,
  • Learners must be familiar with the basics of gnuplot.
  • Please visit this site for pre-requisite tutorials.
Slide Number 5

Code files

* Files used in this tutorial are provided in the Code files link of this tutorial.
  • Please download and extract them.
  • Make a copy and then use them while practising.
Screenshot of Desktop showing input file. I have saved the input file results.txt on Desktop.

Let’s check file format by opening the file in gedit text editor.

You may use the editor of your choice.

Show results.txt file in text editor.

Point to headers and matrix elements.

The first row is row header.

The first row is headers referring to students from S1 to S20.

First column is also a header. It lists the subjects they appeared for exam.

The matrix elements are the marks obtained.

Close gedit. Let’s close the text editor.
Press Ctrl+Alt+T. I will also open a terminal.
Type cd Desktop and press Enter. Change the directory to desktop.
Enter the command gnuplot. Open gnuplot from the terminal.
Press Ctrl+L. I will also clear the screen.
Type,

plot 'results.txt' matrix rowheaders columnheaders with image

and press Enter.

Enter the plot command as seen on the screen.

Here, the matrix name results.txt, is in single quotes.

Use the syntax and whitespaces as seen on the screen for the command.

Follow it with matrix, rowheaders, column headers.

Specify to plot with image and press Enter.

Show graphics window. In the graphics window, a 2D heatmap image of the matrix appears.
Hover mouse over x and y tics The x and y axes tics are represented by strings.

They are the student names.

The subjects which they have appeared for examination is also seen.

Cursor on the color scale bar. Notice the palette setting is indicated in the color scale bar.
Hover mouse over color scale bar. The color palette can be customized as desired.

Let’s change to a custom palette.

Type,

set palette defined (0 "dark-violet", 1 "sandybrown", 2 "grey80", 3 "blue", 4 0.5 1 0.5, 5 1 1 0, 6 1 0.5 0, 7 1 0 0, 8 0.5 0 0) and press Enter.

Now I will define a new color palette.

Type the following command and press Enter.

Hover mouse over 2 “grey80” and 5 1 1 0 . Here, I define the color for each range in the colorbar.
Type, replot and

press Enter.

Replot to see the results.

You may explore more on color palette options if desired.

Write on the Screen, Use the command, help color palette. Use the command, help color palette for learning further.
Go to gnuplot terminal prompt. Go back to the terminal.
Type help palette and press Enter. Use the command help palette to learn more about palette setting.
Press Ctrl+L. I will clear the screen.
Cursor on the graphics window. Let’s now plot contours and label the contours in a 3D plot.

I will open gedit text editor to write a short script.

Cursor on gedit text window. You may open the text editor of your choice.
Type #contour plot and labels

and press Enter.

First, write a comment line, starting with a hash, as seen here.

Next let's incorporate contours in the plot.

Type, set contour base and press Enter. Then set the contour base with the command, set contour base.

Set contour command is used for grid type data and is used with splot command.

Here, base specifies that, the contours are drawn on the grid base.

Type, set isosamples 50,50 and press Enter. Set the grid for isosamples to 50 comma 50.
Type, set style increment default and press Enter. Set the increment to default value.

This increments successive line styles in the graph.

Type, set cntrparam levels auto 10 and press Enter. This command determines the z values for which the contours are to be drawn.

We can also specify the way contours are drawn.

Let’s also set the contour levels. I will set it to autoscale.

Type, set cntrlabel format '%8.3g' font ',7' start 5 interval 20 and press Enter. Now, specify a desired font for the contour label, start and the interval.
Type, set style data lines and press Enter. Set the style to lines for the contours.
Type set autoscale and press Enter. Set axis range for x and y axis as seen here.
Type, splot x**2+y**2 with lines notitle , x**2+y**2 with labels and press Enter. Now, let’s draw the plot with splot command.
Press Ctrl+S. I will save the file. Press Ctrl+S.
Give file name contour.dem in Desktop directory.

Click on Save.

I will give file name contour.dem.


Save the file in Desktop directory.

Minimize gedit and to the terminal. Minimize gedit and go to the terminal.
Type, cd Desktop and press Enter. I will change the directory to Desktop.
Type, gnuplot and press Enter.

Press Ctrl+L.

I will also open gnuplot and clear the terminal.
Type, load 'contour.dem' and press Enter. Let’s load the script file with the load command as seen on the screen.
Cursor on the graphics window, on the surface. The graphics window opens.

Notice the surface plot.

Cursor on the contours. The contours are seen on the base as we specified in the script file.

They are also labeled.

Let’s modify the script file to write the 3D data to a table.

Close the graphics window. Close the graphics window.
Go to the script file. Go back to the script file which is open in gedit text editor.
Type, pause -1 'Press enter to continue' and press Enter. I will add a pause command as seen on the screen.
Type, set table 'contour.dat' and press Enter. Next set table and specify an output file name.
Type, splot x**2+y**2 and press Enter. Let’s splot the function as seen.

This will generate data points in the specified file name.

Type, unset table and press Enter. Unset the table as seen on the screen.
Cursor on the graphics window. Usually the plot will appear small if we splot the function directly.
Type,

set xrange [-9:9]

set yrange [-9:9] and press Enter.

I will also set x and y axis range.
Type, plot 'table-contour.txt' using 1:2:3 with lines, u 1:2:3 every 50 w labels notitle and press Enter. Next, plot the contours with the 2D plot command as seen.
Save the file, minimize gedit and go to the terminal. Save the file, minimize gedit and go to the terminal.
Enter the command gnuplot. Open gnuplot from the terminal.
Enter command load 'contour.dem' . Load the script file as seen on the screen.
Cursor on the graphics window. Notice the first plot which we saw earlier.
Go to the terminal and press Enter. Go to the terminal and press Enter.
Cursor on the graphics window. Notice the 2D contour plot.
Cursor on the graphics window. Learner is encouraged to lookup the details for dgrid3d command.

Use the help command in terminal for this purpose.

Close the graphics window. Close the graphics window.
Type help dgrid3d and press Enter. In the terminal type help dgrid3d to access the help documentation on it.
Press Ctrl+C. Learner may pause the video and explore the details.

Let’s exit the help section by pressing Control-C.

Type q and press Enter. Enter the command q or quit to quit gnuplot.
Close the gedit window. Close the gedit window.
Slide Number 6

Summary

Now let's summarize.

In this tutorial, we learned,

  • About a 2D matrix
  • Plotted the data in the 2D matrix as heatmap
  • Plotted, a 3D function with contours on the base with splot command
  • Labeled the contours
  • Plotted the contours of the 3D function in a 2D plot
Slide Number 7

Assignment

For assignment activity, please do the following.

For the function x2*y2,

  • Change rgbformulae setting to change colors
  • Plot contours with set contour surface and with set base both
  • Notice the position of the contours that is plotted.
Assignment picture The completed assignment may look similar to 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

The Spoken Tutorial Project Team
  • conducts workshops and
  • gives certificates.

For more details, 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 MHRD, Government of India.
This is Rani from IIT, Bombay.

Thank you for joining.

Contributors and Content Editors

Ranipv076