递归更改绘图类型(带线,带点) [英] Change plot type (with lines, with points) recursively

查看:66
本文介绍了递归更改绘图类型(带线,带点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于Julia创建gnuplot的包装,以自动执行我的绘图.我的目标是给朱莉娅要绘制的文件名,要使用的线型类型和要绘制的列.例如,如果我有文件 test1 test2 ,它们都具有3列和标头"time,COL1,COL2"以及自定义线型1和2,我会这样写:

I'm trying to create a wrapper for gnuplot based in Julia to automate my plots. My goal is to give Julia the file names to plot, the type of line style that I want to use and the column to plot. For example, if I have files test1 and test2, both with 3 columns and headers "time,COL1,COL2" and custom line styles 1 and 2, I would write this:

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

分别使用用户选择的线条样式1和样式2,绘制时间与 test1 的COL1以及时间与 test2 的COL2的关系图.但是,如果我想要时间与带点的COL1 和时间与带线的COL2 怎么办?

To plot time vs COL1 of test1 and time vs COL2 of test2 using the line styles 1 and 2, respectively, chosen by the user. However, what if I want time vs COL1 with points and time vs COL2 with lines?

我知道如何手动执行此操作,但是考虑到文件数量可以是任意数量,我如何自动执行此操作?我尝试了几种方法.

I know how to do it manually, but how could I do it automatically, considering that the number of files can be any number? I tried several ways.

1.

我尝试使用 do for 循环,如下所示:

I tried using the do for loop like this:

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
}

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

This approach creates independent windows and not a single plot, and I want a single plot.

2.

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

I tried using macro substitution like this:

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]

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

But macro substitution only accepts scalar variables, not array elements. Later, after further reading, learned how exactly macro substitution works and realized this way would never work.

我非常确定我可以使用整个plot命令自动生成一个字符串,例如:

I'm pretty sure that I could automatically generate a string with the entire plot command like:

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

但是这种方法似乎需要大量工作,而且要管理我要介绍的所有异常情况根本不是一件容易的事.

But this approach seems a lot of work and managing all exceptions that I want to introduce is not easy at all.

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

Is there any better approach or is my only chance to create the string programmatically and then eval it?

预先感谢

推荐答案

我不确定,但是我想您不能将绘图样式从带有点的更改为带有线的通过 @ 宏在绘图命令中(至少我没有成功).但是您可以将绘图样式 with linespoints linestyle 一起使用,以使其看起来像 withpoints lines >.显然,将 pointsize 0 设置为仅获取行.但是,设置 linewidth 0 以便仅获取点不起作用,因为我在这里刚刚学到了:gnuplot:为什么线宽0的宽度不为零?.而是使用 linetype -2 .无论如何,您都必须定义10种线型.

I'm not sure, but I guess you can't change the plotting style from with points to with lines within a plot command via @ macro (at least I haven't succeeded). But you can use the plotting style with linespoints together with linestyle such that it looks either like with points or with lines. It is obvious to set pointsize 0 to get lines only. However, set linewidth 0 in order to get points only doesn't work as I've just learned here: gnuplot: why is linewidth 0 not zero in width?. Instead, use linetype -2. At some point you have to define your 10 linestyles anyway.

代码:

### 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控制台中输入:

From the gnuplot console type:

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

或在您的操作系统中:

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

这篇关于递归更改绘图类型(带线,带点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆