强制 gnuplot 显示零轴,同时让它自动缩放

Force gnuplot to show zeroaxis while letting it autoscale

我正在寻找一种允许 gnuplot 自动缩放的方法,但仍然强制图形在 y 轴上包含 [-1:1] 范围。

而不是像这样的图片 我希望它看起来像这样

不太重要:如果我能去掉 canvas 顶部和底部的白色 space 也很好。

我的代码:

set encoding utf8
set yzeroaxis lt 1 lc 8 lw 2
set xzeroaxis lt 1 lc 8 lw 2
set xtics pi
set mxtics 6
set ytics 0.5
set grid mxtics ytics lt 1 lc 3 lw 0.5
unset key
set format x '%.0P'
set samples 5000
set offset graph 0.0, graph 0.0, graph 0.1, graph 0.1
set xrange [-4*pi:4*pi]
set size ratio -1

set terminal pngcairo enhanced font 'Cambria' size 1600,900

do for [t=1:8]{
    A = 0.5*t

    do for [b=2:4]{

    do for [i=-4:4]{
        if(i!=0){
        c = 0.5*i
        outfile = sprintf('Asin(bx)+c/%.2gsin(%ux)%+.2g.png',A,b,c)
        set output outfile
        plot A*sin(b*x)+c lc 7 lw 2 }
    }
    }
}

选中 help xrange 并向下滚动... 我猜你正在寻找这个:

set yrange [*<-1:1<*]

y 范围将自动缩放,但始终包括 [-1:1]。

加法:

关于你的第二个关于白边的问题。 绘图后,变量 GPVAL_TERM_YMAXGPVAL_TERM_YMIN 保存 canvas(或屏幕或终端)上图表的底部和顶部 y 坐标值 "terminal coordinates"。在 pngcairo 中,20 个终端单元是 1 个像素。要查看更多变量,请键入 show var GPVAL。由于您仅在 绘制 之后获得这些值,因此您必须重新绘制并相应地调整终端大小。对于 x-label 你添加一点 space,例如d=80。我添加了浅灰色背景只是为了说明。我不得不稍微调整一下你的文件名。

如果您希望 x 轴位于图像的恒定像素位置,您可能需要检查一下:GNUplot - draw line using window coordinate system

代码:

### adjust canvas to size of plot
reset session

set encoding utf8
set yzeroaxis lt 1 lc 8 lw 2
set xzeroaxis lt 1 lc 8 lw 2
set xtics pi
set mxtics 6
set ytics 0.5
set grid mxtics ytics lt 1 lc 3 lw 0.5
unset key
set format x '%.0Pπ'
set samples 1600
set offset graph 0.0, graph 0.0, graph 0.1, graph 0.1
set xrange [-4*pi:4*pi]
set size ratio -1

set yrange[*<-1:1<*]
set terminal pngcairo background rgb 0xfcfcfc  # background just added for illustration

set lmargin screen 0.03
set rmargin screen 0.97

d = 80
do for [t=1:8]{
    A = 0.5*t
    do for [b=2:4]{
        do for [i=-4:4]{
            if(i!=0){
                c = 0.5*i
                set terminal pngcairo enhanced font 'Cambria' size 1600,900 
                outfile = sprintf('A=%.1g,b=%.1g,c=%.1g.png',A,b,c)
                set output outfile
                plot A*sin(b*x)+c lc 7 lw 2
                set output
                NewHeight = GPVAL_TERM_YMAX - GPVAL_TERM_YMIN + d
                set terminal pngcairo enhanced font 'Cambria' size 1600,NewHeight
                set output outfile
                replot
                set output
            }
        }
    }
}
### end of code

结果:(90 个生成的图表中只有 2 个示例)