代码之家  ›  专栏  ›  技术社区  ›  Emilio

Gnuplot在同一轴上使用两个不同的刻度

  •  1
  • Emilio  · 技术社区  · 7 年前

    我正在尝试使用gnuplot 5.0创建一个图形,在同一个x轴上有两个刻度。我使用multiplot选项创建了一个,代码如下:

    reset
    
    set terminal pngcairo
    set output "test.png"
    
    unset key
    set ylabel "Temperature (C)"
    set ytics nomirror
    set yrange[0:7]
    
    set multiplot layout 1,2
    
    set xrange [0:5.99] #Avoid plotting the last xtics in the first graphic
    set xlabel "Heating time (minutes)"
    set rmargin at screen 0.7
    plot x
    
    set xrange [0:4]
    set xlabel "Seconds after stop"
    set rmargin
    set lmargin at screen 0.7
    set xtics 1
    unset ylabel
    unset ytics
    set y2tics nomirror
    set format y2 ''
    f(x) = a * exp (-x*b)
    a=6
    b=1 
    plot f(x)
    

    由此结果:

    enter image description here

    我想生成几个像这样的图像,并将它们添加到多点排列中,但我不确定使用嵌套多点是否容易。有没有一种更简单的方法可以不使用multiplot(如将X轴拆分为两个分量)来获取每个图像?

    提前谢谢。

    1 回复  |  直到 5 年前
        1
  •  0
  •   theozh    3 年前

    以防OP或其他人对这件事感兴趣。。。您不需要显式写入是否要打印数据文件中的函数或数据。在您的示例中,您使用函数。

    您可以在没有“嵌套”多点的情况下执行某些操作,这当然也是可能的,但可能会有点混乱。 所以,也许下面的内容更简单。基本上可以“手动”设置xtics。为了避免多次键入相同的代码,可以使用宏(检查 help macros ).

    代码:

    ### multiplot graph with different x-axis scales
    reset session
    
    # define functions
    Temperature(x) = x<t0 ? x*a/t0 : a*exp(-(x-t0)*b)
    myColor(col) = column(col)<t0 ? 0xff0000 : 0x0000ff
    myTic(x,t) = sprintf("%g", x<=t ? x : x-t)
    
    # define macro
    myPlot = "\
        set xrange[0:t0+t1]; \
        set arrow 1 from first t0, graph 0 to first t0, graph 1 nohead; \
        set label 1 'heating' center at first t0/2., graph 0 offset 0,-2; \
        set label 2 'cooling' center at first (t0+t1/2.), graph 0 offset 0,-2; \
        set xtics (); \
        do for [i=0:int(t0/dt1)] { set xtics add (myTic(i*dt1,t0) i*dt1) }; \
        do for [i=0:int(t1/dt2)] { set xtics add (myTic(i*dt2,t1) i*dt2+t0) }; \
        plot '+' u 1:(Temperature($1)):(myColor(1)) w l lc rgb var "
    
    set samples 200
    set key noautotitle
    set bmargin 3
    set ylabel "Temperature"
    set grid x,y
    
    set multiplot layout 2,2
    
        t0=6; t1=4; dt1=1; dt2=1; a=6; b=1
        @myPlot
    
        t0=4; t1=10; dt1=1; dt2=2; a=4; b=0.5
        @myPlot
    
        t0=20; t1=30; dt1=5; dt2=10; a=2; b=0.1
        @myPlot
    
        t0=4; t1=4; dt1=1; dt2=1; a=10; b=1
        @myPlot
    
    unset multiplot
    ### end of code
    

    结果:

    enter image description here