【问题标题】:Change plot type (with lines, with points) recursively以递归方式更改绘图类型(带线,带点)
【发布时间】:2020-08-20 01:01:27
【问题描述】:

我正在尝试为基于 Julia 的 gnuplot 创建一个包装器,以自动化我的绘图。我的目标是为 Julia 提供要绘制的文件名、我想要使用的线型类型以及要绘制的列。例如,如果我有文件 test1test2,它们都有 3 列和标题“时间、COL1、COL2”以及自定义线型 1 和 2,我会这样写:

gnuplot -c gnuplot-script.gnuplot "test1 test2" "COL1 COL2" "1 2"

分别使用用户选择的线型 1 和 2 绘制 test1 的时间与 COL1 和 test2 的时间与 COL2。但是,如果我想要时间 vs COL1 with points 和时间 vs COL2 with lines 怎么办?

我知道如何手动完成,但考虑到文件数可以是任意数字,我怎么能自动完成呢?我尝试了几种方法。

1.

我尝试像这样使用do for 循环:

nplots = words(ARG1)

do for [i=1:nplots] {
    file = word(ARG1,i)
     col = word(ARG2,i)
    styl = word(ARG3,i)+0

    # I have 10 custom line styles and all above 4 should be continuous line
    if (styl>4) {
        points_lines = "with lines"
    } else {
        points_lines = "with points"
    }

    plot file using "time":col @points_lines ls styl title col
}

这种方法会创建独立的窗口而不是单个图,我想要一个图。

2.

我尝试使用这样的宏替换:

nplots = words(ARG1)

array  files[nplots]
array   cols[nplots]
array styles[nplots]
array points_lines[nplots]

do for [i=1:nplots] {
     files[i] = word(ARG1,i)
      cols[i] = word(ARG2,i)
    styles[i] = word(ARG3,i)+0

    if (styles[i]>4} {
        points_lines[i] = "lines"
    } else {
        points_lines[i] = "points"
    }
}


plot for[i=1:nplots] files[i] using "time":cols[i] @points_lines[i] ls styles[i] title cols[i]

但是宏替换只接受标量变量,而不接受数组元素。后来经过进一步阅读,了解了宏替换究竟是如何工作的,并意识到这种方式永远行不通。

我很确定我可以使用整个绘图命令自动生成一个字符串,例如:

plot_command = "plot file1 using "time":"col" points_lines1 ls styles1, ..."
eval plot_command

但是这种方法似乎工作量很大,并且管理我要介绍的所有异常一点也不容易。

有没有更好的方法或者是我唯一的机会以编程方式创建字符串然后eval 它?

提前致谢

【问题讨论】:

  • 编辑:在方法 2 中,with@points_lines[i] 之前缺失

标签: gnuplot


【解决方案1】:

我不确定,但我猜你不能通过@ 宏在绘图命令中将绘图样式从with points 更改为with lines(至少我没有成功)。 但是您可以将绘图样式with linespointslinestyle 一起使用,使其看起来像with pointswith lines。 很明显将pointsize 0 设置为仅获取行。但是,设置linewidth 0 以仅获得积分不起作用,正如我刚刚在这里学到的那样:gnuplot: why is linewidth 0 not zero in width?。相反,请使用linetype -2。 无论如何,在某些时候你必须定义你的 10 种线条样式。

代码:

### change between plotting styles  'with points' and 'with lines' programmatically
reset session

PlotCount = words(ARG1)
File(i) = word(ARG1,i)
Col(i) = word(ARG2,i)
Style(i) = int(word(ARG3,i))

set style line  1 lt -2 pt 7 lc rgb "red"
set style line  2 lt -2 pt 7 lc rgb "green"
set style line  3 lt -2 pt 7 lc rgb "blue"
set style line  4 lt -2 pt 7 lc rgb "magenta"
set style line  5 lt  1 ps 0 lc rgb "yellow"
set style line  6 lt  1 ps 0 lc rgb "cyan"
set style line  7 lt  1 ps 0 lc rgb "orange"
set style line  8 lt  1 ps 0 lc rgb "olive"
set style line  9 lt  1 ps 0 lc rgb "violet"
set style line 10 lt  1 ps 0 lc rgb "pink"

plot for [i=1:PlotCount] File(i) u "time":Col(i) w lp ls Style(i) title File(i)
pause -1
### end of code

从 gnuplot 控制台输入:

call "myScript.gp" "test1 test2" "COL1 COL2" "1 6"

或在您的操作系统中:

gnuplot.exe -c "myScript.gp" "test1 test2" "COL1 COL2" "1 6"

【讨论】:

    猜你喜欢
    • 2014-12-26
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 2012-12-28
    相关资源
    最近更新 更多