gnuplot


Making an animated torus in gnuplot and gimp

The gnuplot code in this article, creates several frames of a 3D torus with a visible structural grid from different viewing angles, like the one in the image below.

[download id=”3549″]

In geometry, a torus (plural tori) is a surface of revolution generated by revolving a circle in three-dimensional space about an axis coplanar with the circle. If the axis of revolution does not touch the circle, the surface has a ring shape and is called a torus of revolution.

From Wikipedia https://en.wikipedia.org/wiki/Torus

By modifying the circles and rings variables in the script you can increase and decrease the number of circles and rings that appear on your torus.
The above image was generated using 60 circles and 30 rings.

To better explain what a circle and what a ring is, please have a look at the following two examples.

The following torus has 10 circles and 30 rings.

The next one has 60 circles and 10 rings.

The last torus has 10 circles and 10 rings.

Using the script, you can also play around with the set view command and the for loop to change the number of frames that will be produced and what movement should the camera perform before printing.

[download id=”3549″]

set terminal pngcairo transparent enhanced font "arial,10" fontscale 1.0 size 700, 400

unset key
unset border
unset tics

set dummy u, v

circles=60
rings=30

set parametric
set isosamples circles, rings
set hidden3d back offset 1 trianglepattern 3 undefined 1 altdiagonal bentover
set urange [ -pi : pi ] noreverse nowriteback
set vrange [ -pi : pi ] noreverse nowriteback

do for [i=1:360/circles] {
 set view 25, i, 1, 1
 set output sprintf('game-of-life-torus.%03.0f.png', i)
 splot cos(u)+.5*cos(u)*cos(v),sin(u)+.5*sin(u)*cos(v),.5*sin(v) with lines
}

[download id=”3549″]

Below, you can see two examples of animation we build together using the gimp application.

Showing one frame at a time.

Accumulating all frames into one, until the loop is reset.

To create the animation we followed the next steps:

  1. Executed gnuplot torus.gnuplot to generated the frames.
  2. Then we started the gimp and from the menu File we chose the option Open as layers... (Ctrl + Alt + o).
  3. We selected all the images we wanted for our animation and pressed the Open button.
  4. The Layers - Brushes window got populated by the new frames. Please note that window you can use that to change the order of the frames (which are now the layers).
  5. Following, from the menu File we chose the option Export As... (Shift + Ctrl + E), from the pop-up window we selected the type of the file to be gif and pressed the Export button.
  6. At the final pop-up window, we enabled the As animation checkbox, then at the Frame disposal where unspecified dropdown menu we chose One frame per layer (replace) and hit the Export button which produce the first animation in this article (two images up).

This figure shows the ‘Layers – Brushes’ window that got populated by the new frames.

From the menu File we chose the option Export As… (Shift + Ctrl + E), from the pop-up window we selected the type of the file to be ‘gif’ and pressed the Export button.

At the final pop-up window, we enabled the As animation checkbox, then at the Frame disposal where unspecified dropdown menu we chose One frame per layer (replace) and hit the Export button which produce the first animation in this article (two images up).

 


[GnuPlot] Create a plot with Date Time on X axis

Download here : [download id=”1309″]

The following script, will read the ‘data.csv’ file and create a plot with the data.
The input file (data.csv) should have two columns: the first one is a date/time string and the second is a number.
e.g

2016-01-19 13:12:38,2
2016-01-19 13:12:38,1
2016-01-19 13:12:38,0
2016-01-19 13:12:40,0
2016-01-19 13:12:41,0
2016-01-19 13:12:47,0

The chart will have on the X axis the date/time and on the Y axis the value of the second column.
All values under the 0 axis will be green and all values above it will be red.

Example output:

data.csv

Execution example: gnuplot -e "filename='data.csv'; width=2000; height=1000;" timeDifference.plot

#Setting output to be a PNG file of size 'width'x'height'
#'width' and 'height' are set from the command line. e.g gnuplot -e "filename='server_1.csv'; width=10000; height=500;" timeDifference.plot
#Setting the font of all text to be 'Verdana' size 8
set terminal pngcairo size width,height enhanced font 'Verdana,8'
#Setting the output filename to be the same as the input filename with the .png extension appended to it.
set output filename.'.png'

#We set the file separator to be the comma, this way we inform the engine that we will be processing a CSV file
set datafile separator ","

#Informing the engine that the X axis of our plot will be date/time data type
set xdata time
#We define how the date/time input must be parsed. In this example we expect the input to be like '2016-01-19 14:25:00'
set timefmt '%Y-%m-%d %H:%M:%S'

#We set the output format that will be shown on the X axis. Here we expect to show '19-01 New Line 14:25"
set format x "%d-%m\n%H:%M"
#Set the X axis label
set xlabel "Event Time"
#Set the Y axis label
set ylabel "Time Difference" 

#Enabling the Grid, this way major tick lines will be visible on the chart
set grid

#As we expect to have negative values as well, we make the zero Y axis line is thicker and has a different style from the rest so that it will be easier to spot
set xzeroaxis linetype 3 linewidth 1.5

#Creating a style for the lines that will be used in the plot. Type = 1, Color = green, Width = 1
set style line 1 linetype 1 linecolor rgb "green" linewidth 1.000
#Creating a style for the lines that will be used in the plot. Type = 1, Color = red, Width = 1
set style line 2 linetype 1 linecolor rgb "red" linewidth 1.000

#Actual plot command
#It directs the engine to plot the file that is in the filename variable, use the first and second column and use vertical columns with the styles we described above
#First line, We will plot only values that are greater or equal to 0, the rest we give 1/0 which is an invalid number and will not be plotted
#Second line, We will plot only values that are strictly less than 0, the rest we give 1/0 which is an invalid number and will not be plotted
plot filename using 1:($2 <= 0?$2:1/0) with impulses ls 1 notitle,\
filename using 1:($2 <= 0?1/0:$2) with impulses ls 2 notitle