Search code examples
gnuplot

Gnuplot: how to (systematically) multiply each column by a factor


I have some data that look like this:

# time  Ux      Uy      sigXX   sigYY   sigXY
1.e-3   0.001  -0.001   1.e6    0.7e6   2.e5
2.e-3   0.002  -0.0025  2.e6    1.2e6   5.e5
3.e-3   0.004  -0.0035  2.8e6   1.4e6   9.e5

as you can guess the data is in SI units (s, m, Pa), and I'm using gnuplot.

For readability in my plots, I'd like to put the x/ytics in (ms, mm, MPa). This is classically done with using ($..):

set ylabel 'sig[MPa]'
plot 'data.dat' using ($1*1000):($4*1.e-6)

... but adding all those parentheses is getting tedious (as I replot a lot this file). I'm dreaming of something like:

set datafactor $1 1000
set datafactor $4 1.e-6
plot 'data.dat' u 1:4 

but couldn't find it yet. I know I could also use redirection and an external script to do the conversion on the fly (I'll post it as an answer if someone's interested), but it seems too complex for such a simple and common task.

Does anyone know how to do this? Is there a native gnuplot way to do this kind of conversion once?


Solution

  • You probably have 3 options:

    1. change your datafile permanently with whatever software
    2. multiply the datafile columns once when you load them to gnuplot
    3. multiply the columns everytime when you plot them

    Option 1 you probably don't want, option 3 you definitely don't want, so, maybe option 2 might be a solution which would be similar to your Python code. You basically load your file into a datablock, scale it and then plot this datablock.

    ### mulitply data columns with factor
    reset session
    
    set table $Data
        plot 'data.dat' u ($1*1000):($2*1000):($3*1000):($4*1e-6):($5*1e-6):($6*1e-6) w table
    unset table
    
    plot $Data u 1:4
    ### end of code
    

    Limitations:

    Keep in mind, in case you have some comment lines or data separated by single or multiple emtpy lines which in some cases might be essential for plotting, these lines will be all removed when plot ... with table. If you need these empty lines there might be more complicated gnuplot workarounds or you need to call external software.