How to create conditional plotting with Gnuplot - Open Source - Techguide

How to create conditional plotting with Gnuplot

 

Summary

Marco Fioretti goes over the finer points of using Gnuplot to design graphs based on mathematical functions and tabular data using conditional plotting.

Events

Echelon 2012
June 11 and 12, 2012

University Cultural Centre, National University of Singapore

Startup Asia Jakarta 2012
June 7 and 8, 2012

12th Floor, Annex Building, Wisma Nusantara Complex, Jl. M.H. Thamrin No. 59 Jakarta 10350, Indonesia

MMA Forum Singapore
April 23-25, 2012

Grand Hyatt Singapore

A couple of months ago, I shared one of the reasons why I like the command line plotting utility Gnuplot: its ability to understand and display in a very readable way time-based data.

This time around I'm going to spend a few words to introduce another Gnuplot feature that makes it an essential tool for everybody who needs automatic but flexible plotting.

As you probably already know, Gnuplot can plot both mathematical functions and tabular data from ASCII files. If, for example, you have these numbers in a file called simpledata.txt:

  1 -3    15
  2 -2    14
  3 -1.5  10
  4  3    -9.5
  5  2.75 -2.58

The following command:

  plot [1:5][:] "simpledata.txt" using 1:2 w l t "2nd column of data file", -2*(cos(3*x)*x) +1 w l

generates the diagram in Figure A.

Figure A

In that plot, the read line is drawn joining the points defined by the first two columns of the file: the first number of each row becomes the horizontal coordinate x, and the second the vertical one, y. The green curve, instead, is the graphical representation of the function -2*(cos(3*x)*x) +1.

Figure A shows the simplest kind of graphs that Gnuplot can draw. As cool as they are, they also are sort of rigid. Once you have written something like "using 1:2″, the corresponding curve on the graph (the red one in Figure A) will use all the content of those columns...and nothing else, for the whole plot. For the same reason, the green curve will always and only show the path of that one function.

Luckily, Gnuplot can be much more flexible than this, thanks to conditional plotting. Figure B shows how the previous diagram changes if you modify the Gnuplot command above as follows:

  plot [1:5][:] "simpledata.txt" using 1:($1 <=3  ? $2 : 1/0) w l t "2nd column of data file",
  (x > 3) ? -2*(cos(3*x)*x) +1 : 1/0 w l


Figure B

The trick is all in the conditional, C-like statements. You must put them right in the places in which you tell Gnuplot which columns (or functions) to use as coordinates for the vertical axis. "1/0″ is sort of a Gnuplot keyword to say "nothing." In this snippet of code:

  ($1 <=3  ? $2 : 1/0)

It means "if the current number in the first column ($1) is less than 3, use the second column ($2), otherwise draw nothing". Similarly, the second condition tells Gnuplot to draw the associated function only for values of x greater than 3.

This is the basic Gnuplot conditional plotting trick. Of course, the conditional statements can be nested. In practice, you can use them in several ways. Combining two or more mathematical functions, expressed by formulas, in the single curve of Figure C it's quick:

gnuplot> plot [1:5][:] (x <3) ? -2*(cos(3*x)*x) +1 : (x >=3 && x <4) ? -7*tan(30*x) : 3*x**3 w l t "One curve made of 3 formulas"

Figure C

The syntax for conditional plotting of tabular data is almost identical. Figure D plots column 2 until x is equal or greater to 3, then switches to column 3:

  plot [1:5][:] "simpledata.txt" using 1:(($1 <=3)  ? $2 : $3) w l t "2 columns of the same data file"

Figure D


What if you wanted to combine in one curve, using this trick, numbers from two different data files, or from one data file and one function? Looking again at Figure B shows that we'd need something more to make the result appear like one curve. Namely, we should tell Gnuplot to always use the same color for the two plots, by adding to each instruction the option lt N, where N is the Gnuplot numeric code for plot colors. To know which color corresponds to each value, type "test" at the Gnuplot prompt.

There is another issue that you need to take into account when playing with conditional plotting. You can see what I am talking about by comparing Figure C with Figure E, which I generated plotting only the [2.9:4.1][-20:400] area to make the diagram more readable:

gnuplot> plot [2.9:4.1][-20:200] -2*(cos(3*x)*x) +1 w l t "First curve",  -7*tan(30*x) w l t "Second curve", 3*x**3 w l t "Third curve"

Figure E

Figure E contains three distinct curves. Each one of them corresponds to one, and only one of the three functions that in Figure C were combined in one single line. If we compare the two diagrams, we'll immediately see that those functions do not have the same values in the points in which we switch from one to another.

What happened? It's simple: when Gnuplot arrives to changing points (x=3 and x=4 in our case) it adds extra straight segments, that connect the last y value of one function with the first y value of the next one.

You just need to know this in advance, in order to have Gnuplot plot exactly what you want. If and when you actually want to draw two separated curves, you must explicitly tell this to Gnuplot. When that's the case, use two conditional controls:

  gnuplot > plot [3.9:4.1][:] ( x <4) ? -7*tan(30*x) : 1/0, (x < 4) ? 1/0 : 3*x**3 w l

This tells Gnuplot to draw two independent curves.

Marco Fioretti is a freelance writer and teacher whose work focuses on open digital technologies.

Talkback

Add your opinion

In order to post a comment, you need to be registered. (Sign In or register below)

Post your comment

ZDNet Asia Live

Alibaba seeks $2.3B from shareholders for Yahoo deal http://t.co/ySyCwLvJ via @zdnetasia

Rise in Chinese-funded acquisitions could trigger more hurdles: By Ellyne Phneah , ZDNet Asia on May 22, 2012 (6... http://t.co/L4QzDq3H

Kodak loses patent ruling against Apple, RIM - ZDNet Asia http://t.co/O7P8U2Ya

Rise in Chinese-funded acquisitions could trigger more hurdles - ZDNet Asia http://t.co/WJCfhWLs

Kodak loses patent ruling against Apple, RIM. http://t.co/N1j7aZ6o

#radio Radio Serbia by EnjoyIT 1.0 http://t.co/nGQFvX2E

Rise in <b>Chinese</b>-funded acquisitions could trigger more hurdles http://t.co/0pXBS1HR

Rise in Chinese-funded acquisitions could trigger more hurdles: By Ellyne Phneah , ZDNet Asia on May 22, 2012 (6... http://t.co/W3SOdw2c

RT @zdnetasia: CFOs increasingly involved in IT investment decisions. http://t.co/8QrfwOSb

CFOs increasingly involved in IT investment decisions http://t.co/XD1LerFq via @zdnetasia #PrivateCloud #SC2012 #CAPEX

Rise in Chinese-funded acquisitions could trigger more hurdles. http://t.co/VC3G3m3o

RT @zdnetasia: Rise in Chinese-funded acquisitions could trigger more hurdles. http://t.co/VC3G3m3o

So much as we know , MTK6575 extremely integrated frequency1GHz ARM Cortex-A9 processor, the superiority of 3G / HSPA Modem, and help the...

2 hours ago by y15822137359 on 5 SaaS adoption speed bumps to avoid

Rise in Chinese-funded acquisitions could trigger more hurdles - ZDNet Asia: Rise in Chinese-funded acquisitions... http://t.co/bZaAQnRL

Rise in Chinese-funded acquisitions could trigger more hurdles http://t.co/mIsuZjnU http://t.co/erFX4aVv #arcavir

http://t.co/VNaZtseV Rise in Chinese-funded acquisitions could trigger more hurdles: "Cash r... http://t.co/N0gZZEdR http://t.co/wiqY9ktt

I reckon your view: "CRM is strategy, not software", if a company replicating the approach uses in ERP implementation into CRM, what they...

1 day ago by wykoong on Gartner: Mobile CRM gives better ROI than social

This video will teach you about the Excel fill handle but also provide you with a workook to download... http://www.youtube.com/watch?v=...

1 day ago by TradeBrother on A quick fill handle trick for Microsoft Excel

waiting...

3 days ago by eapete on What should count in a company's market value?

Boy, you've opened a can of worms now.

Wait for the rants & raves.

3 days ago by eapete on What should count in a company's market value?

I was puzzling before this whether to replicate the success formula we executed for a financial institute, and come out with a standard s...

4 days ago by wykoong on Drop the egos, copy ideas, then innovate