Python产生Gnuplot绘图数据的方法

gnuplot的绘图可以直接选取文件绘图,文件格式要求如下:

x1 y1

x2 y2

......

xn yn

在python中利用文件操作的write方法可以非常方便实现,在此记录一下,这里强调s的是gnuplot的绘图,用python只是产生一下数据w而已,并不讨论python绘图和gnuplot的优劣,自己也是刚学gnuplot

Python代码

import os
import random
import math

os.chdir(u"d:Filesgnuplot")

file = open("random_number.txt",'w+')

for i in range(200):
 file.write(str(i+random.random()))
 file.write(' ')
 file.write(str(math.log10(i+random.randint(-3,7))))
 file.write('n')

file.close()

打开d:Filesgnuplot路径下生成的文件,内容如下:

Python产生Gnuplot绘图数据

注意gnuplot的工作路径也要选择到d:Filesgnuplot ,然后执行下列代码

gnuplot代码

plot 'random_number.txt'

gnuplot输出结果如下:

Python产生Gnuplot绘图数据

以上这篇Python产生Gnuplot绘图数据的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持来客网。