Gnuplot: how to add y2 axis scale for different units

前端 未结 3 1335
别跟我提以往
别跟我提以往 2020-12-14 03:51

I\'m plotting data from a file. The data points are in metric units. I want to show a second scale on the right (y2) that\'s in standard units.

The file represents r

相关标签:
3条回答
  • 2020-12-14 04:23

    Version 5.0 added support for this kind of relations between the y and y2 (or also x and x2) axis:

    set xrange[0:370]
    set ytics nomirror
    set y2tics
    set link y2 via 0.2248*y inverse y/0.2248
    plot x
    

    example for <code>set link</code>

    0 讨论(0)
  • 2020-12-14 04:34

    I know it's an old question and the answer has already been accepted, but I think it's worth sharing my approach.

    I simply use modified labels for the x2axis. In your case, this would be

    set y2tics ("10" 10/0.2248, "20" 20/0.2248 etc etc...
    

    that can be looped this way

    do for [i=0:1000:10] { set y2tics add (sprintf("%i",i) i/0.2248) }
    

    where the for range should be adjusted according to your data (you could use stats and the variable GPVAL_DATA_Y_MAX for complete peace of mind). Don't forget to

    set ytics nomirror
    

    This will give exactly what are you looking for, in (almost) a one liner: enter image description here

    If you want to use a grid and have the converted factors on the x2axis, so that for example to the label y=50 N would correspond y2=11.2 (it keeps things tidy if you use a grid) you can do

    do for [i=0:1000:50] { set y2tics add (sprintf("%5.1f",i*0.2248) }
    

    This is the result: enter image description here

    0 讨论(0)
  • 2020-12-14 04:42

    It seems to me that the easiest way to do this is to simply scale the data:

    set y2tics
    plot sin(x) w lines, 5*sin(x) w lines axes x1y2
    

    Of course, you're plotting data from a file, so it would look something more like:

    set y2tics
    FACTOR=0.2248  #conversion factor from newtons to lbf
    plot 'datafile' u 1:2 w lines, '' u 1:(FACTOR*$2) w lines
    

    If you're setting the yrange explicitly (which you may need to do):

    set yrange [ymin:ymax]
    set y2range [ymin*FACTOR:ymax*FACTOR]
    

    Finally, if you really want to rely on autoscaling, you're going to need to do some "gymnastics".

    First, set a dummy terminal so we can plot without making a plot:

    set term unknown
    plot 'datafile' u 1:2  #collect information on our data
    

    Now that we've collected information on the data, we can set our real y2range

    FACTOR=0.2248
    set y2range [FACTOR*GPVAL_Y_MIN : FACTOR*GPVAL_Y_MAX]
    set y2tics nomirror
    set ytics nomirror
    

    Now set the terminal and plot the data:

    set term ...
    set output ...
    plot 'datafile' u 1:2 w lines
    
    0 讨论(0)
提交回复
热议问题