GNUplot 4.2 尝试使用集合样式行时的语法错误



我一直在互联网上寻找答案。

似乎有很多版本的如何在 gnuplot 中绘制颜色线。我有一个包含 4 列的数据文件。我想用不同的颜色绘制每一列。这是我根据 gnuplot 帮助文件使用的代码片段,但在使用这些时出现语法错误。

set style line 1 lt 1 lc 1 lw 3 # red
set style line 2 lt 1 lc 2 lw 3 #green
set style line 3 lt 1 lc 3 lw 3 #blue
set style line 4 lt 1 lc 4 lw 3 #magenta

我已将终端设置为后记。

我已经尝试了这种线型样式的所有组合,包括线型和lc rgb"红色",例如,它们都不起作用!

谁能告诉我出了什么问题?

让我澄清一下,这是一个 python 脚本中的 gnuplot 脚本。 代码如下所示:

plot = open('plot.pg','w')
plot_script = """#!/usr/bin/gnuplot
reset
set terminal postscript 
#cd publienhanced color
set output "roamingresult.ps"
set xlabel "time (seconds)"
set xrange [0:900]
set xtics (0, 60, 120, 180, 240, 300, 360, 420, 480, 540, 600, 660, 720, 780, 840, 900)
set ylabel "AP1         AP2         AP3       AP4"
set yrange [0:5]
set nokey
set grid
set noclip one
set ytics 1
#set style data boxes"""
set style line 1 lt 1 lc 1 lw 3
set style line 2 lt 1 lc 2 lw 3
set style line 3 lt 1 lc 3 lw 3
set style line 4 lt 1 lc 4 lw 3

好的,从您刚刚更新的代码来看,您的问题很清楚。

出了什么问题(快速回答(

您将 Gnuplot 脚本作为字符串包含在 Python 源代码中。""" 标记表示字符串的开始及其结束。问题是你用下面一行终止字符串:

#set style data boxes"""

同样,这个三引号语法标志着 Gnuplot 字符串的结尾,所以接下来应该是 Python 代码。现在,set意味着与Python完全不同的东西(如果你好奇的话,它是一个数学集合(。你的 Gnuplot set语法与 Python 中的含义不匹配,所以这就是为什么它会给你一个语法错误。

将三引号移动到 Gnuplot 脚本的末尾可以解决问题。但是,有一个更简单的解决方案。

更好的方式

你不应该将 Gnuplot 代码直接内联到 Python 脚本中。相反,您应该从另一个文件(该文件完全是 Gnuplot 代码(中读取脚本,并以这种方式处理它。

因此,请保留一个仅包含您的 Gnuplot 代码的文件(例如 plot.script (:

#!/usr/bin/gnuplot
reset
set terminal postscript 
#cd publienhanced color
set output "roamingresult.ps"
set xlabel "time (seconds)"
set xrange [0:900]
set xtics (0, 60, 120, 180, 240, 300, 360, 420, 480, 540, 600, 660, 720, 780, 840, 900)
set ylabel "AP1         AP2         AP3       AP4"
set yrange [0:5]
set nokey
set grid
set noclip one
set ytics 1
#set style data boxes
set style line 1 lt 1 lc 1 lw 3
set style line 2 lt 1 lc 2 lw 3
set style line 3 lt 1 lc 3 lw 3
set style line 4 lt 1 lc 4 lw 3

然后在 Python 中与这个文件交互,如下所示:

plot_script = open("plot.script", "r").read()

最终结果

plot_script包含完全相同的数据,每个文件都包含一种语言独有的代码,并且您的代码更具可读性。

最新更新