Gnuplot/C2/Generate-3D-plots-and-surfaces/English

From Script | Spoken-Tutorial
Revision as of 13:26, 31 December 2019 by Snehalathak (Talk | contribs)

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

Title Slide

Generate 3D plot and surfaces

Welcome to the tutorial on Generate 3D plot and Surfaces.
Slide Number 2

Learning Objectives

In this tutorial we will learn to,
  • Generate a 3D plot with splot command
  • Rotate to change the viewing angle
  • Change the surface properties
  • Color the two sides of the surface differently
Slide Number 3

Learning Objectives

  • Make a graph using script commands
  • Write, edit and execute a script
  • Draw a sphere
  • Change the surface color of the sphere and
  • Generate a heat map
Slide Number 4

System and Software Requirement

  • Ubuntu Linux 16.04 OS
  • Gedit version 3.18 and
  • gnuplot version 5.2.6
Slide Number 5

Pre-requisites

https://spoken-tutorial.org

To follow this tutorial,
  • Learner must be familiar with basics of gnuplot.
  • For pre-requisite gnuplot tutorials, please visit this site.
Press Ctrl+Alt+T. Open a terminal.
Type cd Desktop and press Enter. Change the directory to Desktop.
Type gnuplot and press Enter. I will open gnuplot from Desktop directory in the terminal.
Press Ctrl+L. I will also clear the screen.

We will plot the function, cos x cos y to draw a surface.


Type splot cos(x)*cos(y) and press Enter. Enter the command splot space cos x times cos y.

Make sure x and y are in parenthesis.


Cursor on the graphics window. The splot command generates 3D graph in the graphics window.

Besides x and y, the third axis is the value of the function in the plot.

Hold down left mouse key and turn around and rotate the plot. Hold down the left mouse key.

Move the mouse to rotate the graph and change the viewing angle.

Zoom into the graph. Zoom in slightly into the graph to see a wavy surface.

Notice the surface is plotted in a wide mesh style.

Cursor on the graphics window. Let's change some of the properties of the surface.
Hover mouse over the two sides of the surface. We will color both sides of the surface differently.
Enter the command set hidden3d .

Enter the command replot.

The required command for this is set space hidden3d .

Enter the command replot, to see the changed colors.

Show the two colors on the screen. Notice the hidden surface is colored green and the top surface is purple.
Type set isosamples 50,50 and press Enter. Let’s divide the surface into a fine grid.

Enter the command, set space isosamples space 50 comma 50.

Type replot and press Enter. Replot to notice a fine mesh defining the surface.
Cursor on the graphics window. Use the command, help space splot to know more about the splot command.
Hold down the left mouse key and move the mouse. Hold down the left mouse key and move the mouse to rotate the graph.
Cursor on the view angle value, in the graphics window.

Highlight the changed angle in the graphics window during video editing.

The viewing angle changes in the graphic window while rotating the graph.
Click on Close button to close the graphic window. Next, we will plot a sphere in gnuplot.

Close the graphics window.

Slide number 6

Equation for a Sphere


This function can be plotted in parametric mode as, r.sin(u).cos(v), r.sin(u).sin(v),r.cos(u)

Equation for a sphere with radius r, centered at x0, y0, and z0 is shown here.

It is, x square plus y square plus z square is equal to r square.

This function can be plotted in parametric mode as seen here.

Cursor on the slide. We will write a script to draw a sphere in gnuplot using the text editor.

Then run the script file to plot a graph.

Press Ctrl+Alt+T.

Type gedit and press Enter.

Open another terminal and type gedit to open a gedit window.
Cursor on Gedit text editor. Windows users may use notepad or wordpad software.
Type # my first 3D plot and press Enter. Let's enter a comment on the first line.

I will type hash my first 3D plot and press Enter to start a newline.

The comments start with a hash.

Type the commands in the text editor. Next, please type the commands as shown here.
Type set parametric and press Enter. First, set the plot to parametric mode.
Type set angle degree.

Press Enter.

Then set the angle to degree .
Enter the 2 lines

set urange [0:360]

set vrange [0:180]

and press Enter.

Next, specify the ranges of parameters u and v.
Type set isosample 50,50 and press Enter. Set isosamples to make a fine mesh.
Type set ticslevel 0 and press Enter. Set ticks level to zero.

This sets the XY plane at zero value of Z axis.

Type r=1 and press Enter. I will set the radius of the sphere to one.
Type,

splot r*sin(u)*cos(v), r*sin(u)*sin(v),r*cos(u) and press Enter.

Next add the line, splot space and the function for sphere as seen.
Press Ctrl+ S. Press Control S to save the file.
Give file name, sphere.dem in path desktop folder.

Click on Save.

A dialogue box opens.

I will save the file in Desktop folder with filename sphere.dem.

Click on Save to save the script.

Minimize gedit and go to terminal. Click on the hyphen sign on the top to minimize the gedit window.

Go back to the terminal, where gnuplot is open.

Cursor on terminal. If you had closed gnuplot, please open gnuplot again.

Let's run the script to generate a sphere.

Enter the command load 'sphere.dem' . Enter the command, load space within single quotes sphere dot dem.
Point to the sphere in graphics window. A sphere, centered at zero, zero, zero with radius one appears on the screen.
Rotate the sphere. Let’s change the color of the sphere to blue.

We will edit the script file to make the required changes.

Go to the gedit script . Go to the edit window to edit the script.

If you had closed it, please open the script file to edit.

Add lc rgb 'blue' in the end of the splot command. Add lc space rgb space within single quotes blue.


Press Ctrl+S . Save the changes in the script file.
Come back to gnuplot . Come back to the gnuplot prompt and load the edited script file again.
Type load 'sphere.dem' and press Enter. Enter the command load space within single quotes sphere.dem.

In some systems, pressing the up arrow key scrolls through the command history.

Show the blue sphere. Notice the blue sphere in the graphics window.
Cursor on the graphics window. Next, let's make a heatmap with pm3d command.
Select and delete the part,

lc rgb blue .

Go back to the script in gedit to make changes in it.

Delete the lc space rgb space blue in the splot command.

Type with pm3d at the end of the same line. Add, with space pm3d at the end of the line, as seen on the screen.
Press Ctrl+S, minimize gedit and go to gnuplot. Save the script file, minimize gedit and go to gnuplot.
Enter command load 'sphere.dem' . Load the sphere.dem script again.
Point mouse next to the map guide in graphics window. Notice the heatmap in the sphere.

The color scale bar for the color gradient, is visible on the side.

Hover mouse on the range guide range. This is called a colorbox in gnuplot.

The values between 1 and 0.8 are colored in yellow.

Values between minus 0.8 to minus 1 are colored in deep purple.

Cursor on the graphics window. Using scripts, we can generate and make changes in the graph.
Exit gnuplot and gedit. Let’s exit gnuplot and gedit.

Similar scripts are available in gnuplot demo website.

Cursor on the terminal. Practise and familiarize with commands and styles.

Learners can also use the help command for this purpose.

Type q to quit gnuplot and close gedit. Let's exit gnuplot and gedit.
Slide Number 7

Summary

To summarize, in this tutorial, we,
  • Generated a 3D surface with the splot command
  • Changed the viewing angle
  • Modified the 3D plot
  • Colored the two surfaces of the plot differently
Slide Number 8

Summary

  • Plotted a sphere and changed the color
  • Wrote and modified a script
  • Executed the script to generate a plot and
  • Learned to generate a heat map
Slide Number 9

Assignment

For assignment, plot the following,
  • Plot five spheres in a graph with different radius.
  • Center and color the spheres differently.
Slide Number 10

Assignment

Hint: For a sphere centered at (x1,y1,z1), use the equation for sphere as seen here.

x1+r.sin(u).cos(v), y1+r.sin(u).sin(v), z1+r.cos(u)

For a sphere centered at (x1,y1,z1), use the equation for sphere as seen here.
Show assignment screenshot. Your completed assignment look similar to this.
Slide Number 11

Spoken Tutorial Project

This video summarises the Spoken Tutorial Project.

Please download and watch it.

Slide Number 12

Spoken Tutorial workshops

We conduct workshops and give certificates.

For more details, please write to us.

Slide Number 13

Forum for Specific Questions:

Post your timed queries in the forum.
Slide Number 14

Acknowledgements

Spoken Tutorial Project is funded by MHRD, Government of India.
This is Rani from IIT Bombay.

Thank you for joining.

Contributors and Content Editors

Madhurig, PoojaMoolya, Ranipv076, Snehalathak