我试图用gnuplot和python从MySQL数据库中提取一些数据gnuplot.py. 我从MySQL数据库中读取了最新的10行数据,并将它们存储在一个临时文件中,该文件应该由gnuplot读取。我可以手动通过终端设置来实现这一点,但我无法理解如何通过python将文件加载到gnuplot中。 如果你能告诉我一个更简单的方法来做这件事,或者只是一个简单的方法,我将非常感激。在
import MySQLdb
import Gnuplot
datafile = open('data', 'w+r')
gp = Gnuplot.Gnuplot(persist=1)
gp('set style data lines')
gp('set term png')
gp('set output "escan_graph.png"')
gp('set datafile separator "|"')
db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="zig")
cursor = db.cursor()
cursor.execute("select * from escan_data")
numrows = int(cursor.rowcount) #get the count of total
# get and display one row at a time
if numrows > 10:
start = numrows-10
else:
start = 0
for x in range(start,numrows):
row = cursor.fetchone()
print row[0], row[1]
datafile.write(str(row[0]) + "|" + str(row[1]) + "\n")
databuff = Gnuplot.Data(datafile.read(), title="test")
gp.plot(databuff)
第[0]行是x轴,第[1]行是y轴。在
这不起作用,因为在
write
之后,file对象的光标位于文件的末尾。如果要真正做到这一点(最好不要将数据写入文件),则需要将光标移回文件的开头:现在您可以
read()
从文件的开头开始。在相关问题 更多 >
编程相关推荐