Gnuplot - 使用两个命令进行迭代

Gnuplot - Iteration with two commands

我正在尝试使用仅包含 0 或 1 的简单数据文件 (.example) 构建一种条形图。这是 .example 中包含的数据:

dest    P1 P2 P3 P4 P5  NA
D1  0 1 1 0 0  0
D2  0 0 1 0 0  0 
D3  0 1 0 1 0  0 
""
GPV 1 1 1 1 1  1

这是我使用的代码:

set style histogram rowstacked title textcolor lt -1
set datafile missing 'nan'
set style data histograms

plot '.example' using ( ==0 ? 1 : 0 ) ls 17 title 'NA', \
'' using ( ==1 ? 1 : 0 ) ls 1, \
for [i=3:5] '.example' using ( column(i)==0 ? 1 : 0) ls 17 notitle, \
for [i=3:5] '' using ( column(i)==1 ? 1 : 0) ls i-1

其中最后两个命令迭代了可能大量的 根据 column(i) 的值堆叠白色或彩色框的列。为了在直方图中的不同列之间保持相同的颜色顺序,我需要使用两个命令将两个迭代合并为一个迭代。

可能吗?关于如何做到这一点有什么建议吗?

你可以使用嵌套循环,我认为这是你想要实现的。您可以使用外层循环遍历大量列,并使用内层循环遍历两个选项(白色与彩色)for [i=3:5] for [j=0:1],并告诉 gnuplot 在内容不匹配时忽略该列j 使用 1/0 的值(或使用对直方图有效的技巧,将其设置为 0,就像您已经在做的那样):

set style histogram rowstacked title textcolor lt -1
set datafile missing 'nan'
set style data histograms

plot '.example' using ( ==0 ? 1 : 0 ) ls 17 title 'NA', \
'' using ( ==1 ? 1 : 0 ) ls 1, \
for [i=3:5] for [j=0:1] '.example' using ( column(i) == j ? 1 : 0 ) \
ls ( j == 0 ? 17 : i-1 ) notitle

上面的代码等同于你已有的代码,只有 j 的值允许根据你有 0 还是 1 作为样式来切换样式列的值。