【问题标题】:Gnuplot: How to load and display single numeric value from data fileGnuplot:如何从数据文件中加载和显示单个数值
【发布时间】:2013-09-06 03:37:06
【问题描述】:

我的数据文件有这个内容

# data file for use with gnuplot
# Report 001
# Data as of Tuesday 03-Sep-2013 
total   1976
case1   522 278 146 65  26  7
case2   120 105 15  0   0   0
case3   660 288 202 106 63  1

我正在使用下面的脚本从案例...行制作直方图 - 这很有效。我的问题是:如何从数据文件中加载总计 1976 年(在“总计”一词旁边)并(a)将其存储到变量中或(b)直接在绘图标题中使用它?

这是我的 gnuplot 脚本:

reset
set term png truecolor
set terminal pngcairo size 1024,768 enhanced font 'Segoe UI,10'
set output "output.png"
set style fill solid 1.00
set style histogram rowstacked
set style data histograms
set xlabel "Case"
set ylabel "Frequency"
set boxwidth 0.8
plot for [i=3:7] 'mydata.dat' every ::1 using i:xticlabels(1) with histogram \
notitle, '' every ::1 using 0:2:2 \
with labels \
title "My Title"

为了其他尝试标记直方图的人的利益,在我的数据文件中,案例标签后面的列表示该行中其余值的总和。这些总数显示在每个直方图条的顶部。例如 case1,522 是 (278 + 146 + 65 + 26 + 7) 的总和。

我想在图表的某处显示总计,例如标题的第二行或标签中。我可以将一个变量放入 sprintf 到标题中,但我还没有想出将“单元格”值(“单元格”表示行列交叉点)加载到变量中的语法。

或者,如果有人可以告诉我如何使用 sum 函数总计 522+120+660(从数据文件中读取,而不是作为常量!)并将该总数存储在一个变量中,那么就不需要在数据文件中有总计,这也让我很高兴。

非常感谢。

【问题讨论】:

    标签: sum gnuplot title


    【解决方案1】:

    对于 linux 和类似的。

    如果你不知道你的数据所在的行号,但是你知道它在第m列的值为x的行的第n列,你可以定义一个函数

    get_data(m,x,n,filename)=system('awk "\$'.m.'==\"'.x.'\"{print \$'.n.'}" '.filename)
    

    然后使用它,例如,as

    y = get_data(1,"case2",4,"datafile.txt")
    

    使用user424855提供的数据

    print y
    

    应该返回15

    【讨论】:

    • 不错。你有一点错字,命令应该是:get_data(m,x,n,filename)=system('awk "\$'.m.'==\"'.x.'\"{print \$'.n.'}" '.filename).
    【解决方案2】:

    让我们从在 (row,col) 处提取单个单元格开始。如果是单个值,可以使用stats 命令提取值。 rowcoleveryusing 指定,就像在绘图命令中一样。在您的情况下,要提取总值,请使用:

    # extract the 'total' cell
    stats 'mydata.dat' every ::::0 using 2 nooutput
    total = int(STATS_min)
    

    要总结第二列中的所有值,请使用:

    stats 'mydata.dat' every ::1 using 2 nooutput
    total2 = int(STATS_sum)
    

    最后,总结所有行中3:7 列中的所有值(即与上一个命令相同,但不使用保存的总数)使用:

    # sum all values from columns 3:7 from all rows
    stats 'mydata.dat' every ::1 using (sum[i=3:7] column(i)) nooutput
    total3 = int(STATS_sum)
    

    这些命令需要 gnuplot 4.6 才能工作。

    因此,您的绘图脚本可能如下所示:

    reset
    set terminal pngcairo size 1024,768 enhanced
    set output "output.png"
    set style fill solid 1.00
    set style histogram rowstacked
    set style data histograms
    set xlabel "Case"
    set ylabel "Frequency"
    set boxwidth 0.8
    
    # extract the 'total' cell
    stats 'mydata.dat' every ::::0 using 2 nooutput
    total = int(STATS_min)
    
    plot for [i=3:7] 'mydata.dat' every ::1 using i:xtic(1) notitle, \
         '' every ::1 using 0:(s = sum [i=3:7] column(i), s):(sprintf('%d', s)) \
         with labels offset 0,1 title sprintf('total %d', total)
    

    给出以下输出:

    【讨论】:

    • 啊哈!!使用 stats 命令加载单元格值的精彩示例。
    • @Christoph 总是很高兴阅读您与 Gnuplot 相关的帖子,非常有用的东西 :)
    • 什么是冒号 : 语法?我不明白 ::::0 或 ::1 应该做什么 - 有人可以向我解释一下吗?
    • @user3728501 这在every 的文档Plot -> data -> every 部分中有描述,请参阅gnuplot.info/docs_5.0/…
    猜你喜欢
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多