gpt4 book ai didi

python - GNUplot stdin,如何绘制多条线?

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:51 24 4
gpt4 key购买 nike

好的,所以我将数据以以下形式传输到 GNUplot,例如:

2013-11-04 20:00:12,875,350,112,29,38,4,44,10,632,121

我在我的 Python 代码中设置了以下内容:

gnuplot.stdin.write("plot '-' u 1:2 t 'aa',\
'' u 1:3 t 'aa in',\
'' u 1:4 t 'bb',\
'' u 1:5 t 'bb in',\
'' u 1:6 t 'cc',\
'' u 1:7 t 'cc in',\
'' u 1:8 t 'dd',\
'' u 1:9 t 'dd in',\
'' u 1:10 t 'ee';\n")

<罢工> 但是我不断收到以下错误:

gnuplot> plot '-' u 1:2 t 'aa',    '' u 1:3 t 'aa in',    '' u 1:4 t 'bb',    '' u 1:5 t 'bb in',    '' u 1:6 t 'cc',    '' u 1:7 t 'cc in',    '' u 1:8 t 'dd',    '' u 1:9 t 'dd in',    '' u 1:10 t 'ee';
^
line 1271: warning: Skipping data file with no valid points

有什么想法吗?

<罢工>###UPDATE:###

<罢工>

基于@Christoph 的反馈;这是我目前使用的代码:

cur.execute(sql)
data = cur.fetchall()

c = 0
for k in data:
dataElement = data[c]
gnuplot.stdin.write("plot '-' u 1:2 t 'aa',\
'' u 1:3 t 'aa in',\
'' u 1:4 t 'bb',\
'' u 1:5 t 'bb in',\
'' u 1:6 t 'cc',\
'' u 1:7 t 'cc in',\
'' u 1:8 t 'dd',\
'' u 1:9 t 'dd in',\
'' u 1:10 t 'ee';\n")

gnuplot.stdin.write("%s,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i\n" % dataElement[:])
gnuplot.stdin.write("e\n")
c = c + 1

<罢工>

最佳答案

您必须为每个'-' 编写自己的数据 block 。 '' 只会让您免于重新输入文件名,但不会重复使用数据。考虑例如以下 gnuplot 脚本:

plot '-', '-'
2
4
6
e
10
12
14
e

还要确保您的分隔符设置正确:set datafile separator ','

这是一个最小的 python 脚本,它绘制了来自 stdin 的两个数据集,列之间用逗号分隔:

#!/usr/bin/env python
import subprocess

gnuplot = subprocess.Popen(["gnuplot"], stdin=subprocess.PIPE)

gp_wrt = gnuplot.stdin.write

gp_wrt("set terminal pngcairo\n")
gp_wrt("set output 'test.png'\n")
gp_wrt("set datafile separator ','\n")
gp_wrt("plot '-' with lines title 'mytitle',\
'-' with lines title 'other title'\n")
for i in range(11):
gp_wrt("{},{}\n".format(i, i**2))
gp_wrt("e\n")

for i in range(11):
gp_wrt("{},{}\n".format(i, (0.5*i)**2))
gp_wrt("e\n")

所以对于你的数据,它可能看起来像

gnuplot.stdin.write("plot '-' u 1:2 t 'aa',\
'' u 1:3 t 'aa in',\
'' u 1:4 t 'bb',\
'' u 1:5 t 'bb in',\
'' u 1:6 t 'cc',\
'' u 1:7 t 'cc in',\
'' u 1:8 t 'dd',\
'' u 1:9 t 'dd in',\
'' u 1:10 t 'ee';\n")

for i in range(10):
for dataElement in data:
gnuplot.stdin.write("%s,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i\n" % dataElement[:])
gnuplot.stdin.write("e\n")

但在那种情况下你可以简单地使用:

gnuplot.stdin.write("plot '-' u 1:2 t 'aa',\
'' u 1:2 t 'aa in',\
'' u 1:2 t 'bb',\
'' u 1:2 t 'bb in',\
'' u 1:2 t 'cc',\
'' u 1:2 t 'cc in',\
'' u 1:2 t 'dd',\
'' u 1:2 t 'dd in',\
'' u 1:2 t 'ee';\n")

for i in range(10):
for dataElement in data:
gnuplot.stdin.write("%s,%i\n" % (dataElement[0], dataElement[i])
gnuplot.stdin.write("e\n")

即每次你只写相关的专栏。

关于python - GNUplot stdin,如何绘制多条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20136763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com