gnuplot 可以分割数据吗?

发布于 2024-12-06 05:08:05 字数 3621 浏览 1 评论 0 原文

我有一些以下格式的数据:

10.0 1 a 2
10.2 2 b 2
10.4 3 a 2
10.6 4 b 2
10.8 4 c 10
11.0 4 c 20

...其中第三列基本上表示属于“单独”数据集;因此,我想用红色显示属于“a”的样本,用蓝色显示属于“b”的样本等(使用 gnuplot 版本 4.4 patchlevel 2)。

我设法以某种方式获得“样本”样式作为“脉冲”和“”样式之间的混合;并通过 在 Gnuplot 4.0 中选择线条类型和颜色 ,设法采用单独的颜色 - 这就是我所取得的成绩(basictest.gnuplot):

#!/usr/bin/env gnuplot

print "Generating data..."

# to specify data inline in script:
# only system can work, as it is quoted;
# but still have to escape newlines!

system "cat > ./inline.dat <<EOF\n\
10.0 1 a 2\n\
10.2 2 b 2\n\
10.4 3 a 2\n\
10.6 4 b 2\n\
10.8 4 c 10\n\
11.0 4 c 20\n\
EOF\n"

print "done generating."


# set ranges
set yrange [0:30]
set xrange [0:4]

# define line styles - can call them up later
set style line 1 linetype 1 linewidth 3 pointtype 3 linecolor rgb "red"
set style line 2 linetype 1 linewidth 2 pointtype 3 linecolor rgb "green"
set style line 3 linetype 1 linewidth 2 pointtype 3 linecolor rgb "blue"


# offset the X axis: instead of 1:2, use: ($1-10):2
# to "mix", use "" for last datset - but must also repeat the "using"!
# ... and plot:

plot 'inline.dat' using ($1-10):2 with impulses linestyle 1,\
     "" using ($1-10):2 notitle with points linestyle 1,\
     "" using ($1-10):2 notitle with lines linestyle 2,\
     'inline.dat' using ($1-10):4 with impulses linestyle 3,\
     "" using ($1-10):4 notitle with points linestyle 3

# below just for saving file
#set terminal png
#set output 'basictest.png'
#replot

...看起来像这样:

basictest.png

换句话说,而不是上面的 - 说对于 ($1-10):2,我希望看到第 1st 和第 3rd 示例 ('a') 为蓝色,第 2nd< /sup> 和第 4 个样本('b')为红色,最后两个样本('c')为绿色(我将绿线留在那里只是为了查看风格混合)。

我知道这可以通过编写一个脚本来实现,该脚本将解析原始数据,并从中生成三个表,如下所示:

10.0 1 a 2
10.4 3 a 2
---
10.2 2 b 2
10.6 4 b 2
---
10.8 4 c 10
11.0 4 c 20

...然后可以“单独”绘制 - 但我在徘徊如果< code>gnuplot 也许有一些类似的内部设施?

预先感谢您的任何答复,
干杯!

 

PS:一些对我有用的链接:

 

编辑:只是想发布 脚本链接< /a> 我已经接近以我想要的方式可视化数据:

basic-labels.png

我有点想将标签(添加的“点”作为矩形背景)视为 graphviz 或 节点 href="http://www.texample.net/tikz/examples/simple-flow-chart/" rel="nofollow noreferrer">TikZ,并在它们之间设置那些漂亮的连接线 - 但即使这样已经对我有很大帮助了:) 请注意,这是 wxt 输出 - 其他终端,如 pngpdfcairo 将会有一个完全混乱的盒子渲染/标签(并且会需要手动调整)。

I have some data in the following format:

10.0 1 a 2
10.2 2 b 2
10.4 3 a 2
10.6 4 b 2
10.8 4 c 10
11.0 4 c 20

... where the third column basically indicates belonging to a 'separate' dataset; and so I would like to show, say, those samples belonging to 'a' in red, those belonging to 'b' in blue etc (using gnuplot Version 4.4 patchlevel 2).

I managed to somehow get the 'sample' style as a mix between 'impulses' and 'point' styles; and via Choosing line type and color in Gnuplot 4.0, managed to employ individual colors - this is how far I got (basictest.gnuplot):

#!/usr/bin/env gnuplot

print "Generating data..."

# to specify data inline in script:
# only system can work, as it is quoted;
# but still have to escape newlines!

system "cat > ./inline.dat <<EOF\n\
10.0 1 a 2\n\
10.2 2 b 2\n\
10.4 3 a 2\n\
10.6 4 b 2\n\
10.8 4 c 10\n\
11.0 4 c 20\n\
EOF\n"

print "done generating."


# set ranges
set yrange [0:30]
set xrange [0:4]

# define line styles - can call them up later
set style line 1 linetype 1 linewidth 3 pointtype 3 linecolor rgb "red"
set style line 2 linetype 1 linewidth 2 pointtype 3 linecolor rgb "green"
set style line 3 linetype 1 linewidth 2 pointtype 3 linecolor rgb "blue"


# offset the X axis: instead of 1:2, use: ($1-10):2
# to "mix", use "" for last datset - but must also repeat the "using"!
# ... and plot:

plot 'inline.dat' using ($1-10):2 with impulses linestyle 1,\
     "" using ($1-10):2 notitle with points linestyle 1,\
     "" using ($1-10):2 notitle with lines linestyle 2,\
     'inline.dat' using ($1-10):4 with impulses linestyle 3,\
     "" using ($1-10):4 notitle with points linestyle 3

# below just for saving file
#set terminal png
#set output 'basictest.png'
#replot

... which looks like this:

basictest.png

In other words, instead of the above - say for ($1-10):2, I'd like to see the 1st and 3rd sample ('a') in blue, 2nd and 4th sample ('b') in red, and the last two ('c') in green (I left the green line there just to see the effect of the style mixing).

I'm aware that this could be achieved by writing a script, that will parse the original data, and generate three tables out of that, as in:

10.0 1 a 2
10.4 3 a 2
---
10.2 2 b 2
10.6 4 b 2
---
10.8 4 c 10
11.0 4 c 20

... which could then be plotted 'separately' - but I was wandering if gnuplot maybe had some internal facility for something like that?

Thanks in advance for any answers,
Cheers!

 

PS: Some useful links for me:

 

EDIT: just wanted to post a link to a script with which I'm getting close to visualizing the data the way I wanted to:

basic-labels.png

I'm kinda wanting to treat the labels (with the added 'points' as rectagle backgrounds) as nodes in graphviz or in TikZ, and set up those nice connection lines between them - but even this already helps me much :) Note that this is the wxt output - other terminals like png or pdfcairo will have a completely messed up rendering of boxes/labels (and would need to be tuned by hand).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残龙傲雪 2024-12-13 05:08:05

这是可能的。 Gnuplot 有一个类似于 C 中的三元运算符。Gnuplot 也会忽略未定义的表达式,例如负数的对数或除以零。将它们放在一起,您可以通过为不满足条件的线生成无效数字来仅绘制满足某些特定条件的线。从问题中稍微简化一下,该方法如下所示:

plot "inline.dat" using (strcol(3) eq 'a' ? $1 : 1/0):2

It is possible. Gnuplot has a ternary operator like in C. Gnuplot also will ignore undefined expressions, such as logarithms of negative numbers or dividing by zero. Putting those together, you can plot only those lines that satisfy some particular condition by producing invalid numbers for those that fail to satisfy the condition. Simplifying a bit from the question, the approach looks like this:

plot "inline.dat" using (strcol(3) eq 'a' ? $1 : 1/0):2
很糊涂小朋友 2024-12-13 05:08:05

您可以使用 awk 来实现这一点。我不知道我是否会称其为 gnuplot 的“某些内部设施”,但我认为它做了您想要它做的事情:

使用数据文件 Data.csv:

10.0 1 a 2
10.2 2 b 2
10.4 3 a 2
10.6 4 b 2
10.8 4 c 10
11.0 4 c 20

绘制数据

plot "<awk '{if($3 == \"a\") print $1,$2}' Data.csv" u ($1 - 10):2 w lp, \
     "<awk '{if($3 == \"b\") print $1,$2}' Data.csv" u ($1 - 10):2 w lp, \
     "<awk '{if($3 == \"c\") print $1,$2}' Data.csv" u ($1 - 10):2 w lp

用注意 \"< /code> 转义脚本解析器 ^^.
至于绘图风格问题,您可以利用 gnuplot 提供的全部功能。

You could use awk for that. I don't know if I would call that "some internal facility" of gnuplot, but I think it does what you want it to do:

With a data file Data.csv:

10.0 1 a 2
10.2 2 b 2
10.4 3 a 2
10.6 4 b 2
10.8 4 c 10
11.0 4 c 20

plot the data with

plot "<awk '{if($3 == \"a\") print $1,$2}' Data.csv" u ($1 - 10):2 w lp, \
     "<awk '{if($3 == \"b\") print $1,$2}' Data.csv" u ($1 - 10):2 w lp, \
     "<awk '{if($3 == \"c\") print $1,$2}' Data.csv" u ($1 - 10):2 w lp

Note the \" to escape the script parser ^^.
As for plot style issues you can make use of the full spectrum gnuplot has to offer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文