Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

generate .eps figure with python by using gnuplot

this is a sample python script to generate a bode plot (nothing fancy).

Mathematical data is computed using scipy (=scientific python) and numpy (=powerful multidimensional array to use with scipy).

Data is handled to gnuplot by the package gnuplot-py (http://gnuplot-py.sourceforge.net). Tu use gnuplot-py with the new scipy/numpy packages you have to replace "Numeric" by "numpy" in all source files of gnuplot-py before installing. Examples provided by the package won't work, but you can use it without problem

if you want to *automatically* use that in latex see my Makefile code snippets

   1  
   2  #!/usr/bin/python
   3  # -*- coding: utf-8 -*-
   4  
   5  import scipy, numpy, Gnuplot
   6  
   7  N = 1000
   8  x = numpy.randn(N)
   9  y = numpy.randn(N)
  10  X = numpy.dft.fft(x)[1:N/2+1]
  11  Y = numpy.dft.fft(y)[1:N/2+1]
  12  S = X * Y.conj()
  13  S_abs = abs(S)
  14  S_phase = numpy.arctan(S.imag / S.real)
  15  
  16  # 100 Hz
  17  fsamp = 100.
  18  freq = numpy.arange(1,N/2+1) * fsamp / N
  19  
  20  plot = Gnuplot.Gnuplot()
  21  data_abs = Gnuplot.Data(freq, S_abs, with='lines')
  22  data_phase = Gnuplot.Data(freq, S_phase, with='lines')
  23  plot('set term postscript eps color blacktext "Helvetica" 24')
  24  plot("set output 'bode.eps'")
  25  plot('set multiplot')
  26  plot('set size 1.0,0.5')
  27  plot('set origin 0.0,0.5')
  28  plot("set ylabel 'amplitude'")
  29  plot('set logscale y')
  30  plot.plot(data_abs)
  31  plot('set origin 0.0,0.0')
  32  plot("set ylabel 'phase'")
  33  plot("set xlabel 'Frequency (Hz)'")
  34  plot('set nologscale y')
  35  plot.plot(data_phase)
  36  plot('set nomultiplot')
  37  plot('set output')
  38  plot('quit')
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS