如何在 gnuplot 中从相同的标准输入数据绘制多个图?

发布于 2024-10-17 15:49:36 字数 595 浏览 2 评论 0原文

我想要一个存储数据和 gnuplot 命令的 .plt 文件。我的数据看起来像

# x1 y1 x2 y2
  1  2  3  4
  5  6  7  8

并对应于两个图:(x1,y1) 和 (x2,y2)。

我知道我可以使用 "-" ,例如:

plot "-" using 1:2
# x1 y1 x2 y2
  1  2  3  4
  5  6  7  8
e

但这只会生成一个图,即 (x1,y1)。我正在尝试做类似的事情

plot "-" using 1:2, "-" using 3:4
# x1 y1 x2 y2
  1  2  3  4
  5  6  7  8
e

,但显然这不起作用,因为 gnuplot 需要来自第二个 "-" 的标准输入的一组新数据。

注意:

  1. 我无法更改数据的样式。它分为四列。
  2. 看来我可以通过 reread 来完成,但这需要两个文件。我真的只想要一个文件。

I want to have a single .plt file storing both data and gnuplot commands. My data looks like

# x1 y1 x2 y2
  1  2  3  4
  5  6  7  8

and corresponds to two plots: (x1,y1) and (x2,y2).

I know I can use "-" like:

plot "-" using 1:2
# x1 y1 x2 y2
  1  2  3  4
  5  6  7  8
e

But that would generate only one plot, i.e., (x1,y1). I'm trying to do something like

plot "-" using 1:2, "-" using 3:4
# x1 y1 x2 y2
  1  2  3  4
  5  6  7  8
e

but obviously that doesn't work since gnuplot expects a new set of data from the standard input for the second "-".

Notes:

  1. I cannot change the style of the data. It comes in four columns.
  2. It seems that I can do it with reread but that requires two files. I really want only one file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

游魂 2024-10-24 15:49:36

如果不修改输入数据的方式,您就无法做到这一点。当通过标准输入提供 gnuplot 数据时,它期望多个数据集之间用两个空行分隔,或者在连续行上交错。选项包括:

  • 将两个数据集输入不同的数据集
    完全绘制命令。

  • 更改
    文件格式,以便数据集具有
    它们之间有空行,然后
    使用索引引用它们。

  • 更改文件格式,以便
    交替的线代表不同的
    数据集,然后引用它们
    每个

  • 将数据放入一个
    文件,将绘图脚本放入
    另一个,然后引用数据
    使用不同的文件多次归档
    每次using子句。

如何在单个文件中绘制多个数据集?这些是 gnuplot 中内置的用于此类事情的唯一设施,而且也不完全符合您的要求。很高兴您已经修改了数据格式,因为这永远不会像您最初希望的那样工作。

You can't do this without modifying something about the way you input the data. When feeding gnuplot data via standard input, it expects multiple data sets to be delimited with two blank lines between them, or to be interleaved on successive lines. The options are:

  • Feed the two data sets into different
    plot commands altogether.

  • Change the
    file format so that data sets have
    blank lines between them, then
    reference them all with index.

  • Change the file format so that
    alternating lines represent different
    data sets, then reference them all
    with every.

  • Put the data into one
    file, the plotting script into
    another, and then reference the data
    file more than once with different
    using clauses each time.

There's an intro to the every and index commands starting at How do I plot several data sets in a single file? Those are the only facilities built into gnuplot for this sort of thing, and neither does exactly what you were asking about. it's good you've already modified the data formatting, because this wasn't ever going to work as you'd originally hoped.

陌上芳菲 2024-10-24 15:49:36

我不确定你可以编辑多少文件,但最简洁的方法可能是将整个文件放入 shell 脚本/批处理脚本中(你在 Linux 还是 Windows 上?)

在 Linux 上我做了这样的事情

#!/bin/bash

#put my data in a file
echo "
# x1 y1 x2 y2
  1  2  3  4
  5  6  7  8
" > my_dat.dat

#launch gnuplot
gnuplot<<EOF
#gnuplot commands here
set output "test.ps"
set term postscript

plot "my_dat.dat" u 1:2, \
     "my_dat.dat" u 3:4

set term pop
set output

EOF

# cleanup
rm my_dat.dat

然后我 chmod +wrx 我将上述命令放入并运行的文件。

注意:这个问题似乎也有相似之处:

gnuplot stdin, how to绘制两条线?

所以你可能也想看看那里

I'm not sure how much you can edit the file, but the tidiest way is probably to put the whole thing in a shell script/batch script (are you on linux or windows?)

On linux I do something like this

#!/bin/bash

#put my data in a file
echo "
# x1 y1 x2 y2
  1  2  3  4
  5  6  7  8
" > my_dat.dat

#launch gnuplot
gnuplot<<EOF
#gnuplot commands here
set output "test.ps"
set term postscript

plot "my_dat.dat" u 1:2, \
     "my_dat.dat" u 3:4

set term pop
set output

EOF

# cleanup
rm my_dat.dat

Then I chmod +wrx the file I put the above commands in and run.

Note: there also seems to be a similarity to this question:

gnuplot stdin, how to plot two lines?

So you might want to look there too

郁金香雨 2024-10-24 15:49:36

自 gp5.0 以来的新选项(另请参阅帮助内联数据):

$dataset << EOD
1 2 3 4 
5 6 7 8
EOD

plot $dataset using 1:2, $dataset using 3:4

New option since gp5.0 (see also help inline data) :

$dataset << EOD
1 2 3 4 
5 6 7 8
EOD

plot $dataset using 1:2, $dataset using 3:4
梦纸 2024-10-24 15:49:36

我知道这是一篇旧帖子,但我想指出另一种策略,以防其他人仍然在解决这个问题:

您还可以使用绘图命令并输入数据两次,例如:

plot "-" using 1:2, "-" using 3:4
# 1  2  3  4
  5  6  7  8
  e
  1  2  3  4
  5  6  7  8
  e

Gnuplot 实际上会等待两个块这个案例。当我不想更改命令以及通过管道输入 Gnuplot 时,我发现这非常有用。在实时场景中(取决于数据大小),这很可能仍然比缓冲到硬盘驱动器上的文件更快。

根据我的经验,缓冲脚本中的数据所需的代码量(需要多次通过管道传输)非常少。

I know this is an old post, but I would like to point to another strategy in case someone else still struggles with this issue:

You could also use your plotting command and input the data twice, like:

plot "-" using 1:2, "-" using 3:4
# 1  2  3  4
  5  6  7  8
  e
  1  2  3  4
  5  6  7  8
  e

Gnuplot will actually wait for two blocks in this case. I find this very useful when I don't want to change the commands and when I am feeding Gnuplot by pipe. In a real-time scenario (depending on the data-size) this is very likely to be still faster than buffering to a file on the hard-drive.

In my experience the amount of code required to buffer the data in your script, which is required to pipe it more than once, is very low.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文