bifandvel.py -- support for multiple temperature overlays
[quplot.git] / map.py
blobf681c0d796860703f926a895676f966fd809fba7
1 import matplotlib as mpl
2 import matplotlib.mlab as mlab
3 import matplotlib.pyplot as plt
4 import matplotlib.axes as axe
5 import matplotlib.collections as col
6 import numpy as np
7 import random
8 from pylab import *
9 from matplotlib.pyplot import *
11 FIN = '../data/dual_bif_alpha_l_55_65_1_4_f0005.dat'
12 FOUT = '../db5.png'
14 samples = 10
15 ticksize = 14
16 labelsize = 24
17 title = "a = 1.15, w = 0.1, F = 0.00, eta1 = 1.0, eta2 = 1.5, theta = 0"
18 xlabel = r'$\alpha$'
19 ylabel = r'$l$'
21 xtfreq = 2
22 ytfreq = 1
23 xpad = 0.01
24 ypad = 0.01
25 ms = 30
26 ystep = 0.1
27 xstep = 0.1
28 res = (1024/80,768/80) # default dpi is 80
29 method = 'direct' # mean, direct
30 has_y = False
31 has_col = False
32 has_bias = False
34 ################################################################################
36 def assign_label(color):
37 ret_label = 'DUMMY'
38 if (color == 'grey'):
39 ret_label = r'$|v| = 0$'
40 if (color == 'blue'):
41 ret_label = r'$|v| = -1$'
42 if (color == 'red'):
43 ret_label = r'$|v| = 1$'
44 if (color == 'orange'):
45 ret_label = r'$|v| = 0.5$'
46 if (color == 'green'):
47 ret_label = r'$|v| = -0.5$'
48 if (color == 'black'):
49 ret_label = r'other rational $v$'
50 if (color == 'cyan'):
51 ret_label = r'$v < 0$'
52 if (color == 'magenta'):
53 ret_label = r'$v > 0$'
54 return (ret_label)
56 def assign_color2(colors,xar,yar,vel,clist):
57 print "---------------------------------------------------------"
58 unique_colors = unique(colors)
59 #print range(len(clist))
60 ret_xcolors = [ [] for DUMMYVAR in range(len(clist)) ]
61 ret_ycolors = [ [] for DUMMYVAR in range(len(clist)) ]
62 i = 0
63 assert len(xar) == len(yar) == len(colors)
64 xarlen_trunc = len(xar)
65 while i < xarlen_trunc:
66 tmp = []
67 tmpv = []
68 for j in range(samples):
69 assert i+j <= len(xar)
70 #assert yar[i+j] == yar[i] and xar[i+j] == xar[i]
71 tmp.append(colors[i+j])
72 tmpv.append(vel[i+j])
73 # print len(tmp), samples
74 assert len(tmp) == samples
75 ccount = len(unique(tmp))
76 if ccount == 2 and 'green' in tmp and 'orange' in tmp:
77 ret_xcolors[6].append(xar[i])
78 ret_ycolors[6].append(yar[i])
79 elif ccount == 2 and 'blue' in tmp and 'red' in tmp:
80 ret_xcolors[7].append(xar[i])
81 ret_ycolors[7].append(yar[i])
82 elif ccount == 2 and 'blue' in tmp and 'black' in tmp:
83 if tmp.count('blue') >= samples/2:
84 ret_xcolors[2].append(xar[i])
85 ret_ycolors[2].append(yar[i])
86 else:
87 ret_xcolors[0].append(xar[i])
88 ret_ycolors[0].append(yar[i])
89 elif ccount == 2 and 'red' in tmp and 'black' in tmp:
90 if tmp.count('red') >= samples/2:
91 ret_xcolors[5].append(xar[i])
92 ret_ycolors[5].append(yar[i])
93 else:
94 ret_xcolors[0].append(xar[i])
95 ret_ycolors[0].append(yar[i])
96 elif ccount == 2 and 'green' in tmp and 'black' in tmp:
97 if tmp.count('green') >= samples/2:
98 ret_xcolors[3].append(xar[i])
99 ret_ycolors[3].append(yar[i])
100 else:
101 ret_xcolors[0].append(xar[i])
102 ret_ycolors[0].append(yar[i])
103 elif ccount == 2 and 'orange' in tmp and 'black' in tmp:
104 if tmp.count('orange') >= samples/2:
105 ret_xcolors[4].append(xar[i])
106 ret_ycolors[4].append(yar[i])
107 else:
108 ret_xcolors[0].append(xar[i])
109 ret_ycolors[0].append(yar[i])
110 elif ccount == 3 and 'green' in tmp and 'orange' in tmp and 'grey':
111 ret_xcolors[6].append(xar[i])
112 ret_ycolors[6].append(yar[i])
113 elif ccount == 3 and 'blue' in tmp and 'red' in tmp and 'grey':
114 ret_xcolors[7].append(xar[i])
115 ret_ycolors[7].append(yar[i])
116 elif ccount == 4 and 'blue' in tmp and 'red' in tmp and 'green' in tmp and 'orange' in tmp:
117 print tmp
118 ret_xcolors[11].append(xar[i])
119 ret_ycolors[11].append(yar[i])
120 elif ccount == 4 and 'green' in tmp and 'orange' in tmp and 'grey' in tmp and 'black' in tmp:
121 ret_xcolors[6].append(xar[i])
122 ret_ycolors[6].append(yar[i])
123 elif ccount == 4 and 'blue' in tmp and 'red' in tmp and 'grey' in tmp and 'black' in tmp:
124 ret_xcolors[7].append(xar[i])
125 ret_ycolors[7].append(yar[i])
126 elif ccount == 1 and 'grey' in tmp:
127 ret_xcolors[1].append(xar[i])
128 ret_ycolors[1].append(yar[i])
129 elif ccount == 1 and 'blue' in tmp:
130 ret_xcolors[2].append(xar[i])
131 ret_ycolors[2].append(yar[i])
132 elif ccount == 1 and 'green' in tmp:
133 ret_xcolors[3].append(xar[i])
134 ret_ycolors[3].append(yar[i])
135 elif ccount == 1 and 'orange' in tmp:
136 ret_xcolors[4].append(xar[i])
137 ret_ycolors[4].append(yar[i])
138 elif ccount == 1 and 'red' in tmp:
139 ret_xcolors[5].append(xar[i])
140 ret_ycolors[5].append(yar[i])
141 elif len(unique(tmpv)) >= samples:
142 #print unique(tmpv)
143 ret_xcolors[0].append(xar[i])
144 ret_ycolors[0].append(yar[i])
145 elif ccount == 1 and 'black' in tmp:
146 ret_xcolors[0].append(xar[i])
147 ret_ycolors[0].append(yar[i])
148 else:
149 print unique(tmp)
150 #print unique(tmpv)
151 ret_xcolors[0].append(xar[i])
152 ret_ycolors[0].append(yar[i])
153 i = i+samples
154 #print "increment:",i
155 for j in range(len(clist)):
156 print clist[j],":",len(ret_xcolors[j])
157 #print "unmatched velocities:"
158 #print ret_xcolors[13], ret_ycolors[13]
159 print "---------------------------------------------------------"
160 return (ret_xcolors, ret_ycolors)
162 def assign_color(colors,xar,yar):
163 print "---------------------------------------------------------"
164 unique_colors = unique(colors)
165 ret_xcolors = [ [] for DUMMYVAR in range(len(unique_colors)) ]
166 ret_ycolors = [ [] for DUMMYVAR in range(len(unique_colors)) ]
167 for j in range(len(unique_colors)):
168 tmpx = []
169 tmpy = []
170 k = 0
171 for i in range(len(colors)):
172 if (colors[i] == unique_colors[j]):
173 k = k+1
174 tmpxcolors = ret_xcolors[j]
175 tmpycolors = ret_ycolors[j]
176 #print tmpxcolors
177 if len(tmpxcolors) > 0:
178 if (tmpxcolors[-1] != xar[i]) or (tmpycolors[-1] != yar[i]):
179 #print tmpxcolors[-1],tmpycolors[-1],"is not", xar[i],yar[i],"!"
180 ret_xcolors[j].append(xar[i])
181 ret_ycolors[j].append(yar[i])
182 else:
183 ret_xcolors[j].append(xar[i])
184 ret_ycolors[j].append(yar[i])
185 lenc = float(len(colors))
186 per = 100*k/lenc
187 print unique_colors[j],":",len(ret_xcolors[j]),k,"(",'%.2f' % per ,"%)"
188 print "---------------------------------------------------------"
189 return (ret_xcolors, ret_ycolors)
191 print "reading data..."
192 r = mlab.csv2rec(FIN, delimiter='\t', comments='#')
194 x = r.x
195 x_color = r.vx_color
196 y_color = r.vy_color
197 y = r.y
198 vx = r.vx
199 vy = r.vy
201 assert len(x) == len(y) == len(vx) == len(vy)
202 print "done."
204 xmin = np.min(unique(x))
205 xmax = np.max(unique(x))
206 ymin = np.min(unique(y))
207 ymax = np.max(unique(y))
209 # assumes that the step distance doesn't change over the course of time
210 rat = xstep/ystep
211 print "size of xstep:", xstep
212 print "size of ystep:", ystep
213 print "aspect ratio:",rat
215 print "different values of vx:"
216 print unique(vx),"(",len(vx),"total)"
218 print "different values of vy:"
219 print unique(vy),"(",len(vy),"total)"
221 # needed for assign_color2
222 color_list = [
223 'black', # 0 everything else
224 'gray', # 1 v = 0
225 'blue', # 2 v = -1.0
226 'green', # 3 v = -0.5
227 'orange', # 4 v = 0.5
228 'red', # 5 v = 1.0
229 'khaki', # 6 v = -0.5 && v = 0.5
230 'purple', # 7 v = -1.0 && v = 1.0
231 'white', # 8 not understood
232 'darkgoldenrod',# 9 v = +/- 0.5 & 0
233 'mediumpurple', # 10 v = +/- 1 1 & -0
234 'brown', # 11 v = +/1 1 & +/- 0.5
237 label_list = [
238 r'other rational $v$', # 0 black
239 r'$v = 0$', # 1 grey
240 r'$v = -1$', # 2 blue
241 r'$v = -\frac{1}{2}$', # 3 green
242 r'$v = \frac{1}{2}$', # 4 orange
243 r'$v = 1$', # 5 red
244 r'$v = \pm \frac{1}{2}$', # 6 brown
245 r'$v = \pm 1$', # 7 purple
246 r'???', # 8 white
247 r'$v = \pm \frac{1}{2},$ other',# 9 darkgoldenrod
248 r'$v = \pm 1,$ other ', # 10 mediumpurple
249 r'$v = \pm 1,\pm \frac{1}{2}$', # 11 brown
252 xpadmin = xmin-xstep
253 xpadmax = xmax+xstep
254 ypadmin = ymin-ystep
255 ypadmax = ymax+ystep
256 xlim = (xpadmin, xpadmax) # small padding between plot and
257 ylim = (ypadmin, ypadmax) # axis so they don't overlap
258 xticks = np.arange(xmin, xmax+xpad, xtfreq)
259 yticks = np.arange(ymin, ymax+ypad, ytfreq)
262 fig = plt.figure() #(figsize=res)
263 #fig.suptitle(title, size=labelsize)
265 max = 0.84
266 c = max/2
267 rect1 = [0.1, 0.1, c, 0.9]
268 rect2 = [c+0.1, 0.1, c, 0.9]
270 if has_y is True:
271 ax1 = fig.add_subplot(121, aspect=rat)
272 ax2 = fig.add_subplot(122, aspect=rat)
273 else:
274 ax1 = fig.add_subplot(111, aspect=rat)
276 if (method == 'mean'):
278 print "assigning colors...."
279 xcolors_x,xcolors_y = assign_color(x_color,x,y)
280 ycolors_x,ycolors_y = assign_color(y_color,x,y)
281 print "done."
283 print "plotting..."
284 for k in range(len(unique(x_color))):
285 x_xtli = xcolors_x[k]
286 x_ytli = xcolors_y[k]
287 labelx = assign_label(unique(x_color)[k])
288 ax1.scatter(x_xtli, x_ytli, marker='s', lw=0, s=ms, alpha=0.5,
289 c = unique(x_color)[k], label=labelx,
290 edgecolor='face')
291 if (has_y is True):
292 for k in range(len(unique(y_color))):
293 y_xtli = ycolors_x[k]
294 y_ytli = ycolors_y[k]
295 labely = assign_label(unique(y_color)[k])
296 ax2.scatter(y_xtli, y_ytli, marker='s', lw=0,
297 s=ms, alpha=0.5, c =
298 unique(y_color)[k],
299 label=labely, edgecolor='none')
301 print "done."
303 elif (method == 'direct'):
305 print "assigning colors..."
306 xcolors_x,xcolors_y = assign_color2(x_color,x,y,vx,color_list)
307 ycolors_x,ycolors_y = assign_color2(y_color,x,y,vy,color_list)
308 print "done."
310 #for i in range(8):
311 # print i, xcolors_x[i],xcolors_y[i];
313 print "plotting..."
314 for k in range(len(xcolors_x)):
315 if len(xcolors_x[k]) > 0:
316 x_xtli = xcolors_x[k]
317 x_ytli = xcolors_y[k]
318 ax1.scatter(x_xtli, x_ytli, marker='s', lw=0,
320 s=ms, alpha=1, c=color_list[k],
321 label=label_list[k])
323 if (has_y is True):
324 for k in range(len(ycolors_x)):
325 if len(ycolors_x[k]) > 0:
326 y_xtli = ycolors_x[k]
327 y_ytli = ycolors_y[k]
328 ax2.scatter(y_xtli, y_ytli, marker='s',
329 lw=0, s=ms, alpha=1,
330 c=color_list[k],
331 label=label_list[k])
333 print "done."
334 else:
335 print "fatal error: specify a valid method"
337 leg1 = ax1.legend(loc=0, shadow=True,
338 fancybox=True, scatterpoints=1, markerscale=1,
339 borderaxespad=0.)
340 for t in leg1.get_texts():
341 t.set_fontsize('small')
343 if has_y is True:
344 leg2 = ax2.legend(loc=2, shadow=True,
345 fancybox=True, scatterpoints=1, markerscale=1,
346 borderaxespad=0.)
347 for t in leg2.get_texts():
348 t.set_fontsize('small')
351 ax1.set_title(r'$v_x$', size=labelsize)
352 ax1.set_xlim(xlim)
353 ax1.set_ylim(ylim)
354 ax1.set_xlabel(xlabel, size=labelsize)
355 ax1.set_ylabel(ylabel, size=labelsize)
356 ax1.set_xticks(xticks, minor=False)
357 ax1.set_yticks(yticks, minor=False)
358 ax1.set_xticklabels(ax1.get_xticks(), size=ticksize)
359 ax1.set_yticklabels(ax1.get_yticks(), size=ticksize)
361 if has_y is True:
362 ax2.set_title(r'$v_y$', size=labelsize)
363 ax2.set_xlim(xlim)
364 ax2.set_ylim(ylim)
365 ax2.set_xlabel(xlabel, size=labelsize)
366 ax2.set_ylabel('', size=labelsize)
367 ax2.set_xticks(xticks, minor=False)
368 ax2.set_yticks(yticks, minor=False)
369 ax2.set_xticklabels(ax2.get_xticks(), size=ticksize)
370 ax2.set_yticklabels('', size=ticksize)
372 if (has_col is True):
373 s1 = np.arange(54.5,66,0.5)
374 collection = col.BrokenBarHCollection.span_where(s1, ymin=0.95, ymax=4.05,
375 where=s1>54, facecolor='None', edgecolor='red')
376 ax1.add_collection(collection)
379 leg1.get_frame().set_alpha(0.75)
381 if has_y is True:
382 leg2.get_frame().set_alpha(0.75)
385 plt.subplots_adjust(hspace=0)
386 print "flushing into a file..."
387 plt.savefig(FOUT)
388 print "done."
389 #plt.show()