Showing posts with label gnuplot. Show all posts
Showing posts with label gnuplot. Show all posts

Saturday, January 26, 2013

Real-time plot updates using Gnuplot

Sometimes it's very convenient to see real time updates of your gnuplot graphs, for example if you're doing Monte Carlo simulations or if you have are monitoring stocks. Below I'll show you the way I do it, by only using gnuplot commands.

First, create a gnuplot configuration file called 'loop.plt', containing:

pause 2
replot
reread
Let's assume we have already given gnuplot the commands to plot something. What this piece of configuration file then does, is wait two seconds, then replot and then reread the loop.plt file. This will cause these three commands to be executed in an infinite loop. Reread is a very useful command. You can read more about it here.

Now, how do we give the first plot command? Well, there are various options. You could for example add the plot command to the  'loop.plt' file (which makes it less portable), but I like to give the instructions through the command line:
gnuplot -persist -e "plot 'data.dat'" loop.plt
where data.dat is a file that is continuously updated by some process. The nice thing about this is that you can recycle the 'loop.plt' without making any changes to it.

Passing a configuration file

If you have a lot of settings, a better alternative is to let the loop configuration file load your own configuration file. We make the following adjustments:

load config
pause 2
replot
reread
and we call gnuplot with:

gnuplot -persist -e "config='config.plt'" loop.plt
Which sets the variable config to your own configuration file. Using this method we have the option to just call gnuplot 'config.plt' to draw the plot once, or use the above command to keep refreshing without having to change any of the files. If you want to have even more control, you could also make the pause time a variable. See you next time!

Wednesday, July 18, 2012

Making gnuplot plots look like Mathematica plots

For an article I am writing I wanted to plot the following function:  $$ \theta \left(x > \frac{1}{2} \right) \left(\sin(4 \pi x) + \sin(4 \pi y) +2 \right) $$
Wolfram Alpha gives the following pretty plot:
Plot from Wolfram Alpha
However, since you need to pay now to export this picture in a more usable format (like svg, eps or pdf) I decided to recreate the picture in gnuplot. Friendly advice for you others writing articles: always use vector formats where possible, otherwise your graphics will look horrible on higher resolutions!

To plot the function in gnuplot we first have to do a little trick, because the step function is not included in gnuplot. It does have the sign function, however. So we can do the following: $$ \theta(x > \frac{1}{2}) = \frac{1 + \text{sgn}(x-\frac{1}{2})}{2} $$
A first attempt, with default settings is:

set terminal postscript eps
set pm3d at s hidden3d
unset hidden3d
unset surface
set xrange [0:1]
set yrange [0:1]
set samples 20,20

splot (1+sgn(x-0.5))*(sin(4*pi*x) + sin(4*pi*y)+2)/2 title ''

This gives



This doesn't really look too good. First thing that has to be changed is the color palette and the color of the grid. The color palette can be changed like this:

set terminal postscript eps
set palette defined ( 0 "#bd2c29", 2 "#ffd35a", 6 "white")

The numbers in front of the color codes are the values of the function that the color should correspond to. Thankfully, gnuplot automatically interpolates the colors.

Next up is the grid color. The color can be set to a given line style by adding the style number at the end of the pm3d options:
set pm3d implicit at s hidden3d 100
set style line 100 lc rgb '#000000' lt 1 lw 0.6

Here I have defined the grid style to be a thin black line. I have chosen index 100 to make sure it doesn't clash with other settings.

After removing the color bar and shifting the image down with ticslevel, we get the final image:
This looks a lot better! The only thing missing is the shadowing of surfaces angled away from the camera. I am not aware of a way to produce these in gnuplot. If anyone has additional advice to improve the plot, please let me know!

The final version of the code:

set terminal postscript eps
set output '| epstopdf --filter --outfile=plot.pdf'
set palette defined ( 0 "#bd2c29", 2 "#ffd35a", 6 "white")
set pm3d implicit at s hidden3d 100
set style line 100 lc rgb '#000000' lt 1 lw 0.6
unset hidden3d
unset surface
set border 4095 front linetype -1 linewidth 0.8
set ticslevel 0
set xrange [0:1]
set yrange [0:1]
set ztics 1
set isosamples 40,40
set samples 20,20
unset colorbox

splot (1+sgn(x-0.5))*(sin(4*pi*x) + sin(4*pi*y)+2)/2 title ''