首页 > 解决方案 > gnuplot“条件”:三列上的多重曲线(30?)

问题描述

我一生中写过很少的脚本,但我几乎只做 bash。从来不需要更多。到现在为止:我想用这些包装器制作一些图表

1 type1 1
2 type1 2
3 type2 1
4 type1 3
5 type2 2
6 type3 1

模式在哪里:

在电子表格中,3d 列类似于=(NB.si($B$1:$B4;$B4)/nb.si($B$1:$B;$B4)第 4 行。


我仍在处理我将如何在 python 中将“累积”总和附加到数据中(我当时只有两个第一列),这是简单的数学和文本处理脚本。我知道如何在电子表格中实现自动化,对 bash 有一些想法,但我对 python 知之甚少。但是在那里,这不是我的问题(但我在这里向仁慈的人敞开心扉:))


问题

我发现gnuplot可能会有所帮助,并且我在各个网站上阅读了手册和一些示例,但我仍然有点困惑:我将如何绘制树曲线,从 0 开始,到 1,与

样本


多谢你们 !:)

标签: pythongnuplot

解决方案


我会做这样的事情

# this function relates every type to an int, convenient for setting the plot styles
f(x) = x eq "type1"? 1: x eq "type2"? 2:0

# this tell gnuplot to ignore the result of lines not matching
set datafile missing "NaN"

# setting a nice style for every type
set style line 1 linetype 1 linewidth 2 pointtype 3 linecolor rgb "red"
set style line 2 linetype 1 linewidth 2 pointtype 3 linecolor rgb "blue"

# using a ternary operator to pick out the lines matching that type
plot for [i in "type1 type2"] 'test.dat' u (strcol(2) eq i?$3:NaN) w l ls f(i)

得到这个 结果

如果您愿意,您可以for从绘图命令中删除并使用 just plot 'test.dat' u (strcol(2) eq "type1"?$3:NaN) w l ls 1, 'test.dat' u (strcol(2) eq "type2"?$3:NaN) w l ls 2,为每种类型显式绘图,并更好地控制每条绘制线的细节。

您可以创建另一个函数来为每一行添加标题,类似于f(x)但返回每种类型的字符串而不是 int。

我还听说过使用 awk 或内部函数在 gnuplot 中进行累积和的方法,您可以在此处查看gnuplot-cumulative-column-question


推荐阅读