tomwalters@100: #!/usr/bin/env python tomwalters@100: """ tomwalters@100: spider_plot.py tomwalters@100: tomwalters@100: Created by Thomas Walters on 2010-09-12. tomwalters@100: """ tomwalters@100: tomwalters@101: import sys tomwalters@101: import getopt tomwalters@110: import matplotlib as mpl tomwalters@110: matplotlib.use('PDF') tomwalters@100: import numpy as np tomwalters@100: import pylab as p tomwalters@100: import mpl_toolkits.mplot3d.axes3d as p3 tomwalters@100: from matplotlib import cm tomwalters@100: import matplotlib.ticker as ticker tomwalters@100: tomwalters@101: help_message = ''' tomwalters@101: Plot the spider. tomwalters@100: tomwalters@101: Arguments: tomwalters@101: -i --input_file tomwalters@101: -o --output_file tomwalters@101: ''' tomwalters@100: tomwalters@101: class Usage(Exception): tomwalters@101: def __init__(self, msg): tomwalters@101: self.msg = msg tomwalters@100: tomwalters@100: tomwalters@101: def main(argv=None): tomwalters@101: if argv is None: tomwalters@101: argv = sys.argv tomwalters@101: try: tomwalters@101: try: tomwalters@101: opts, args = getopt.getopt(argv[1:], "hi:o:", tomwalters@101: ["help", "input_file=", "output_file="]) tomwalters@101: except getopt.error, msg: tomwalters@101: raise Usage(msg) tomwalters@101: tomwalters@101: # defaults tomwalters@101: input_file = "results_iteration_15.txt" tomwalters@101: output_filename = "results_iteration_15.png" tomwalters@101: tomwalters@101: # option processing tomwalters@101: for option, value in opts: tomwalters@101: if option in ("-h", "--help"): tomwalters@101: raise Usage(help_message) tomwalters@101: if option in ("-i", "--input_file"): tomwalters@101: input_file = value tomwalters@101: if option in ("-o", "--output_file"): tomwalters@101: output_file = value tomwalters@101: tomwalters@101: except Usage, err: tomwalters@101: print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg) tomwalters@101: print >> sys.stderr, "\t for help use --help" tomwalters@101: return 2 tomwalters@109: tomwalters@109: central_vtl=15 tomwalters@109: central_vtl_scaling=112.32 tomwalters@109: tomwalters@101: # Read in a file with lines in the form tomwalters@101: # Pitch Scale Percentage tomwalters@101: xs=[] tomwalters@101: ys=[] tomwalters@101: zs=[] tomwalters@101: f = open(input_file, 'r') tomwalters@101: for line in f: tomwalters@101: if line[0] != "#": tomwalters@101: values = line.strip().split(' ') tomwalters@101: xs.append(central_vtl*central_vtl_scaling/float(values[1])) tomwalters@101: ys.append(float(values[0])) tomwalters@101: zs.append(float(values[2])) tomwalters@100: tomwalters@100: tomwalters@101: # Define a tiny sphere, centered on the origin, which tomwalters@101: # we'll shift to the desired position. tomwalters@101: u=np.r_[0:2*np.pi:50j] tomwalters@101: v=np.r_[0:np.pi:50j] tomwalters@101: sx=0.01*np.outer(np.cos(u),np.sin(v)) tomwalters@101: sy=0.01*np.outer(np.sin(u),np.sin(v)) tomwalters@101: sz=2.5*np.outer(np.ones(np.size(u)),np.cos(v)) tomwalters@100: tomwalters@101: fig=p.figure() tomwalters@101: ax = p3.Axes3D(fig, azim=-80, elev=60) tomwalters@100: tomwalters@101: colormap = cm.get_cmap('jet', 100) tomwalters@100: tomwalters@101: # Note: here I fake out the lack of proper logarihmic scales on 3D axes in tomwalters@101: # matplotlib by just plotting log values on a linear scale and renaming tomwalters@101: # the labels. tomwalters@101: # (This doesn't work: ax.w_yaxis.set_scale('log') ax.w_xaxis.set_scale('log')) tomwalters@100: tomwalters@101: # Plot the values seven at a time as dark lines. tomwalters@101: # These are the individual spokes of the spoke pattern. tomwalters@101: n=7 tomwalters@101: for i in xrange(0,8): tomwalters@101: 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: tomwalters@101: for x,y,z in zip(xs,ys,zs): tomwalters@101: ax.plot(np.log([x, x]), np.log([y, y]), [z, 0], color=[0.8,0.8,0.8]) tomwalters@101: ax.plot_surface(sx+np.log(x),sy+np.log(y),sz+z, color=colormap(int(z)), linewidth=0) tomwalters@100: tomwalters@101: ax.set_ylabel('GPR/Hz') tomwalters@101: ax.set_xlabel('VTL/cm') tomwalters@101: ax.set_zlabel('Percent correct') tomwalters@101: ax.set_ylim3d(np.log([131,225])) tomwalters@101: ax.set_xlim3d(np.log([9.9, 22.1])) tomwalters@101: ax.set_zlim3d([-1, 101]) tomwalters@101: ax.w_zaxis.set_major_locator(ticker.FixedLocator([0, 20, 40, 60, 80, 100])) tomwalters@100: tomwalters@101: ax.w_xaxis.set_major_locator(ticker.FixedLocator(np.log([10,15,22]))) tomwalters@101: ax.w_xaxis.set_ticklabels(['10', '15', '22']) tomwalters@101: ax.w_yaxis.set_major_locator(ticker.FixedLocator(np.log([132, 172, 224]))) tomwalters@101: ax.w_yaxis.set_ticklabels(['132', '172', '224']) tomwalters@100: tomwalters@101: #for a in ax.w_xaxis.get_ticklines()+ax.w_xaxis.get_ticklabels(): tomwalters@101: # a.set_visible(False) tomwalters@100: tomwalters@101: #for a in ax.w_yaxis.get_ticklines()+ax.w_yaxis.get_ticklabels(): tomwalters@101: # a.set_visible(False) tomwalters@101: tomwalters@101: tomwalters@101: #p.show() tomwalters@101: p.savefig(output_filename) tomwalters@101: tomwalters@108: if __name__ == "__main__": tomwalters@108: sys.exit(main())