首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PyGnuplot结束gnuplot进程

使用PyGnuplot结束gnuplot进程
EN

Stack Overflow用户
提问于 2018-08-19 03:26:18
回答 3查看 717关注 0票数 0

我使用gnuplot (通过Python和PyGnuplot)作为来自Arduino的数字数据的实时绘图仪。另外,我想在任意点保存绘图。每当我尝试在实时绘图时从gnuplot保存pdf/jpg时,gnuplot就会停止工作。

我认为在将绘图保存为pdf之前,有必要重新启动gnuplot-process -使用PyGnuplot.c('quit')或类似命令根本不会起作用。当我使用不同的python脚本进行实时绘图和保存pdf时,一切都很好,但我知道没有必要运行两个脚本。

下面是一个运行(分别不运行)的最小示例:

代码语言:javascript
复制
import random,time
import PyGnuplot as gp
filename = "data.txt"

def rand():
    return random.random()

def writetxt(file,info):
    fobj = open(file, "a")
    fobj.write(info)
    fobj.close()

def liveplot(file):
    gp.c('plot "' + file + '" with lines')

def plotinfile(dat):
    gp.c('set terminal pdf')
    gp.c('set output "example.pdf"')
    gp.c('plot "' + dat + '" with lines')
    gp.c('unset output')
    gp.c('set terminal x11')

for i in range(10): 
         onerow = str(i) + " " + str(rand()) + " " + str(rand()) #simulate incoming data
         print(onerow) #print in console for comparison
         writetxt(filename ,onerow + "\n") #write data into txt-file
         liveplot(filename) #liveplot the data from txt-file
         if i == 4: #simulate an arbitrary point for saving
            plotinfile(filename) #save graph from txt-file to pdf
         time.sleep(1)   #incoming data occurs only every second
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-08-20 02:30:08

我相信您希望将曲线添加到您的曲线图或添加数据点,并间歇性地保存。如果是这样的话,这样的代码应该就足够了:

代码语言:javascript
复制
python
>>> import PyGnuplot as gp
>>> gp.c('plot sin(x)')
>>> gp.pdf('sin_graph.pdf')   # discretionary save point 1 
>>> gp.c('replot cos(x)')     # add a new curve  
>>> gp.pdf('sin_and_cos_graph.pdf') # discretionary save point 2

如果您只有pdfcario终端而没有pdf,那么您需要通过自己编写pdf来解决pygnuplot c.pdf脚本的问题:

代码语言:javascript
复制
>>>filename = 'sin_graph.pdf'
>>>gp.c('set term pdf enhanced size 14cm, 9cm color solid')
>>>gp.c('set out "' + filename + '";')
>>>gp.c('replot;')
>>>term='x11`  # or whatever term you typically use
>>>gp.c('set term ' + str(term) + '; replot')

您可以将此例程包装到一个函数中,并将filenameterminal作为参数。无论如何,每次打印pdf时,您都必须执行这一系列命令。

票数 0
EN

Stack Overflow用户

发布于 2018-08-19 06:39:31

gnuplot不会关闭输出文件,直到(1)终端类型更改为其他类型或(2)有一个明确的命令"unset output“。否则,gnuplot将等待查看是否有另一个plot命令将进入相同的输出文件。

票数 0
EN

Stack Overflow用户

发布于 2018-08-20 04:56:48

问题是:用过的终端不在手边。使用另一个终端解决了此问题。谢谢你的帮助。在PyGnuplot中使用的是终端x11 --这对我来说是不可用的(我仍然不知道为什么,不知道如何安装它,也不知道如何使用PyGnuplot结束gnuplot进程……)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51911908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档