4 from __future__
import print_function
8 import matplotlib.pyplot
as pl
13 def _kw_extract(kw,dict):
14 """ if keys from dict occur in kw, pop them """ 15 for key,value
in dict.items():
16 dict[key]=kw.pop(key,value)
19 def imshow(f,colorbar=None,origin='lower',interpolation='nearest',
20 cmap='coolwarm',verbose=0,update=False,**kw):
22 imshow with default options and updateable colorbar. Example use: 24 import matplotlib.pyplot as pl 25 import dispatch.select as ds 26 import dispatch.graphics as dg 28 s=dispatch.snapshot(1) 29 dg.imshow(ds.unigrid_plane(s,iv=0)) 31 dg.imshow(ds.unigrid_plane(s,iv=1),cb) 33 The second call updates the colorbar from the 1st call 36 print (
'min:',f.min(),
' max:',f.max())
37 pl.imshow(np.transpose(f),origin=origin,interpolation=interpolation,cmap=cmap,**kw)
38 """ Check if there is an existing colorbar """ 43 if type(im.colorbar)==matplotlib.colorbar.Colorbar:
45 """ If a colorbar exists, update the limits - if not, make one """ 47 cb.set_clim(f.min(),f.max())
53 """ plot(f) allows f to be (x,y) tuple """ 60 def plot_values_along(pp,pt=[0.5,0.5,0.5],**kw):
61 """ Plot values along direction dir={0,1,2}, through point pt=[x,y,z] """ 62 kv = {
'dir':0,
'verbose':0,
'all':
False,
'iv':0,
'i4':0,
'var':
None}
63 kw,kv = _kw_extract(kw,kv)
64 plot(ds.values_along(pp,pt,iv=kv[
'iv'],dir=kv[
'dir'],all=kv[
'all']),**kw)
66 def plot_patch_values_along(pp_in,pt=[0.5,0.5,0.5],hold=False,**kw):
67 """ Plot values along direction dir={0,1,2}, through point pt=[x,y,z] """ 68 kv = {
'dir':0,
'verbose':0,
'all':
False,
'iv':0,
'i4':0,
'var':
None}
69 kw,kv = _kw_extract(kw,kv)
70 pp = ds.patches_along(pp_in,pt,dir=kv[
'dir'],verbose=kv[
'verbose'])
71 xmin,xmax = ds.minmax_patches(pp,dir=kv[
'dir'])
75 plot(ds.values_in(p,pt,**kv),**kw)
78 '''Rotate data and extent to landscape format''' 80 e=[e[2],e[3],e[0],e[1]]
84 """plot power spectrum of 2D array""" 85 ft2=np.abs(np.fft.fft2(f))**2
87 k=np.meshgrid(range(m[0]),range(m[1]))
88 k=np.sqrt(k[0]**2+k[1]**2)
95 kk.append((k0*k1)**0.5)
96 w=np.where((k>k0) & (k <= k1))
97 power.append(ft2[w].sum())
100 pl.loglog(kk,power,*kw)
102 def show_plot(figure_id=None):
103 """ raise a figure to the top """ 104 if figure_id
is None:
108 fig = pl.figure(num=figure_id)
111 fig.canvas.manager.window.activateWindow()
112 fig.canvas.manager.window.raise_()
114 def pause(time=1e-6):
115 """ replace the normal pause, w/o raising window() """ 117 pl.gcf().canvas.start_event_loop(time)
119 def image_plane(snap,x=None,y=None,z=None,iv=0,verbose=1):
125 labels=[(
'y',
'z'),(
'z',
'x'),(
'y',
'z')]
126 pp=snap.patches_in(x=x,y=y,z=z)
130 f=p.plane(x=x,y=y,z=z,iv=iv)
134 emin=np.array((e[0],e[2]))
135 emax=np.array((e[1],e[3]))
137 im=p.plane(x=x,y=y,z=z,iv=iv)
138 fmin=min(fmin,im.min())
139 fmax=max(fmax,im.max())
141 emin=np.minimum(emin,np.array((e[0],e[2])))
142 emax=np.maximum(emax,np.array((e[1],e[3])))
143 ll[p.id]=(im,e,p.level)
144 for id
in sorted(ll.keys()):
147 imshow(im,extent=e,vmin=fmin,vmax=fmax,hold=
True)
149 pl.xlim(emin[0],emax[0])
150 pl.ylim(emin[1],emax[1])
151 pl.title(
'{}={:.3f}'.format(sdir[i],xyz[i]))
152 pl.xlabel(labels[i][0])
153 pl.ylabel(labels[i][1])
155 def pdf_d(iout,run='',data='../data',iv='d',i4=0,nbin=100,xlim=[-4,3],lnd=False):
156 """ Plot the PDF of the density """ 157 s = di.snapshot(iout,run=run,data=data)
159 bins = np.linspace(xlim[0],xlim[1],n+1)
165 print(
'{:.1f}%'.format(i/len(s.patches)*100.0))
171 h,e = np.histogram(logd,bins=bins)
173 pl.hist(bins[0:n],bins=bins,weights=htot,log=
True,density=
True)
176 def unigrid_plane(sn,**kw):
178 imshow(dse.unigrid_plane(sn,**kw))
181 def amr_plane(sn,iv='d',z=0.5,log=False,mesh=False,ident=False,cmap=None,
182 title=None,verbose=0):
184 return p.level >= 6
and abs(z-p.position[2]) <= p.size[2]/2
186 x=[e[0],e[0],e[1],e[1],e[0]]
187 y=[e[2],e[3],e[3],e[2],e[2]]
188 pl.plot(x,y,color=
'grey')
189 def interpolate(p,iv,z,log=False):
192 z0=p.position[2]-p.size[2]/2+dz/2
198 if iv==0
or iv==4
or iv==
'd' or iv==
'e':
202 f=(1-w)*f[:,:,i]+w*f[:,:,i+1]
206 pp=[p
for p
in sn.patches
if contains(p,z)]
210 d=interpolate(p,iv,z)
211 vmin=min(vmin,d.min())
212 vmax=max(vmax,d.max())
214 print(p.id,d.min(),d.max())
215 pl.xlim(0,1); pl.ylim(0,1)
221 d=interpolate(p,iv,z)
223 d=np.log10(interpolate(p,iv,z))
224 pl.imshow(d.transpose(),extent=e,
225 vmin=vmin,vmax=vmax,origin=
'lower',cmap=cmap)
226 x0,x1,y0,y1=p.extent[2]
230 x0,x1,y0,y1=p.extent[2]
231 pl.text(x0,y0,
'{}'.format(p.id))
236 pl.title(
'iv={}, z={:.2f}, t={:.4f}'.format(iv,z,sn.time))
239 def plot_pdf(pdf,**kwargs):
240 """ Plot the output of the dispatch.pdf() procedure """ 241 pl.hist(pdf.bins,bins=pdf.bins,weights=pdf.counts,**kwargs)
244 def pdf(io,run,data,log=True,**kwargs):
245 """ Shortcut of dispatch.graphics.plot_pdf(dispatch.pdf(io,run,data)) """ 246 tmp=di.pdf(io,run,data,**kwargs)
247 plot_pdf(tmp,**kwargs)