我的数据文件包含此内容
# 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
Run Code Online (Sandbox Code Playgroud)
我正在使用下面的脚本从案例...行制作直方图 - 这是有效的.我的问题是:如何从数据文件加载1976年的总值(在'total'一词旁边)并且(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"
Run Code Online (Sandbox Code Playgroud)
为了其他人试图标记直方图的好处,在我的数据文件中,case标签后面的列表示该行上其余值的总和.这些总数显示在每个直方图栏的顶部.例如,对于case1,522是总数(278 + 146 + 65 + 26 + 7).
我想在我的图表上的某个地方显示总计,例如标题的第二行或标签.我可以在sprintf中获得一个变量到标题中,但我还没有想出将"单元格"值("单元格"意味着行列交集)加载到变量中的语法.
或者,如果有人可以告诉我如何使用sum函数总计522 + 120 + 660(从数据文件读取,而不是作为常量!)并将该总数存储在变量中,这样就不需要使用sum总数在数据文件中,这也会让我很开心.
非常感谢.
Chr*_*oph 13
让我们从(row,col)中提取单个单元格开始.如果它是单个值,则可以使用该stats命令提取值.该row和col与指定every和using,就像在一个plot命令.在您的情况下,要提取总值,请使用:
# extract the 'total' cell
stats 'mydata.dat' every ::::0 using 2 nooutput
total = int(STATS_min)
Run Code Online (Sandbox Code Playgroud)
要汇总第二列中的所有值,请使用:
stats 'mydata.dat' every ::1 using 2 nooutput
total2 = int(STATS_sum)
Run Code Online (Sandbox Code Playgroud)
最后,要总结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)
Run Code Online (Sandbox Code Playgroud)
这些命令需要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)
Run Code Online (Sandbox Code Playgroud)
它给出了以下输出:

| 归档时间: |
|
| 查看次数: |
11932 次 |
| 最近记录: |