Gnuplot 无效命令“设置输出”

发布于 2025-01-09 07:13:26 字数 1525 浏览 0 评论 0原文

我已经尝试解决这个问题超过一天了,但是无论api(C++,python)和gnuplot版本不会,问题都是一样的。 让我们看一下几个不同的 python 代码示例,这些代码在 gnuplot 终端中无需 API 即可正常工作。

1.来自此处

from subprocess import Popen, PIPE
gnuplot = 'gnuplot'
p = Popen([gnuplot, '-persist'], stdin=PIPE)
cmd = b"""set terminal qt
set timefmt "%H:%M:%S"
set xdata time
set term png size 1600, 1200
set output "test.png"
set yrange [-1.5:1.5]
set grid
set title ("test_param"); 
plot "test.dat" using 1:2 with lines lt rgb "blue" lw 2 title "Q value"
"""
p.stdin.write(cmd); p.stdin.flush()

2.来自这里

import PyGnuplot as pg

pg.c("set terminal qt")
pg.c('set timefmt "%H:%M:%S";')
pg.c('set xdata time;')
pg.c('set yrange [-1.5:1.5]')
pg.c('set grid')
pg.c('plot "test.dat" using 1:2 with lines lt rgb "blue" lw 2 title "Q value"')
pg.c('set term png size 1600, 1200;')
pg.c('set output "test.png"')

在这两种情况下,我都会遇到一些奇怪的错误:

gnuplot> t output "test.png"
         ^
         line 0: invalid command

我已经尝试了几乎所有 API 示例,但没有任何效果几乎相同的错误

“test.dat”示例:

00:00:05.742    -0.098281
00:00:05.745    -0.098281
00:00:05.746    -0.099969
00:00:05.747    -0.100051
00:00:05.748    -0.100051
00:00:05.749    -0.100037
00:00:05.750    -0.102142
00:00:05.750    -0.102150
00:00:05.752    -0.101780

I have been trying to solve the problem for more than a day, but whatever api (C++, python) and gnuplot version would not be, the problem is the same.
Let's look at several different examples of python code that works fine without an API in the gnuplot terminal.

1.From here

from subprocess import Popen, PIPE
gnuplot = 'gnuplot'
p = Popen([gnuplot, '-persist'], stdin=PIPE)
cmd = b"""set terminal qt
set timefmt "%H:%M:%S"
set xdata time
set term png size 1600, 1200
set output "test.png"
set yrange [-1.5:1.5]
set grid
set title ("test_param"); 
plot "test.dat" using 1:2 with lines lt rgb "blue" lw 2 title "Q value"
"""
p.stdin.write(cmd); p.stdin.flush()

2.From here

import PyGnuplot as pg

pg.c("set terminal qt")
pg.c('set timefmt "%H:%M:%S";')
pg.c('set xdata time;')
pg.c('set yrange [-1.5:1.5]')
pg.c('set grid')
pg.c('plot "test.dat" using 1:2 with lines lt rgb "blue" lw 2 title "Q value"')
pg.c('set term png size 1600, 1200;')
pg.c('set output "test.png"')

In both cases i get some strange error:

gnuplot> t output "test.png"
         ^
         line 0: invalid command

I have tried almost all API examples and nothing works with almost the same errors

Example of "test.dat":

00:00:05.742    -0.098281
00:00:05.745    -0.098281
00:00:05.746    -0.099969
00:00:05.747    -0.100051
00:00:05.748    -0.100051
00:00:05.749    -0.100037
00:00:05.750    -0.102142
00:00:05.750    -0.102150
00:00:05.752    -0.101780

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

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

发布评论

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

评论(1

最丧也最甜 2025-01-16 07:13:26

虽然我还没有完全理解所有细节,但以下内容对我有用(Python 3.6.3 Windows 10)。

与您的第一个代码示例有一些区别:

  • 如果您之后直接将其设置为 png ,为什么还要设置 term qt ?
  • 就我而言,我必须提供 gnuplot 可执行文件的完整路径(因为 Windows 环境变量中未设置 gnuplot 路径)。请注意Python代码中的r'...'
  • 我使用了数据和绘图的绝对路径(否则这取决于你启动Python脚本的位置),我猜你拥有的文件路径使用单引号(对于 gnuplot?!)并转义反斜杠 \\ (对于 Python?!)。或者,您可以仅使用单个前斜杠 /,例如 set output 'C:/Users/W2102029/Scripts/test.png'

如果有更好的方法和解释,我我很高兴向更有经验的 Windows/Python/gnuplot 用户学习。

代码:

from subprocess import Popen, PIPE

gnuplot = r'C:\Users\Test\Programs\gnuplot5.4.0\bin\gnuplot'

p = Popen([gnuplot, '-persist'], stdin=PIPE)
cmd = b"""
set xdata time
set timefmt "%H:%M:%S"
set term png size 1600, 1200
set output 'C:\\Users\\Test\\Scripts\\test.png'
set yrange [-1.5:1.5]
set grid
set title ("test_param") noenhanced
plot 'C:\\Users\\Test\\Scripts\\test.dat' using 1:2 with lines lt rgb "blue" lw 2 title "Q value"
set term qt
replot
"""
p.stdin.write(cmd)
p.stdin.flush()

结果:

  • 带有绘图的文件 test.png 将在磁盘上创建,
  • 带有 qt 终端的窗口打开,绘图出现并持续存在。

Although I haven't fully understood all the details, the following works for me (Python 3.6.3 Windows 10).

Some difference to your first code example:

  • Why do you set term qt if you set it to png directly afterwards?
  • in my case, I had to give the full path for the gnuplot executable (because gnuplot path is not set in the Windows environment variables). Mind the r'...' in the Python code
  • I used absolute paths for the data and the plot (otherwise it depends on where you start your Python script) and I guess for the file paths you have to use single quotes (for gnuplot?!) and escape the backslashes \\ (for Python?!). Alternatively, you could use only single foreslashs /, e.g. set output 'C:/Users/W2102029/Scripts/test.png'

If there are better ways and explanations, I am happy to learn from more experienced Windows/Python/gnuplot users.

Code:

from subprocess import Popen, PIPE

gnuplot = r'C:\Users\Test\Programs\gnuplot5.4.0\bin\gnuplot'

p = Popen([gnuplot, '-persist'], stdin=PIPE)
cmd = b"""
set xdata time
set timefmt "%H:%M:%S"
set term png size 1600, 1200
set output 'C:\\Users\\Test\\Scripts\\test.png'
set yrange [-1.5:1.5]
set grid
set title ("test_param") noenhanced
plot 'C:\\Users\\Test\\Scripts\\test.dat' using 1:2 with lines lt rgb "blue" lw 2 title "Q value"
set term qt
replot
"""
p.stdin.write(cmd)
p.stdin.flush()

Result:

  • a file test.png with the plot will be created on disk
  • a window with qt terminal opens and the plot appears and persists.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文