【问题标题】:gnuplot do not overlap linesgnuplot 不重叠线
【发布时间】:2018-03-22 03:11:41
【问题描述】:

下图是用gnuplot生成的:

它显示了 5 个(但可能更多或更少)波浪。但是,我需要的是分别显示每个波浪,就像这样(我用 gimp 做的):

有没有办法用 gnuplot 做到这一点?我还需要每个波浪都是不同的颜色,就像现在一样。

这里是使用的 gnuplot 脚本:http://pastebin.com/vAD2syTS

这里是 gnuplot 脚本用来修复我的数据的 python 脚本:http://pastebin.com/WEncNjDA

这是数据:http://pastebin.com/ewBpvHWM

这是python脚本修复后的数据:http://pastebin.com/cgimMyr9

【问题讨论】:

  • 如果我们不能假设您提前知道地块的数量,这将是一个棘手的问题......
  • @mgilson 我可以写一个脚本来找出这个或类似的东西
  • 我今天要休息,但请看我的回答……无论如何,它应该给你一些可以玩的东西。

标签: gnuplot


【解决方案1】:

我玩这个很开心。我的策略包括绘制设置略有不同的第一个、最后一个和中间图,以使边界对齐。我还定义了一些函数,以确保所有绘图颜色确实不同,除非您有超过 1600 万个数据集要绘制。

### indices: change this parameter to equal the number of data sets to be plotted
indices = 8 
# h: height of output in pixels
h = 150.0*indices
# d: top and bottom margin in pixels
d = 75.0

### define functions to help set top/bottom margins
top(i,n,h,d) = 1.0 - (d+(h-2*d)*(i-1)/n)/h
bot(i,n,h,d) = 1.0 - (d+(h-2*d)*i/n)/h

### define some fun RGB code converter functions

# round: crude rounding function (gnuplot doesn't have this?)
# assumes a float, returns an int
round(x) = x-int(x)>=0.5?ceil(x):floor(x)

# i2h: converts a (decimal) integer between 0 and 15 to hex.
# returns a string, 0-F corresponding to 0-15
i2h(i) = i==10?'A':i==11?'B':i==12?'C':i==13?'D':i==14?'E':i==15?'F':sprintf('%d',i)

# i2r: converts an integer to an RGB code.
# returns a string (RGB code) of length 6, 000000-FFFFFF corresponding to 0-16777215
# changing the last division to 15 instead of 16 prevents colors being too faint
i2r5(i) = i2h(i/(15**5))
i2r4(i) = i2h(i%(16**5)/(15**4))
i2r3(i) = i2h(i%(16**5)%(16**4)/(15**3))
i2r2(i) = i2h(i%(16**5)%(16**4)%(16**3)/(15**2))
i2r1(i) = i2h(i%(16**5)%(16**4)%(16**3)%(16**2)/(15**1))
i2r0(i) = i2h(i%(16**5)%(16**4)%(16**3)%(16**2)%(16**1))
i2r(i) = i2r5(i).i2r4(i).i2r3(i).i2r2(i).i2r1(i).i2r0(i)

# rgb_iter: returns the i-th of n RGB codes, evenly spaced across the spectrum
rgb_iter(i, n) = '#'.i2r(round((i-1)*(16777215.0/(n-1))))

### first set up some basic plot parameters
set term png enhanced size 800,h font 'Courier-Bold,14'
set output 'waves.png'

set title 'Wave propagation by geophones'
set ylabel 'Wave'

set xrange [1400:]
set yrange [-0.15:0.15]
set ytics ('-0.1' -0.1, '0.0' 0.0, '0.1' 0.1)

set key out right

### now make plots
set multiplot layout indices,1

### first plot
set border 14
set tmargin at screen top(1,indices,h,d)
set bmargin at screen bot(1,indices,h,d)
unset xtics
plot 'temp.dat' index 0 w lines lw 3 lc rgb rgb_iter(1,indices) title 'Geophone 1'
unset title

### intermediate plots
set border 10
unset xlabel
do for [i=1:indices-2] {
 set tmargin at screen top(i+1,indices,h,d)
 set bmargin at screen bot(i+1,indices,h,d)
 plot 'temp.dat' index i w lines lw 3 lc rgb rgb_iter(i+1,indices) title sprintf('Geophone %d', i + 1)
}

### last plot
set border 11
set tmargin at screen top(indices,indices,h,d)
set bmargin at screen bot(indices,indices,h,d)
set xtics nomirror
set xlabel 'Iterations'
plot 'temp.dat' index (indices-1) w lines lw 3 lc rgb rgb_iter(indices,indices) title sprintf('Geophone %d', indices)

unset multiplot

样本数据集的输出如下所示:

顶部/底部地块的大小不是很完美,正如 mgilson 所说,可能需要对set xmargin at screen ... 命令进行一些摆弄才能使所有地块大小相等。

(如果没有别的,那些 int->RGB 转换器对于专门的应用程序很方便;我也有从 RGB 代码到整数的函数。)

编辑:我更新了脚本,因此所有地块的高度都相同。

【讨论】:

  • 为什么第一张和最后一张图都比其他的小?我的意思是,它们的高度更小
  • 当使用set multiplot layout X,1 命令时,每个绘图都获得输出文件垂直区域的 1/X,默认情况下,其中一些空间用于边距/标签。顶部和底部的地块较短,因为它们不占用整个地块区域。对于中间图,bmargin 和 tmargin 设置为 0,因此它们恰好占据 1/indices 垂直区域。顶部和底部的图有非零边距以容纳标题和 xlabel。
  • 我知道我没有问过这个问题,但是我应该在哪里看才能让这张图向右旋转 90 度?
  • 没有简单的方法可以将绘图旋转 90 度;它不是 gnuplot 的内置选项。如果你用谷歌搜索“gnuplot rotate plot”,你会发现人们做过各种各样的黑客行为,通常涉及旋转 gnuplot 中的所有标签,然后在创建输出文件后用外部程序旋转输出文件。
  • 是的,我看到了。那么垂直绘制图形呢?那可能吗?也许以某种方式改变输入
【解决方案2】:

如果你提前知道地块的数量(gnuplot 4.6):

NUM_PLOTS = 6
set multiplot layout NUM_PLOTS,1
do for [i=1:NUM_PLOTS]{
   plot 'temp.dat' index i w lines lw 3 title sprintf('Geophone %d', i + 1)
}
unset multiplot

如果你真的想确保你的边框和轴对齐,你可以使用set lmargin at screen ...set rmargin at screen ...set tmargin at screen ...set bmargin at screen ...的组合,其中...是一些函数iNUM_PLOTS,尽管可能将 lmarginrmargin 设置在某个恒定位置就足够了(如果您不需要共享轴,gnuplot 应该注意从上到下对齐)。

set border 10 如果您真的需要,将删除顶部和底部边框线,但可能没有必要。

我会推荐类似的东西:

NUM_PLOTS = 6
set multiplot layout NUM_PLOTS,1
set lmargin at screen 0.1
set rmargin at screen 0.9
do for [i=1:NUM_PLOTS]{
   plot 'temp.dat' index i w lines lw 3 title sprintf('Geophone %d', i + 1)
}
unset multiplot

【讨论】:

  • 这很好,但是它不断重复标题、X 轴标签和 X 轴测量点。有没有办法将标题保留在第一张图中,X 轴标签和测量点保留在最后一张图中?
猜你喜欢
  • 2013-02-13
  • 1970-01-01
  • 2016-12-19
  • 1970-01-01
  • 2012-08-25
  • 1970-01-01
  • 2013-03-28
  • 2018-10-24
  • 1970-01-01
相关资源
最近更新 更多