bash 如何在 gnuplot 中绘制一个从 y 数据中自动减去最低值的图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10034891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:57:48  来源:igfitidea点击:

How do I make a plot in gnuplot with the lowest value automatically subtracted from the y data?

bashgnuplot

提问by andyras

I am plotting the creation times of a large batch of files in gnuplot to see if they are created linearly in time (they are not).

我正在 gnuplot 中绘制大量文件的创建时间,以查看它们是否按时间线性创建(它们不是)。

Here is my code:

这是我的代码:

#!/bin/bash

stat -c %Y img2/*png > timedata

echo "set terminal postscript enhanced colour
set output 'file_creation_time.eps'
plot 'timedata'" | gnuplot

The problem I have is that the y data are the creation time in seconds since unix start time, so the plot just has 1.333...e+09 on the y-axis. I would like to have the creation time of the first file scaled to zero so that the relative creation times are readable.

我的问题是 y 数据是自 unix 开始时间以来的创建时间(以秒为单位),因此该图在 y 轴上只有 1.333...e+09。我想将第一个文件的创建时间缩放为零,以便相对创建时间是可读的。

I encounter this problem in a number of data-plotting contexts, so I would like to be able to do this within gnuplot rather than resorting to awk or some utility to preprocess the data.

我在许多数据绘图上下文中都遇到了这个问题,因此我希望能够在 gnuplot 中执行此操作,而不是诉诸 awk 或某些实用程序来预处理数据。

I know the first time will be the smallest since the files are named serially, so is there a way to access the first element in a file, something like

我知道第一次将是最小的,因为文件是按顺序命名的,所以有没有办法访问文件中的第一个元素,比如

`plot 'data' using (-[firstelement])`

?

?

采纳答案by mgilson

I think you can do something like that...(the following is untested, but I think it should work...). Basically, you have to plot the file twice -- the first time through gnuplot picks up statistics about the dataset. The second time through, you use what you found on the first run-through to plot what you actually want.

我认为你可以做这样的事情......(以下未经测试,但我认为它应该工作......)。基本上,您必须绘制文件两次——第一次通过 gnuplot 获取有关数据集的统计信息。第二次通过时,您使用在第一次运行中发现的内容来绘制您真正想要的内容。

set terminal unknown
plot 'datafile' using 1:2
set terminal post enh eps color
set output 'myfile.eps'
YMIN=GPVAL_Y_MIN
plot '' u 1:(-YMIN)

If you have gnuplot 4.6, you can do the same thing with the statscommand. http://www.gnuplot.info/demo/stats.html

如果你有 gnuplot 4.6,你可以用这个stats命令做同样的事情。 http://www.gnuplot.info/demo/stats.html

EDITIt appears you want the first point to provide the offset (sorry, misread the question)...

编辑看来您希望第一点提供偏移量(对不起,误读了问题)...

If you want the first point to provide the offset, you may be able to do something like (again, untested -- requires gnuplot >= 4.3):

如果您希望第一个点提供偏移量,您可以执行以下操作(同样,未经测试 - 需要 gnuplot >= 4.3):

first=0;
offset=0;
func(x)=(offset=(first==0)?x:offset,first=1,x-offset)
plot 'datafile' using (func())

回答by YBE

Gnuplot accepts unix commands, so you can say something like

Gnuplot 接受 unix 命令,所以你可以这样说

gnuplot> plot "< tail -3 test.dat" using 1:2 with lines

in order to plot just the last three lines. You can use something like this for your purpose. Moreover, if you want to plot let's say from line 1000 to 2000

为了只绘制最后三行。您可以将这样的东西用于您的目的。此外,如果你想绘制让我们说从 1000 到 2000

plot "<(sed -n '1000,2000p' filename.txt)" using 1:2 with lines 

You can check this website, for more examples.

您可以查看此网站,了解更多示例。

回答by andyras

I found a related stackoverflow question hereand exploited the awk script from one of the answers:

我在这里找到了一个相关的 stackoverflow 问题并利用了其中一个答案中的 awk 脚本:

#!/bin/bash

stat -c %Y img2/*png > timedata

echo "set terminal postscript enhanced colour
set output 'file_creation_time.eps'
unset key
set xlabel 'file number'
set ylabel 'file creation time (after first)'
plot \"<awk '{if(NR==1) {shift = $1} print ($1 - shift)}' timedata\"" | gnuplot

The output looks like this (these are not the data I was talking about in my question, but similar): enter image description here

输出看起来像这样(这些不是我在我的问题中谈论的数据,但类似): 在此处输入图片说明

So, gnuplot can do what I want but it does depend on the UNIX environment...

所以,gnuplot 可以做我想做的事,但它确实取决于 UNIX 环境......

I also tried mgilson's method:

我也试过mgilson的方法:

plot 'timedata'
YMIN=GPVAL_Y_MIN
plot '' u (-YMIN)

but gnuplot (my version is 4.4.2) did not find the minimum correctly. It came close; it looks like it plotted such that the minimum of the y range is 0: enter image description here

但是 gnuplot(我的版本是 4.4.2)没有正确找到最小值。它接近了;看起来它的绘制使得 y 范围的最小值为 0: 在此处输入图片说明