Gnuplotting

Create scientific plots using gnuplot

August 22nd, 2013 | 12 Comments

On the PGF plots page I found a nice example of visualizing data with cubes. Here we will recreate the same with gnuplot as you can see in Fig. 1.

Cubes

Fig. 1 Cubes with different colors. (code to produce this figure, cube function, data)

We need basically two things in order to achieve it. First we have to plot a single cube with gnuplot. This is not as straight forward as you may thing. We have to define a data file for it and plot it with the pm3d style which will result in Fig. 2.

# single cube
0 0 0
0 0 1
0 1 1
0 1 0
0 0 0

1 0 0
1 0 1
1 1 1
1 1 0
1 0 0

0 0 0
1 0 0
1 1 0
0 1 0
0 0 0

0 0 1
1 0 1
1 1 1
0 1 1
0 0 1
set cbrange [0.9:1]
set palette defined (1 '#ce4c7d')
set style line 1 lc rgb '#b90046' lt 1 lw 0.5
set pm3d depthorder hidden3d
set pm3d implicit
unset hidden3d
splot 'cube.txt' u 1:2:3:(1) w l ls 1

The use of the fourth (1) column for the splot command ensures that the cube gets the same color on every side. Only the edges are highlighted by a slighty different color given by the line style.

A single cube

Fig. 2 A single cube. (code to produce this figure, data)

In a second step we reuse the code from the Object placement using a data file entry in order to plot cubes at different positions with different colors. To get the different colors and positions we replace the cube.txt file with a cube function that returns the values for the desired position and color.

add_cube(x,y,z,c) = sprintf('cube(%f,%f,%f,%i) w l ls %i,',x,y,z,c,c)
CMD = ''
stats 'cube_positions.txt' u 1:(CMD = CMD.add_cube($1,$2,$3,$4))
CMD = 'splot '.CMD.'1/0 w l ls 2'
eval(CMD)

12 Comments

  1. […] Plotting cubes (gnuplotting.org) […]

  2. Hello;

    “Code to produce this figure” Link at the Fig 1. is not Working.

    However, cube part is amazing. Thank you for your work. Really impessive.

    Emre

  3. hagen says:

    Thanks, I corrected the link.

  4. Oscar says:

    If I wanted prisms instead cubes, is possible to do it ? for example i have the coordinates of six prisms (the bottom,left and front node) with six different heights.
    for i=1,5 the height prism changes with the index h=1.0*i, the z cooordinate of the sixth prism is above from the others and its height is greater than others. So the function cube changes with each prism, how can i do a loop at the code? Thanks for your answer

  5. Laurent says:

    Many thanks! Great work.

    When I active the tic option on, the coordinate system seems to me a diaster.
    Do you have any correction for this version that the coordiante system could be just in the middle and every tic works fine without the color flag flot?

    Moreover, I wish I could define every length of cube I want to draw, if that’s not too much difficult to write it on.

    Thank you.

  6. hagen says:

    Hi Laurent.

    If you comment the two lines with the border and tics settings

    #set border 0
    #unset tics
    

    you will get a normal coordinate system with tics. If the coordinate system should be centered at z=0 you can achieve this by

    set xyplane 0
    

    If you further want to change the length of every cube you have to add another option to the cube function that can do this.

  7. Carl says:

    Hi Hagen,

    I tried to run both codes “single_cube.gnu” and “cube.gnu” under Gnuplot 4.6 Patch level 3.

    Unfortunately I got always a message like this one:
    line 67: warning: Skipping unreadable file “<echo "0.000000 0.000000 0.000000 3\n0.000000 0.000000 1.000000 3\n0.000000 1.000000 1.000000 3\n0.000000 1.000000 0.000000 3\n0.000000 0.000000 0.00
    0000 3\n\n1.000000 0.000000 0.000000 3\n1.000000 0.000000 1.000000 3\n1.000000 1.000000 1.000000 3\n1.000000 1.000000 0.000000 3\n1.000000 0.000000 0.000000 3\n\n0.000000 0.000000 0.000000 3\n1.000000 0
    .000000 0.000000 3\n1.000000 1.000000 0.000000 3\n0.000000 1.000000 0.000000 3\n0.000000 0.000000 0.000000 3\n\n0.000000 0.000000 1.000000 3\n1.000000 0.000000 1.000000 3\n1.000000 1.000000 1.000000 3\n
    0.000000 1.000000 1.000000 3\n0.000000 0.000000 1.000000 3""
    Any idea? It seems that the function cube.fct is not really understood from Gnuplot.
    By the way, your idea is great! But of course I would like to understand why I have this error message.
    Thanks in advance.
    Carl.

  8. hagen says:

    Hi Carl.
    That is strange, I have gnuplot 4.6 patchlevel 0 and get no error at all. Even the line showing in your error message seems to be ok to me. Are you running under Linux?

  9. Carl says:

    Hello Hagen,
    no I am running gnuplot under Windows XP Professional!
    As you say, it is quite strange.
    Could it be that something under Windows must be done?
    Thank you in advance.
    Carl.

  10. hagen says:

    I never really tested gnuplot under Windows, but it could be that the echo command is not available at all under Windows or not working as expected. You could test it for example by plotting single points as explained here.

  11. Tim says:

    Impressive looking chart but I’m something of a gnuplot novice and I cannot seem to get this example to work with either gnuplot 4.4.3 or 4.6.5 under ubuntu 12.04 LTS.

    I first get a diagnostic about a missing argument in the ‘set pm3d’ line, which was easy to get past by adding a digit.

    The second error involves a complaint about the stats line:

    stats ‘cube_positions.txt’ u 1:(CMD = CMD.add_cube($1,$2,$3,$4))
    ^
    “./cube.gnu”, line 64: invalid command

    at which point my (few) attempts to come up with a workaround have proven insufficient.

  12. hagen says:

    Hi Tim.

    Did you really used gnuplot 4.6.5? Because the stats command should be part of it. Did you make sure that the cube.gnu and cube.fct file were in the same directory you ran the code?

    For me the cube.gnu file is still working.

Leave a Reply