首页 > 解决方案 > 以递归方式更改绘图类型(带线,带点)

问题描述

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

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

分别使用用户选择的线型 1 和 2绘制时间与 COL1 的关系test1和时间与 COL2 的关系。test2但是,如果我想要 time vs COL1with points和 time vs COL2with 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 命令自动生成一个字符串,例如:

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

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

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

提前致谢

标签: gnuplot

解决方案


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

代码:

### 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"

推荐阅读