【问题标题】:Gnuplot use two different scales in the same axisGnuplot 在同一轴上使用两个不同的刻度
【发布时间】:2021-08-20 13:43:12
【问题描述】:

我正在尝试使用 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)

有了这个结果:

我想生成几张像这样的图像并将它们添加到多图排列中,但我不确定使用嵌套多图是否容易。有没有更简单的方法可以在不使用 multiplot 的情况下获取每个图像,比如将 xaxis 分成两个组件?

提前致谢。

【问题讨论】:

  • 您想绘制像示例中的xf(x) 之类的函数还是数据文件中的数字数据?您的输入数据如何?请举个例子。

标签: gnuplot


【解决方案1】:

以防 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

结果:

【讨论】:

  • 嗨。感谢你的回答。在我的示例中,为了简单起见,我使用了函数,但它也可以是从数据文件中读取的数据。我认为使用函数或外部数据的代码会相似,但既然你问要绘制什么样的数据,现在我想会有差异。如果我想使用数据文件,代码是否会发生显着变化?
  • @Emilio 感谢您的反馈。在 3 年多没有答案之后,您可能找到了另一个解决方案?!如果您绘制函数,则通过xrange[:] 给出范围。如果您绘制数据,则必须从数据中获取范围,可能通过stats。最重要的是,我现在注意到,第一部分有几分钟,第二部分有几秒钟。当然,这也可以处理,但因此,了解您的数据如何存储在文件中非常重要。加热和冷却等是只有一个时间柱还是两个时间柱?请编辑您的问题并给出数据示例。
  • @Emilio 问题回答了吗?任何回应将不胜感激。您能否为您的数据文件提供示例数据?
  • 非常感谢。我找不到我正在使用的数据文件。如果我没记错的话,对于每个地块,我都有一个加热时间的文件和一个不同的冷却时间文件。在每个文件中,第一列是时间,第二列是温度数据。无论如何,我认为您的回答对我的问题是正确的,因为我没有说数据在文件中。
猜你喜欢
  • 1970-01-01
  • 2021-11-05
  • 2019-02-16
  • 2022-01-25
  • 2023-02-26
  • 2012-12-07
  • 2021-05-17
  • 1970-01-01
  • 2021-08-13
相关资源
最近更新 更多