lolamontes69

graphplotting from Ch 8 adapted for gnuplot. Prog Col Intel

Aug 12th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. """ The graph-plotting functions from Chapter 8 adapted to use GNUPLOT instead of MATPLOTLIB.
  2.    Dependencies gnuplot, gnuplot.py, numpy.
  3.    Gnuplot is a lot lighter than matplotlib and a lot easier to install :)
  4.    See end for usage
  5. """
  6.  
  7. from numpy import *
  8. import Gnuplot, Gnuplot.funcutils
  9.  
  10. def cumulativegraph(data,vec1,high,k=5,weightf=gaussian):
  11.     t1=arange(0.0,high,0.1)
  12.     cprob=array([probguess(data,vec1,0,v,k,weightf) for v in t1])
  13.     coords=[]
  14.     for a in range(len(t1)):
  15.         point=(t1[a],cprob[a])
  16.         coords.append(point)
  17.     # plot(t1,cprob) in gnuplot
  18.     g = Gnuplot.Gnuplot(debug=1)
  19.     g.title('Cumulative Graph   ')
  20.     g('set data style lines')
  21.     g.plot(coords)
  22.     t=raw_input('Press enter to continue')
  23.  
  24. def probabilitygraph(data,vec1,high,k=5,weightf=gaussian,ss=5.0):
  25.     # Make a range for the prices
  26.     t1=arange(0.0,high,0.1)
  27.  
  28.     # Get the probabilities for the entire range
  29.     probs=[probguess(data,vec1,v,v+0.1,k,weightf) for v in t1]
  30.  
  31.     # Smooth them by adding the gaussian of the nearby probabilities
  32.     smoothed=[]
  33.     for i in range(len(probs)):
  34.         sv=0.0
  35.         for j in range(0,len(probs)):
  36.             dist=abs(i-j)*0.1
  37.             weight=gaussian(dist,sigma=ss)
  38.             sv+=weight*probs[j]
  39.         smoothed.append(sv)
  40.     smoothed=array(smoothed)
  41.    
  42.     coords=[]
  43.     for a in range(len(t1)):
  44.         point=(t1[a],smoothed[a])
  45.         coords.append(point)
  46.     # plot(t1,smoothed) in gnuplot
  47.     g = Gnuplot.Gnuplot(debug=1)
  48.     g.title('Probability Graph   ')
  49.     g('set data style lines')
  50.     g.plot(coords)
  51.     t=raw_input('Press enter to continue')
  52.  
  53. """
  54. Usage
  55. *****
  56. The usage is the same for the original functions
  57.  
  58. import numpredict4 as numpredict
  59. data = numpredict.wineset1()
  60.  
  61. numpredict.cumulativegraph(data,(1,1),200)
  62. numpredict.cumulativegraph(data,(95,3),300)
  63.  
  64. numpredict.probabilitygraph(data,(1,1),200)
  65. numpredict.probabilitygraph(data,(95,4),300)
  66. """
Advertisement
Add Comment
Please, Sign In to add comment