Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过python使用gnuplot(python gnuplot)一次打印所有文件_Python_Python 2.7_Gnuplot - Fatal编程技术网

通过python使用gnuplot(python gnuplot)一次打印所有文件

通过python使用gnuplot(python gnuplot)一次打印所有文件,python,python-2.7,gnuplot,Python,Python 2.7,Gnuplot,要求使用python脚本一次从目录中的所有特定.csv文件绘制两列。这非常有效: plot_str = 'plot for [i=0:*] file=sprintf("cpu%03d.csv",i) title_s=sprintf("cpu%03d",i) file using 16:7 pt 7 ps 1 title title_s' g_plot.__call__(plot_str) 其中g_图为: g_plot = Gnuplot.Gnuplot(persist=1) 然而,另外,在前

要求使用python脚本一次从目录中的所有特定.csv文件绘制两列。这非常有效:

plot_str = 'plot for [i=0:*] file=sprintf("cpu%03d.csv",i) title_s=sprintf("cpu%03d",i) file using 16:7 pt 7 ps 1 title title_s'
g_plot.__call__(plot_str)
其中
g_图
为:

g_plot = Gnuplot.Gnuplot(persist=1)
然而,另外,在前面的步骤中有时涉及cpu掩码,导致文件名不连续。例如:

$ ls -1 cpu???.csv
cpu001.csv
cpu003.csv
cpu005.csv
cpu007.csv
在这种情况下,上述命令在序列中第一个丢失的文件时中止。
我尝试过几种方法,大多数都是从stackoverflow中获得的。我的第二次至最新尝试(包括调试标志等):

导致:

flag 1
cpu001.csv
cpu003.csv
cpu005.csv
cpu007.csv

flag 2
cpu001
cpu003
cpu005
cpu007

flag 3
plot for [i=1:words(file_list)] 'word(file_list,i)' using 16:7 pt 7 ps 1 title word(title_list,i)
         line 0: undefined variable: cpu001
gnuplot> cpu003.csv
         ^
         line 0: invalid command
gnuplot> cpu005.csv
         ^
         line 0: invalid command
gnuplot> cpu007.csv
         ^
         line 0: invalid command
         line 0: undefined variable: cpu001
gnuplot> cpu003
         ^
         line 0: invalid command
gnuplot> cpu005
         ^
         line 0: invalid command
gnuplot> cpu007
         ^
         line 0: invalid command
         line 0: undefined variable: file_list
flag 1
['cpu001.csv', 'cpu003.csv', 'cpu005.csv', 'cpu007.csv']
flag 2
['cpu001', 'cpu003', 'cpu005', 'cpu007']
flag 3
plot for [i=1:words(file_list)] 'word(file_list,i)' using 16:7 pt 7 ps 1 title word(title_list,i)
gnuplot> file_list = ['cpu001.csv', 'cpu003.csv', 'cpu005.csv', 'cpu007.csv']
                     ^
         line 0: invalid expression
gnuplot> title_list = ['cpu001', 'cpu003', 'cpu005', 'cpu007']
                      ^
         line 0: invalid expression
         line 0: undefined variable: file_list
我的最新尝试:

#    the following commands are the current experiment.
    file_list = subprocess.check_output('ls -1 cpu???.csv',shell=True).splitlines()
    title_list = subprocess.check_output('ls -1 cpu???.csv | sed -e \'s/.csv//\'',shell=True).splitlines()
#    file_list = file_list.splitlines()

    plot_str = "plot for [i=1:words(file_list)] 'word(file_list,i)' using 16:7 pt 7 ps 1 title word(title_list,i)"
#    plot_str = 'plot for [i=1:words(file_list)] file=word(file_list,i) title_s=word(title_list,i) file using 16:7 pt 7 ps 1 title title_s'

    print('flag 1')
    print(file_list)
    print('flag 2')
    print(title_list)
    print('flag 3')
    print(plot_str)

    g_plot('file_list = {}'.format(file_list))
    g_plot('title_list = {}'.format(title_list))
#    g_plot.__call__('file_list = "{0}"'.format(file_list))
#    g_plot('print file_list')
#    g_plot.__call__('title_list = "{0}"'.format(title_list))
#    g_plot('print title_list')

    g_plot.__call__(plot_str)
导致:

flag 1
cpu001.csv
cpu003.csv
cpu005.csv
cpu007.csv

flag 2
cpu001
cpu003
cpu005
cpu007

flag 3
plot for [i=1:words(file_list)] 'word(file_list,i)' using 16:7 pt 7 ps 1 title word(title_list,i)
         line 0: undefined variable: cpu001
gnuplot> cpu003.csv
         ^
         line 0: invalid command
gnuplot> cpu005.csv
         ^
         line 0: invalid command
gnuplot> cpu007.csv
         ^
         line 0: invalid command
         line 0: undefined variable: cpu001
gnuplot> cpu003
         ^
         line 0: invalid command
gnuplot> cpu005
         ^
         line 0: invalid command
gnuplot> cpu007
         ^
         line 0: invalid command
         line 0: undefined variable: file_list
flag 1
['cpu001.csv', 'cpu003.csv', 'cpu005.csv', 'cpu007.csv']
flag 2
['cpu001', 'cpu003', 'cpu005', 'cpu007']
flag 3
plot for [i=1:words(file_list)] 'word(file_list,i)' using 16:7 pt 7 ps 1 title word(title_list,i)
gnuplot> file_list = ['cpu001.csv', 'cpu003.csv', 'cpu005.csv', 'cpu007.csv']
                     ^
         line 0: invalid expression
gnuplot> title_list = ['cpu001', 'cpu003', 'cpu005', 'cpu007']
                      ^
         line 0: invalid expression
         line 0: undefined variable: file_list
编辑:尝试使用Christoph的答案导致:

gnuplot> cpu003.csv
         ^
         line 0: invalid command
gnuplot> cpu005.csv
         ^
         line 0: invalid command
gnuplot> cpu007.csv
         ^
         line 0: invalid command
gnuplot> ""
         ^
         line 0: invalid command
gnuplot> cpu003
         ^
         line 0: invalid command
gnuplot> cpu005
         ^
         line 0: invalid command
gnuplot> cpu007
         ^
         line 0: invalid command
gnuplot> ""
         ^
         line 0: invalid command
其他说明:

  • 希望这能与python版本2.7及更高版本一起使用。i、 e.i 尝试了使用“从子流程运行”的方法,但版本2.7没有 它
  • CPU的数量可能相当高,超过100个。一小部分是 现在已经被用来测试了
  • 而同一个python脚本确实知道前面的CPU掩码 步骤,首选的是不要依赖它,因为这里可能有 缺少相关.csv文件的其他原因
问题:如何将文件列表和标题列表字符串从python传递到gnuplot,以便打印它们?

即使是上面示例中的整数数组,即1,3,5,7,也可能足够好,因为文件名和标题可以在gnuplot端重新构造。

打印时,必须引用指定给gnuplot变量的字符串,但在
word
函数周围不能有引号

file_list = subprocess.check_output('ls -1 cpu???.csv',shell=True)
title_list = subprocess.check_output('ls -1 cpu???.csv | sed -e \'s/.csv//\'',shell=True)

plot_str = "plot for [i=1:words(file_list)] word(file_list,i) using 16:7 pt 7 ps 1 title word(title_list,i)"

g_plot('file_list = "{0}"'.format(file_list))
g_plot('title_list = "{0}"'.format(title_list))
g_plot.__call__(plot_str)

第一种方法的问题是仍然包含换行符,因此大约将其传递给gnuplot:

file_list = cpu001.csv
cpu003.csv
cpu005.csv
cpu007.csv
因此gnuplot抱怨它认为是命令的后续3行。(第二个字符串也是如此)

在我看来,第二种方法的问题是从.splitlines()操作返回的字符串格式非常奇怪。所以gnuplot在抱怨,因为它不明白传递给它的是什么

因为我们希望将一个干净的字符串传递给gnuplot,所以我们面临的挑战是找到一个可以实现这一点的函数
.replace('\n','')
似乎可以完成这项工作。一个字符串就足够了,因为我们可以使用实际的plot for loop命令轻松地重建完整文件名。(请注意,在plot for循环中,从整数迭代器到字符串迭代器的更改):


谢谢你的回复。我肯定我试过你建议的变种。我把你的答案直接复制到剧本里了。我将编辑我的问题,以包括现在略有不同的错误列表。我无法测试我的解决方案,所以我只是立即更正了我看到的两个错误。“我稍后会检查的”。@克里斯托夫:我已经用你的答案的结果编辑了我的问题。我有两种方法:首先,我用您建议的更改手动编辑代码;第二,如果我犯了错误,我会注释掉我的代码,并将答案代码直接复制粘贴到python脚本中。