0% found this document useful (0 votes)
14 views5 pages

Plotlab1 Cbcsstud

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Plotlab1 Cbcsstud

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Visualizing Data with Gnuplot

Roychowdhury & Banerjee

Syllabus
Basic 2D and 3D graph plotting - plotting functions and data files, fitting data using gnuplot’s fit function,
polar and parametric plots, modifying the appearance of graphs, Surface and contour plots, exporting plots.

Page 1 of 5
©Shibaji Banerjee and Suparna Roychowdhury: All rights for circulation, modification, utilization
and (re)distribution of this document or parts thereof lies solely with the copyright holders and
authors of this document.
1 Basic 2D plotting
Hello everyone! This is the beginning of a three year long journey into different aspects and tools in
Computational Physics. We would begin this journey by learning to visualize elementary functions, data
and analyse them through a graphical interface called the “gnuplot ”. This would help in developing an
intuitive understanding of different aspects of elementary functions, data and the information they contain.
In particular, even if rigorous model building might be one’s ultimate goal, graphical methods still need to
be the first step, so that a sense for the data, its behaviour, and quality is developed. As you would realize
later, this would prove to an extremely important gadget which you can pick and safe-keep for your future
endeavours in Physics or any form of scientific pursuit.
gnuplot is a program for exploring data graphically. Its purpose is to generate plots and graphs from
data or fun ctions. It can produce highly polished graphs, suitable for publication, and throw-away graphs,
when you’re merely playing with an idea. gnuplot is command-line driven: you issue commands at a prompt,
and gnuplot will draw the current plot in response. gnuplot is also interactive: the output is generated and
displayed immediately in an output window.

1.1 Invoking gnuplot


If gnuplot is installed on your system, it can usually be invoked by issuing the command:
gnuplot
at the shell prompt. Once launched, a gnuplot window opens up which displays a welcome message finally
leading to a prompt showing
gnuplot>
Anything entered at this prompt will be interpreted as gnuplot commands until you issue an exit or quit
command, or type an end-of-file (EOF) character, usually by hitting Control-D. Probably the simplest
plotting command we can issue is:
gnuplot> plot sin(x)
To plot the function gnuplot generates some datapoints by uniformly sampling 100 x points from the range
(-10 to 10). The number of such points determine how accurately the graph is drawn at the expense of
plotting speed and a thicker appearance although none may be apparent. Both the default range and the
number of sampling points can be changed. We begin by introducing you to a set of commands which can
be issued at the gnuplot prompt to visualize elementary functions in different ways and to choose suitable
domain and range according to user specific needs. Check all these commands specified under the heading
“Facts” and try simple variations to get a feel and hold for the language.

1.2 Facts
1. To plot a single function f (x)

gnuplot> plot f(x)

The function f (x) can be an expression containing a combination of well known functions or defined
in a preceding line as in

gnuplot> f(x)=sin(x)+cos(x)*exp(-x)

Note that the operator to raise a function to a power is **. If your function does not work for some
reason, check first for an omitted multiplication operator.
2. To plot two functions f (x) and g(x) together,

©Shibaji Banerjee and Suparna Roychowdhury, See Page 1 for full text of copyright notice Page 2 of 5
gnuplot> plot f(x),g(x)

3. As you might have noticed, the default x-range of gnuplot is (-10,10) and the y-range is selected
according to the values of the function. However, these ranges can be altered and set according to your
choice by issuing

gnuplot> set xrange [-3:3]


gnuplot> set yrange [0:10]

before the plot command.


4. To set the x-y axes, say

gnuplot> set zeroaxis

5. To change the number of samples, go for, say

gnuplot> set samples 1000

You would need this when the curve has sharp bends and / display vertical asymptotes. See Ex. 1.(c)
6. The width of a line can be changed using specifying a relative value after the individual plot command
(a trace). Thus

gnuplot> plot sin(x) lw 2, cos(x) lw 1

Will plot the sine function with a 2x thicker line compared to the cosine curve. The color of a line can
be changed using the option “lc” as in

gnuplot> plot sin(x) lw 2 lc ’red’

7. The title of an individual trace can be changed from the automatic legend by specifying the title as a
string value as in

gnuplot> plot sin(x) lw 2 title ’The Sinusoid’

Note that individual options are separated by the space character and not the comma. The comma
separates individual traces.
8. If we want to mark some points on a curve, these points can be defined as a block variable and plotted
with the curve. Suppose we want to mark the two consecutive maxima of a sin function on the function
itself. The following code achieves this.

gnuplot> $data << EOD


1.57 1.00
7.85 1.00
EOD
gnuplot> set xrange [0:10]
gnuplot> plot sin(x) lw 2, ’$data’ ps 4

The variable $data reads and remembers the values given in the lines following its definition. It goes
on reading till it encounters the name EOD occuring all by itself on a line. When we plot this variable
in a separate trace just after the sin function, the two data-points are plotted with a relative size of 4
(so ps stands for pointsize) over the curve. The variable $data is a rather special variable ; ordinary
variables without the dollar prefix (like a,b,c, ndata etc) can be defined and used in gnuplot with a
numerical or string data-value (as in a=4 followed by f(x)=x**a) just like variables in other computer
languages. Some variables like pi are already defined. You can define and use others, like e=exp(1)
followed by print e which displays its value on the terminal.

©Shibaji Banerjee and Suparna Roychowdhury, See Page 1 for full text of copyright notice Page 3 of 5
9. Multiple plot windows can remain open together, but only the last one is active; windows cannot
be switched. One of these windows can be the “test” window which showcases the properties of the
current terminal.

gnuplot> set term win enh 0


gnuplot> test
gnuplot> set term wxt enh 1
plot sin(x)

The “win” terminal would work on the windows platform even if the default terminal “wxt” does not
work for some reason. It is good if it works since png pictures of graphs can be generated (screenshots
are not a lot presentable let alone publication ready!) directly from this terminal.
10. It is frequently useful to show the axis for which x = 0 and y = 0: For these cases say set zeroaxis
before plotting anything. Lookup help set zeroaxis for more details.
11. If you write down the commands one after another in a text file, the file can be ‘loaded’ each time you
decide to change something about the plot. This is the preferred way to run gnuplot, as it encourages
both graphical experiments and generation of publication quality graphs with a high degree of finish.
Assuming such a file (myplot_plt.txt) is present in the current working directory, it can loaded
(repeatedly) using,

gnuplot> load ’myplot_plt.txt’

Long lines in the file can be split by putting a backslash at a point where it needs to be continued
to the next line. A good place to break such lines is just aftert the comma character which serves to
separate the individual traces, e.g

plot sin(x) lw 3,\


cos(x)

12. Note that you can say reset at the gnuplot prompt to make it forget almost anything about the
changes in settings (like xrange or zeroaxis) you have made. If all else fails, exit and restart gnuplot

In the following exercises, we will study elementary functions and their combinations. As discussed in an
accompanying lecture, it is quite easy to acquire a rough idea of the shape of a function checking whether it
has any kind of symmetry and by adding, multiplying and dividing graphs and finding out where they cross
an axis or blow up. In the following exercises, you would first predict the form of the function, and then
plot it using Gnuplot to verify your ideas. Refer to the “Facts” first if you hit a roadblock.

Exercise 1 For all exercises outline your logic leading to the construction of the curve. Then make a freehand
sketch and finally compare it with gnuplot output.
(a) x + sin x in (-2π,+2π) Instructions: Argue and show with a sketch how this curve is resulting out of a translation
of the curve sin x by x. Overlay your plot of the final curve on y = x to show this feature.

(b) x2 +x+1, x ∈ (−3, 3) Instructions: show the zero y axis. Display the point of intersection of the graph with the y
axis and the point of minima on the curve (Write down the expression as a complete square + something first in the
analysis part). Choose lw 2 for the function and pt 7 and pt 5 for the function and the two datapoints. Choose
large points. Change the titles of the curve to “Function” and the data-points to “Y-intercept” and “Minima”
respectively.

(c) x(x2 − 1) Instructions: mark the origin and the other roots of this function.

©Shibaji Banerjee and Suparna Roychowdhury, See Page 1 for full text of copyright notice Page 4 of 5
x2
(d) , y ∈ (−10, 10) Instructions: set the zeroaxis. Set the number of samples to about 400. This would take
x−1
care of the discontinuity of the graph. Show the two asymptotic lines to this curve. To show the vertical asymptote
make use of the set arrow command (look up help)

(e) (ax)2 e−ax , x ∈ (0, 10) where a = 1, 2, 3 All three traces should appear on the same plot and colored red,
green and blue. Find the approximate maxima of the three curves by identifying the points using the mouse
and double clicking on them to copy the coordinates to the clipboard. Paste these coordinates before the
plot commands and design arrow commands to display vertical lines connecting the highest point on the
curves to their respective bases.
The next exercise would focus more on inspecting graphs and extracting information from them. Appearances
are of minor concern. The following Facts would tell you how to zoom on a part of a graph and set up a
ruler on top of a graph for taking measurements.

1.3 Facts
1. To zoom manually over a part of a graph, change its axis limits.
2. Place the mouse cursor on a part of the curve you are about to zoom. Then keep CTRL pressed and
roll the mouse wheel. This works for both the win and wxt terminals. Press ‘u’ to get back to the
original, un-zoomed level.
3. Drag with the RMB to draw a zoom rectangle. Now click with the RMB to zoom.
4. Wheel / Shift wheel to PAN in the x or y direction.
5. Double click on a point to copy its coordinate to the CLIPBOARD. You can paste back the coordinates
to the gnuplot workplace or a text file as long as it is there on the clipboard.
6. To mark a point on the graph with its coordinates, Press down with the wheel on that point. These
labels are not permanant; they get lost over a zooming operation. To place permanent labels say
set mouse labels before pressing down with the wheel Even permanant labels are erasable,
Just keep CTRL pressed while pressing down with the wheel again on the same point.

7. Press ‘r’ over a point to display a vertical ruler at that point. Press ‘r’ again to turn it off. While the
ruler is on, the distance in user units from the ruler origin to the mouse is displayed continuously.

Exercise 2 (a) Plot the graph of x3 + 1 − exp(x) over x = −8 . . . 8. Select suitable y range to show all the
four x intercepts. [Set the zeroaxis, set manually the yrange to be [-5:5] ]

(b) Plot 3x4 − 6x2 over the domain [-10,10] with automatic y scaling. After observing the graph, edit the
domain and range so that you can see the x-intercepts clearly. Estimate the x-intercepts with the mouse
cursor.
(c) Define the functions g(x) = 5 exp(−0.5x) and h(x) = x + 1, then do the following. Plot a graph that
shows both functions g(x) and h(x). Experiment with different values for domain and range. Estimate the
coordinates of the point of intersection of these two graphs by using left mouse-button click. Check the
answer by any calculator. You can also use gnuplot ’s print command to check the answer.
(d) Define the function k(x) = x + 3 sin(2x). Plot the graph of this function on the domain [-1,8]. Modify
your plot to include the horizontal line y = 4. Use this new plot to estimate the number and approximate
values for x such that k(x) = 4.

(e) Plot exp(−x/100) cos x with a suitable sampling rate to plot a smooth curve. Set the domain to [0:100].
Examine with different sampling rates.

©Shibaji Banerjee and Suparna Roychowdhury, See Page 1 for full text of copyright notice Page 5 of 5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy