递归地改变绘图类型(有线,有点)。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了递归地改变绘图类型(有线,有点)。相关的知识,希望对你有一定的参考价值。

我正试图在Julia中创建一个基于gnuplot的包装器,以自动完成我的绘图。我的目标是给Julia提供要绘制的文件名、要使用的行式类型和要绘制的列。例如,如果我有文件 test1test2我可以用3列和标题 "time,COL1,COL2 "以及自定义行样式1和2,我可以这样写。

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

绘制时间与COL1的 test1 和时间与COL2的关系 test2 分别使用用户选择的行样式1和2。然而,如果我想要时间与COL1 with points 和时间与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命令。

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

但这种方法似乎很麻烦 而且要管理所有的异常情况 我想引入这些异常情况一点也不容易。

有什么更好的方法吗,或者说我唯一的机会就是以编程的方式创建字符串,然后用 eval 吗?

先谢谢你

答案

我不确定,但我想你不能改变绘图风格,从 with pointswith lines 在绘图命令中通过 @ 宏(至少我还没有成功).但你可以使用绘图风格的 with linespoints 连同 linestyle 这样看起来 with pointswith lines.很明显,设置 pointsize 0 只获得行。然而,设置 linewidth 0 以获得积分,但我在这里才知道,这是不可行的。gnuplot: 为什么linewidth 0的宽度不是0?. 相反,使用 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"

以上是关于递归地改变绘图类型(有线,有点)。的主要内容,如果未能解决你的问题,请参考以下文章

需要示例代码片段帮助

.htaccess - 递归地将斜杠映射到下划线(内部重定向)

Javascript - 在递归函数中获取 setTimeout 的返回值

python的递归和二分法

java基础3-重载+命令行传参+递归+数组+排序

为啥我的有线球体在平移和改变相机角度时会变成椭球体?