GNUPlot: animation shortcuts

My problem, following my previous question, looks like this: I have a particle moving in a plane and now I want to have some kind of box on the side that says the position of the particle in XY and its velocity. I tried using labels, but they end up overlapping each other.

Here's a really rough sketch of what I want to see:

    +------------------------------------------+
    |         +-----------+                    |
    |         |           |       t = 2        |
    |         | PLOT HERE |  x = 0    y = 1    |
    |         |           |  vx = 2   vy = 3   |
    |         +-----------+                    |
    +------------------------------------------+

      

where these numbers will change every frame. I was able to animate the title, but the label seems to be different.

My current code is very similar to GNUPlot's answer here - Plot 2D datapoints as MPEG , but with some minor stylistic changes and that I have removed the title. I can create data that is only XY coordinates and this is what I am using right now.

I can also create something like (these are random dots, for illustration only)

 #X      Y      t       vx     vy
  0.00   1.00   0.0     0.0   6.28
  0.01   0.01   0.01    1.0   6.00

      

for animation labels.

+3


source to share


1 answer


Plot style is a good way to place marks from a data file labels

. However, there is some difficulty in placing marks outside of the actual printable area as these dots and marks are usually cut off.

Since you are using stats

anyway to fix x- and yrange, this is how I would go about it:

  • Set a fixed right margin for example. set rmargin 20

    ... This uses the right edge 20 characters wide. You can also use absolute coordinates such as set rmargin at screen 0.8

    , but since you need a field to place the labels, the character units seem to be fine.

  • Use the upper right corner of your plot area as a control point (STATS_max_x, STATS_max_y)

    and move the labels with the parameter offset

    and move back some character widths.

So the complete script might look like this:

# calculate the number of points
stats 'file.txt' using 1:2 nooutput

# if you want to have a fixed range for all plots
set xrange [STATS_min_x:STATS_max_x]
set yrange [STATS_min_y:STATS_max_y]

set terminal pngcairo size 800,400
outtmpl = 'output%07d.png'

v_label(x, y) = sprintf('vx = %.2f  vy = %.2f', x, y)
c_label(x, y) = sprintf('x = %d  y = %d', x, y)
t_label(t) = sprintf('t = %.2f', t)

set rmargin 20

do for [i=0:STATS_records-1] {
    set output sprintf(outtmpl, i)
    plot 'file.txt' every ::::i with lines title sprintf('n = %d', i),\
         '' every ::i::i using (STATS_max_x):(STATS_max_y):(t_label($3)) with labels offset char 11,-5 notitle,\
         '' every ::i::i using (STATS_max_x):(STATS_max_y):(c_label($1, $2)) with labels offset char 11,-6.5 notitle,\
         '' every ::i::i using (STATS_max_x):(STATS_max_y):(v_label($4, $5)) with labels offset char 11,-8 notitle
}

set output

      



Script output for t = 0.01

Note that the parameters rmargin

and offset

depend on terminal, terminal size, font, and font size. For more convenient placement of the labels, you can place the labels vx

and vy

separately and possibly change their alignment.

Alternatively, at each iteration, you can extract the current line from your data file and manually set the labels. However, this requires using an external tool to extract the string:

do for [i=0:STATS_records-1] {
    line = system(sprintf("sed -n %dp file.txt", i+2))
    set label 1 at screen 0.9, screen 0.9 sprintf("t = %.2f", real(word(line, 3)))
    set label 2 at screen 0.9, screen 0.88 sprintf("x = %.2f y = %.2f", real(word(line, 1)), real(word(line, 2)))
    plot 'file.txt' every ::::i with lines title sprintf('n = %d', i)
}

      

I don't know which option suits you best. I used i+2

as the line number to skip the commented header line that is not automatically detected. By using the tag for the label ( set label 1

), you will ensure that the old labels are overwritten.

+2


source







All Articles