Gnuplot:如何从数据文件加载和显示单个数值 [英] Gnuplot: How to load and display single numeric value from data file

查看:108
本文介绍了Gnuplot:如何从数据文件加载和显示单个数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据文件具有此内容

My data file has this content

# 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(在单词"total"旁边)的总计值,然后(a)将其存储到变量中,或者(b)直接在图的标题中使用它?

I am making a histogram from the case... lines using the script below - and that works. My question is: how can I load the grand total value 1976 (next to the word 'total') from the data file and either (a) store it into a variable or (b) use it directly in the title of the plot?

这是我的gnuplot脚本:

This is my gnuplot script:

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)的总数.

For the benefit of others trying to label histograms, in my data file, the column after the case label represents the total of the rest of the values on that row. Those total numbers are displayed at the top of each histogram bar. For example for case1, 522 is the total of (278 + 146 + 65 + 26 + 7).

我想在图表的某处显示总计,例如标题的第二行或标签.我可以将变量放入sprintf的标题中,但是我还没有弄清楚将单元格"值("cell"表示行列交集)加载到变量中的语法.

I want to display the grand total somewhere on my chart, say as the second line of the title or in a label. I can get a variable into sprintf into the title, but I have not figured out syntax to load a "cell" value ("cell" meaning row column intersection) into a variable.

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

Alternatively, if someone can tell me how to use the sum function to total up 522+120+660 (read from the data file, not as constants!) and store that total in a variable, that would obviate the need to have the grand total in the data file, and that would also make me very happy.

非常感谢.

推荐答案

让我们首先从(row,col)处提取单个单元格开始.如果它是单个值,则可以使用stats命令提取值.与everyusing一样指定rowcol,就像在plot命令中一样.在您的情况下,要提取总价值,请使用:

Lets start with extracting a single cell at (row,col). If it is a single values, you can use the stats command to extract the values. The row and col are specified with every and using, like in a plot command. In your case, to extract the total value, use:

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

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

To sum up all values in the second column, use:

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

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

And finally, to sum up all values in columns 3:7 in all rows (i.e. the same like the previous command, but without using the saved totals) use:

# 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才能起作用.

These commands require gnuplot 4.6 to work.

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

So, your plotting script could look like the following:

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)

给出以下输出:

这篇关于Gnuplot:如何从数据文件加载和显示单个数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆