## Sunday, October 16, 2011

### Broken axes graph in gnuplot (2)

Last time I said there is a "better but a little tricky" method to plot a broken axes graph. And now let us come to see this method.

If we want to plot function f(x) in range of [a:b] and [c:d] (i.e., a broken axes graph), we can define a new function:
```g(x) = f(x)                         a<= x <= b
g(x) = f(x+c-b-dx)              b+dx <= x <= b+dx-c+d
g(x) = 1/0                         other x
```
where dx is the gap length. We plot this new function and modify xtics to a right one. Then we get a broken x-axes graph which will similar to the one we plotted last time. Let us start work our idea out.
```reset
set term png
set output "broken_axes2.png"
#Plot f(x)=sin(x**2/4.) at range [20:22] and [40:42]
f(x)=sin(x**2/4.)
g(x)=20<x && x<22 ?f(x) :22<x && x<22.2 ?1/0 :22.2<x && x<24.2 ?f(x+17.8) :1/0
set xrange [20:24.2]
set yrange [-1:1]
set sample 2000
set border 2+8  #the bottom and top border will be plotted mannually
set arrow 1 from 20,-1 to 22,-1 nohead              #bottom and top border
set arrow 2 from 22.2,-1 to 24.2,-1 nohead
set arrow 3 from 20,1 to 22,1 nohead
set arrow 4 from 22.2,1 to 24.2,1 nohead
set arrow 5 from 21.95,-1.05 to 22.05,-0.95 nohead  #axes broken indication line
set arrow 6 from 22.15,-1.05 to 22.25,-0.95 nohead
set arrow 7 from 21.95,0.95 to 22.05,1.05 nohead
set arrow 8 from 22.15,0.95 to 22.25,1.05 nohead
#Modify xtics to a right value
set xtics ("20" 20,"20.5" 20.5,"21" 21,"21.5" 21.5,"22" 22,\
"40" 22.2,"40.5" 22.7,"41" 23.2,"41.5" 23.7,"42"24.2)
set xlabel "Time:t(s)"
set ylabel "Signal:U(V)"
plot g(x) w l lw 2 notitle
```
The picture file broken_axes2.png is shown below. Broken axes graph plotted by gnuplot

Next time I will put this method to the datafile case. 