annotate experiments/scripts/cnbh-syllables/results_plotting/spider_plot.py @ 219:776b2783d955

- Make the plotting script actually do something
author tomwalters
date Tue, 14 Sep 2010 01:16:55 +0000
parents b50a09ac97ec
children 6b10f79a123a
rev   line source
tomwalters@100 1 #!/usr/bin/env python
tomwalters@100 2 """
tomwalters@100 3 spider_plot.py
tomwalters@100 4
tomwalters@100 5 Created by Thomas Walters on 2010-09-12.
tomwalters@100 6 """
tomwalters@100 7
tomwalters@212 8 import sys
tomwalters@212 9 import getopt
tomwalters@100 10 import numpy as np
tomwalters@100 11 import pylab as p
tomwalters@100 12 import mpl_toolkits.mplot3d.axes3d as p3
tomwalters@211 13 import matplotlib as mpl
tomwalters@100 14 from matplotlib import cm
tomwalters@100 15 import matplotlib.ticker as ticker
tomwalters@100 16
tomwalters@212 17 help_message = '''
tomwalters@212 18 Plot the spider.
tomwalters@100 19
tomwalters@212 20 Arguments:
tomwalters@212 21 -i --input_file
tomwalters@212 22 -o --output_file
tomwalters@212 23 '''
tomwalters@100 24
tomwalters@212 25 class Usage(Exception):
tomwalters@212 26 def __init__(self, msg):
tomwalters@212 27 self.msg = msg
tomwalters@100 28
tomwalters@100 29
tomwalters@212 30 def main(argv=None):
tomwalters@212 31 if argv is None:
tomwalters@212 32 argv = sys.argv
tomwalters@212 33 try:
tomwalters@212 34 try:
tomwalters@212 35 opts, args = getopt.getopt(argv[1:], "hi:o:",
tomwalters@212 36 ["help", "input_file=", "output_file="])
tomwalters@212 37 except getopt.error, msg:
tomwalters@212 38 raise Usage(msg)
tomwalters@212 39
tomwalters@212 40 # defaults
tomwalters@212 41 input_file = "results_iteration_15.txt"
tomwalters@212 42 output_filename = "results_iteration_15.png"
tomwalters@212 43
tomwalters@212 44 # option processing
tomwalters@212 45 for option, value in opts:
tomwalters@212 46 if option in ("-h", "--help"):
tomwalters@212 47 raise Usage(help_message)
tomwalters@212 48 if option in ("-i", "--input_file"):
tomwalters@212 49 input_file = value
tomwalters@212 50 if option in ("-o", "--output_file"):
tomwalters@212 51 output_file = value
tomwalters@212 52
tomwalters@212 53 except Usage, err:
tomwalters@212 54 print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
tomwalters@212 55 print >> sys.stderr, "\t for help use --help"
tomwalters@212 56 return 2
tomwalters@100 57
tomwalters@212 58 # Read in a file with lines in the form
tomwalters@212 59 # Pitch Scale Percentage
tomwalters@212 60 xs=[]
tomwalters@212 61 ys=[]
tomwalters@212 62 zs=[]
tomwalters@212 63 f = open(input_file, 'r')
tomwalters@212 64 for line in f:
tomwalters@212 65 if line[0] != "#":
tomwalters@212 66 values = line.strip().split(' ')
tomwalters@212 67 xs.append(central_vtl*central_vtl_scaling/float(values[1]))
tomwalters@212 68 ys.append(float(values[0]))
tomwalters@212 69 zs.append(float(values[2]))
tomwalters@100 70
tomwalters@100 71
tomwalters@212 72 # Define a tiny sphere, centered on the origin, which
tomwalters@212 73 # we'll shift to the desired position.
tomwalters@212 74 u=np.r_[0:2*np.pi:50j]
tomwalters@212 75 v=np.r_[0:np.pi:50j]
tomwalters@212 76 sx=0.01*np.outer(np.cos(u),np.sin(v))
tomwalters@212 77 sy=0.01*np.outer(np.sin(u),np.sin(v))
tomwalters@212 78 sz=2.5*np.outer(np.ones(np.size(u)),np.cos(v))
tomwalters@100 79
tomwalters@212 80 fig=p.figure()
tomwalters@212 81 ax = p3.Axes3D(fig, azim=-80, elev=60)
tomwalters@100 82
tomwalters@212 83 colormap = cm.get_cmap('jet', 100)
tomwalters@100 84
tomwalters@212 85 # Note: here I fake out the lack of proper logarihmic scales on 3D axes in
tomwalters@212 86 # matplotlib by just plotting log values on a linear scale and renaming
tomwalters@212 87 # the labels.
tomwalters@212 88 # (This doesn't work: ax.w_yaxis.set_scale('log') ax.w_xaxis.set_scale('log'))
tomwalters@100 89
tomwalters@212 90 # Plot the values seven at a time as dark lines.
tomwalters@212 91 # These are the individual spokes of the spoke pattern.
tomwalters@212 92 n=7
tomwalters@212 93 for i in xrange(0,8):
tomwalters@212 94 ax.plot(np.log(xs[i*n:(i+1)*n]), np.log(ys[i*n:(i+1)*n]), zs[i*n:(i+1)*n], color=[0,0,0])
tomwalters@100 95
tomwalters@212 96 for x,y,z in zip(xs,ys,zs):
tomwalters@212 97 ax.plot(np.log([x, x]), np.log([y, y]), [z, 0], color=[0.8,0.8,0.8])
tomwalters@212 98 ax.plot_surface(sx+np.log(x),sy+np.log(y),sz+z, color=colormap(int(z)), linewidth=0)
tomwalters@100 99
tomwalters@212 100 ax.set_ylabel('GPR/Hz')
tomwalters@212 101 ax.set_xlabel('VTL/cm')
tomwalters@212 102 ax.set_zlabel('Percent correct')
tomwalters@212 103 ax.set_ylim3d(np.log([131,225]))
tomwalters@212 104 ax.set_xlim3d(np.log([9.9, 22.1]))
tomwalters@212 105 ax.set_zlim3d([-1, 101])
tomwalters@212 106 ax.w_zaxis.set_major_locator(ticker.FixedLocator([0, 20, 40, 60, 80, 100]))
tomwalters@100 107
tomwalters@212 108 ax.w_xaxis.set_major_locator(ticker.FixedLocator(np.log([10,15,22])))
tomwalters@212 109 ax.w_xaxis.set_ticklabels(['10', '15', '22'])
tomwalters@212 110 ax.w_yaxis.set_major_locator(ticker.FixedLocator(np.log([132, 172, 224])))
tomwalters@212 111 ax.w_yaxis.set_ticklabels(['132', '172', '224'])
tomwalters@100 112
tomwalters@212 113 #for a in ax.w_xaxis.get_ticklines()+ax.w_xaxis.get_ticklabels():
tomwalters@212 114 # a.set_visible(False)
tomwalters@101 115
tomwalters@212 116 #for a in ax.w_yaxis.get_ticklines()+ax.w_yaxis.get_ticklabels():
tomwalters@212 117 # a.set_visible(False)
tomwalters@212 118
tomwalters@212 119
tomwalters@212 120 #p.show()
tomwalters@212 121 p.savefig(output_filename)
tomwalters@212 122
tomwalters@219 123 if __name__ == "__main__":
tomwalters@219 124 sys.exit(main())