python - Gnuplot 从 python 发送换行符和 ",\"

标签 python gnuplot

通常在使用交互式 gnuplot 时我会这样做:

gnuplot> plot "state_log_6548032.data" using 4 with lines lt -1 lw 0.5 title "X axis" ,\
>"state_log_6548032.data" using 5 with lines lt 1 lw 0.5 title "Y axis" ,\
>"state_log_6548032.data" using 6 with lines lt 2 lw 0.5 title "Z axis"

但是,当我尝试使用子进程从 python 执行相同操作时:

gnuplot.write( "plot \"%s\" using 1 with lines lt -1 lw 0.5 title 'X axis' ,\ \n" %filename )
gnuplot.write( "plot \"%s\" using 2 with lines lt 1 lw 0.5 title 'Y axis' ,\ \n" %filename )
gnuplot.write( "plot \"%s\" using 3 with lines lt 2 lw 0.5 title 'Z axis' \n" %filename )

我收到以下错误:

gnuplot> plot "state_log_6548032.data" using 1 with lines lt -1 lw 0.5 title 'X axis' ,\ 
                                                                                       ^
         line 0: invalid character \

gnuplot> plot "state_log_6548032.data" using 2 with lines lt 1 lw 0.5 title 'Y axis' ,\ 
                                                                                      ^
         line 0: invalid character \

我花了很多时间试图弄清楚这是否是 python 的问题,但我发现这是 gnuplot 的问题,它在从控制台调用时出于某种原因使用转义字符,但不是必需的就我而言。但是我的问题仍然存在。我如何通过管道指令从 python 子进程连续绘制上面的数据^^,或者通过从 python 创建 gnu 文件并调用 gnuplot 子进程来使用该文件?

编辑:

对于任何可能会陷入这个简单的小事情的人:正如让这个社区保持活力的好人在下面解释的那样,当您使用“\”时,Python 会转义“\”。 所以解决方案很简单:

gnuplot.write( "plot \"%s\" using 1 with lines lt -1 lw 0.5 title 'X axis' ,\\\n" %filename )
        gnuplot.write( "\"%s\" using 2 with lines lt 1 lw 0.5 title 'Y axis' ,\\\n" %filename )
        gnuplot.write( "\"%s\" using 3 with lines lt 2 lw 0.5 title 'Z axis' \n" %filename )

最佳答案

在您发布的命令行 gnuplot 示例中,您转义换行符以避免在屏幕上出现一英里长的行,但仍将数据作为单行发送给 gnuplot。

如果您手动输入数据,这很酷,但如果您以编程方式进行,您为什么要关心呢?只需发送一行,Python 不会提示可读性:)。如果您对问题出在哪里感到好奇,那是因为 Python 还使用 \ 作为转义序列,所以字符永远不会到达 gnuplot .

也就是说,有 python-gnuplot 库可以为您处理所有这些肮脏的工作。检查出来!

关于python - Gnuplot 从 python 发送换行符和 ",\",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6838194/

相关文章:

python - 用户无法上传到 Pythonanywhere 上的媒体目录

plot - gnuplot 堆叠直方图重叠

linux - 在 Gnuplot 中命名图例条目,同时从数据文件中绘制

gnuplot - 如何在 gnuplot 中制作带边框的框架?

image - 使用 gnuplot pm3d 和 pdf 输出生成的图像中有问题的莫尔图案

python - PyQt 相当于 WinForms 激活事件是什么?

Python,在for循环中有一个条件

python - Gunicorn:多个后台工作线程

Python在两个整数的两个元组之间交换元素

c - 如何将非标准头文件链接到 C 编译器