首页 > 代码库 > gnuplot Python API
gnuplot Python API
源文件
1 #!/usr/bin/env python 2 3 from os import popen 4 5 class gnuplot_leon: 6 # Author : Leon Email: yangli0534@gmail.com 7 # a gnuplot api of python 8 9 def __init__(self):10 self.gnuplot = popen(‘gnuplot‘,‘w‘)11 self.write = self.gnuplot.write12 self.flush = self.gnuplot.flush13 self.close = self.gnuplot.close14 #return gp15 16 def set_plot_size(self,x=0.85,y=0.85):17 self.write(‘‘.join([‘set size ‘,str(x),‘ ,‘,str(y),‘\n‘]))18 #self.write(‘‘.join([‘set term png size ‘,str(x),‘ ‘,str(y),‘\n‘]))19 #self.flush()20 21 def set_canvas_size(self,x=600,y=400):22 #self.write(‘set size 0.85, 0.85\n‘)23 self.write(‘‘.join([‘set term png size ‘,str(x),‘ ‘,str(y),‘\n‘]))24 25 26 def set_title(self,title=‘gnuplot‘):27 self.write(‘‘.join([‘set title "{/Times:Italic ‘,str(title), ‘}"\n‘]))28 self.write(‘set title font ",10" norotate tc rgb "white"\n‘)29 30 def set_gif(self):31 self.write(‘set terminal gif animate\n‘)32 33 def set_png(self):34 self.write(‘set terminal png\n‘)35 36 def set_file_name(self,filename=‘gnuplot.gif‘):37 self.write(‘‘.join([‘set output ‘, ‘"‘,str(filename) ,‘"‘,‘\n‘]))38 39 def set_tics_color(self,color=‘orange‘):40 self.write(‘‘.join([‘set tics textcolor rgb ‘,‘"‘,str(color),‘"‘,‘\n‘]))41 42 def set_border_color(self,color=‘orange‘):43 self.write(‘‘.join([‘set border lc rgb ‘,‘"‘,str(color),‘"‘,‘\n‘]))44 45 def set_grid_color(self,color=‘orange‘):46 self.write(‘‘.join([‘set grid lc rgb ‘,‘"‘,str(color),‘"‘,‘\n‘]))47 48 def set_bkgr_color(self,color=‘orange‘):49 self.write(‘‘.join([‘set object 1 rectangle from screen 0,0 to screen 1,1 fc rgb ‘,‘"‘,str(color),‘"‘,‘ behind\n‘]))50 51 def set_xlabel(self,text=‘x‘,color=‘white‘):52 self.write(‘‘.join([‘set xlabel " {/Times:Italic distance: ‘, str(text) ,‘ } " tc rgb ‘,‘"‘,str(color),‘"‘,‘ \n‘]))53 54 def set_ylabel(self,text=‘x‘,color=‘white‘):55 self.write(‘‘.join([‘set ylabel " {/Times:Italic distance: ‘, str(text) ,‘ } " tc rgb ‘,‘"‘,str(color),‘"‘,‘ \n‘]))56 57 def auto_scale_enable(self):58 self.write(‘set autoscale\n‘)59 60 def set_key(self,onoff=‘off ‘,text=‘gnuplot‘,color=‘white‘):61 self.write(‘unset key\n‘)62 self.write(‘‘.join([‘set key ‘,str(onoff),‘ title "‘,str(text),‘" textcolor rgbcolor "‘,str(color),‘"\n‘]))63 #self.write(‘show key\n‘)64 65 def set_x_range(self,start,end):66 self.write(‘‘.join([‘set xrange [ ‘,str(start),‘:‘,str(end),‘]\n‘]))67 68 def set_y_range(self,start,end):69 self.write(‘‘.join([‘set yrange [ ‘,str(start),‘:‘,str(end),‘]\n‘]))70 71 def set_frame_start(self,linestype = ‘l‘,linewidth=3,l_color=‘green,‘):72 #self.write(‘plot "-" w l lw 1.5 lc rgb "green"\n‘)73 self.write(‘‘.join([‘plot "-" notitle w ‘,str(linestype),‘ lw ‘, str(linewidth), ‘ lc rgb ‘, ‘"‘, str(l_color),‘" \n‘])) 74 75 def update_point(self,x,y):76 self.write(‘‘.join([str(x),‘ ‘,str(y),‘\n‘]))77 78 def set_frame_end(self):79 self.write(‘e\n‘)80 81 def set_output_valid(self):82 self.write(‘set output\n‘)83 84 def close(self):85 self.close()
例‘1
1 #!/usr/bin/env python 2 3 import sys 4 import math 5 import os 6 from gnuplot_leon import * 7 8 # Author : Leon Email: yangli0534@gmail.com 9 # fdtd simulation , plotting with gnuplot, writting in python 10 # perl and gnuplot software packages should be installed before running this program11 # 1d fdtd with absorbing boundary and TFSF boundary between [49] and [50]12 # lossy dielectric material localted at > ez[150]13 14 gp = gnuplot_leon()15 16 gp.set_plot_size(1,1)17 gp.set_canvas_size(600,800)18 gp.set_title(‘fdtd simulation by leon : gnuplot class test‘)19 title = ‘fdtd simulation by leon,yangli0534\\\\@gmail.com‘20 #gp.write(‘set terminal gif animate\n‘)21 gp.set_title(title)22 #gp.set_gif()23 gp.set_png()24 gp.set_file_name(‘demo1.png‘)25 gp.set_tics_color(‘white‘)26 gp.set_border_color(‘orange‘)27 gp.set_grid_color(‘orange‘)28 gp.set_bkgr_color(‘gray10‘)29 gp.set_xlabel(‘length‘,‘white‘)30 gp.set_ylabel(‘amplitude‘,‘white‘)31 gp.auto_scale_enable()32 gp.set_key(‘off‘,‘sin(x)‘,‘white‘)33 34 size = 400#physical distance35 36 sinwave=size * [0.00]# 37 38 cnt = 039 elem = 0.0000040 pi = 3.1415926535897932384641 #gp.write(‘‘.join([‘set xrange [0:‘,str(size),‘-1]\n‘]));42 gp.set_x_range(0,size-1)43 #for i in range(0,size):44 # sinwave[i] = 0.045 46 47 48 for mm in range(0, size-1): 49 sinwave[mm] = math.sin(2*pi*mm/size)50 51 gp.set_frame_start(‘l‘, 3, ‘green‘)52 cnt = 0 53 for elem in sinwave:54 gp.update_point(cnt,elem)55 #print ‘‘.join([str(cnt),‘:‘,str(elem),‘\n‘]) 56 cnt += 1 57 gp.set_frame_end() 58 gp.set_key(‘off‘,‘sin(x)‘,‘white‘)59 gp.set_output_valid()60 gp.close()
例2
1 #!/usr/bin/env python 2 3 import sys 4 import math 5 import os 6 from gnuplot_leon import * 7 8 # Author : Leon Email: yangli0534@gmail.com 9 # fdtd simulation , plotting with gnuplot, writting in python 10 # perl and gnuplot software packages should be installed before running this program11 # 1d fdtd with absorbing boundary and TFSF boundary between [49] and [50]12 # lossy dielectric material localted at > ez[150]13 14 gp = gnuplot_leon()15 16 gp.set_plot_size(0.85,0.85)17 gp.set_canvas_size(600,400)18 #gp.set_title(‘fdtd simulation by leon : gnuplot class test‘)19 title = ‘fdtd simulation by leon,yangli0534\\\\@gmail.com‘20 21 gp.set_title(title)22 gp.set_gif()23 #gp.set_png()24 gp.set_file_name(‘demo2.gif‘)25 gp.set_tics_color(‘white‘)26 gp.set_border_color(‘orange‘)27 gp.set_grid_color(‘orange‘)28 gp.set_bkgr_color(‘gray10‘)29 gp.set_xlabel(‘length‘,‘white‘)30 gp.set_ylabel(‘amplitude‘,‘white‘)31 gp.auto_scale_enable()32 gp.set_key(‘off‘,‘sin(x)‘,‘white‘)33 34 size = 400#physical distance35 ez=size * [0.00]#electric field36 hy=size * [0.00]#magnetic field37 ceze=size * [0.00]# 38 cezh=size * [0.00]# 39 chye=size * [0.00]# 40 chyh=size * [0.00]# 41 #sinwave=size * [0.00]# 42 imp0 = 377.0043 LOSS = 0.0144 LOSS_LAYER = 25045 MaxTime = 1800046 cnt = 047 elem = 0.0000048 49 gp.set_x_range(0,size-1)50 for i in range(0,size):51 ez[i] = 0.052 hy[i] = 0.053 #sinwave[i] = 0.054 if (i < 100):55 #$epsR[$i] = 1.0;56 ceze[i] = 1.057 cezh[i] = imp0 58 elif(i < LOSS_LAYER):59 #$epsR[$i] = 1.0;60 ceze[i] = 1.061 cezh[i] = imp0/9.0 62 else :63 #$epsR[$i] = 9.0;64 ceze[i] = (1.0-LOSS)/(1.0+LOSS)65 cezh[i] = imp0 / 9 /(1.0+LOSS)66 if( i < LOSS_LAYER):67 chye[i] = 1.0/imp068 chyh[i] = 1.069 else:70 chye[i] = 1.0/imp0/(1.0+LOSS)71 chyh[i] = (1.0-LOSS)/(1.0+LOSS)72 for qTime in range(0, MaxTime): 73 # update magnetic field74 for mm in range(0, size-1):75 hy[mm] = hy[mm]*chyh[mm] + (ez[mm+1]-ez[mm])*chye[mm]76 #sinwave[mm] = math.sin(mm/size*2*pi)77 hy[49] = hy[49]-math.exp(-(qTime - 30.0)*(qTime - 30.0)/100.0)/imp078 # update electric field79 ez[0] = ez[1]#abc80 #$ez[$size-1] = $ez[$size-2];81 for mm in range(1, size-1):82 ez[mm] = ez[mm]*ceze[mm] + (hy[mm] - hy[mm-1])*cezh[mm] 83 if(qTime % 30 == 0): 84 gp.set_frame_start(‘l‘, 3, ‘green‘)85 cnt = 0 86 for elem in ez:87 gp.update_point(cnt,elem)88 cnt += 1 89 gp.set_frame_end() 90 ez[50] = ez[50]+math.exp(-(qTime +0.5-(-0.5)- 30.0)*(qTime +0.5-(-0.5)- 30.0)/100.0);91 #gp.write(‘set output\n‘)92 #gp.close()93 gp.set_output_valid()94 gp.close()
gnuplot Python API
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。