Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix zero to be at the same place when using separate y axes in gnuplot?

Tags:

gnuplot

I have a data file, with column 1 as the independent variable and columns 2 and 3 as dependent variables. I want to plot variables 2 and 3 on different y axes using something like this:

plot "file.out" u 1:2 axes x1y1, "file.out" u 1:3 axes x1y2

When I do this, the "0" for both axes are offset from one another. How can I fix the zero of one y-axis to the zero of the other y-axis, without explicitly setting yrange to be symmetric for both quantities?

like image 328
Dan Avatar asked Dec 07 '25 06:12

Dan


2 Answers

It is possible form version 5 to use set link. However it does not autofit the ratios, so you're left with calculating them yourself

stat "file.out" u 1:2
MAX1=abs(STATS_max_y)
MIN1=-abs(STATS_min_y)
stat "file.out" u 1:3
MAX2=abs(STATS_max_y)
MIN2=-abs(STATS_min_y)
min(a,b)=(a<b)?a:b
set link y2 via min(MAX1/MAX2,MIN1/MIN2)*y inverse y/min(MAX1/MAX2,MIN1/MIN2)
plot "file.out" u 1:2 axes x1y1, "file.out" u 1:3 axes x1y2
like image 189
Joce NoToPutinsWarInUkraine Avatar answered Dec 09 '25 02:12

Joce NoToPutinsWarInUkraine


Here is a solution which works without linking axes, hence it also works even with gnuplot 4.4 (the version from 2010). Although, it doesn't need stats but as a disadvantage it requires to replot the data to get the proper scaling of the y2-axis.

Code:

### aligning zero on y1- and y2-axes
reset

set ytics nomirror
set y2tics nomirror
set xzeroaxis
set key top left

plot \
    sin(x) axes x1y1 w l, \
    cos(x)-0.5 axes x1y2 w l

R0 = -GPVAL_Y_MIN/(GPVAL_Y_MAX-GPVAL_Y_MIN)
y2_min_new  = abs(GPVAL_Y2_MIN)>abs(GPVAL_Y2_MAX) ? GPVAL_Y2_MIN : R0*GPVAL_Y2_MAX/(R0-1)
y2_max_new = abs(GPVAL_Y2_MAX)>abs(GPVAL_Y2_MIN) ? GPVAL_Y2_MAX : (R0-1)*GPVAL_Y2_MIN/R0

set y2range[y2_min_new:y2_max_new]
replot
### end of code

Result:

enter image description here

like image 33
theozh Avatar answered Dec 09 '25 02:12

theozh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!