11"""Plot scalar and vector fields."""
22
3+ from itertools import chain
4+
35import numpy as np
46import matplotlib as mpl
57import matplotlib .pyplot as plt
68import matplotlib .patches as mpat
9+ from mpl_toolkits .axes_grid1 import make_axes_locatable
710
811from . import conf , misc , phyvars
912from .error import NotAvailableError
@@ -119,28 +122,6 @@ def get_meshes_vec(step, var):
119122 return xmesh , ymesh , vec1 , vec2
120123
121124
122- def set_of_vars (arg_plot ):
123- """Build set of needed field variables.
124-
125- Each var is a tuple, first component is a scalar field, second component is
126- either:
127-
128- - a scalar field, isocontours are added to the plot.
129- - a vector field (e.g. 'v' for the (v1,v2,v3) vector), arrows are added to
130- the plot.
131-
132- Args:
133- arg_plot (str): string with variable names separated with
134- ``,`` (figures), and ``+`` (same plot).
135- Returns:
136- set of str: set of needed field variables.
137- """
138- sovs = set (tuple ((var + '+' ).split ('+' )[:2 ])
139- for var in arg_plot .split (',' ))
140- sovs .discard (('' , '' ))
141- return sovs
142-
143-
144125def plot_scalar (step , var , field = None , axis = None , ** extra ):
145126 """Plot scalar field.
146127
@@ -217,13 +198,15 @@ def plot_scalar(step, var, field=None, axis=None, **extra):
217198
218199 cbar = None
219200 if conf .field .colorbar :
220- cbar = plt .colorbar (surf , shrink = conf .field .shrinkcb )
201+ cax = make_axes_locatable (axis ).append_axes (
202+ 'right' , size = "3%" , pad = 0.15 )
203+ cbar = plt .colorbar (surf , cax = cax )
221204 cbar .set_label (meta .description +
222205 (' pert.' if conf .field .perturbation else '' ) +
223206 (' ({})' .format (unit ) if unit else '' ))
224207 if step .geom .spherical or conf .plot .ratio is None :
225- plt . axis ('equal' )
226- plt . axis ( 'off' )
208+ axis . set_aspect ('equal' )
209+ axis . set_axis_off ( )
227210 else :
228211 axis .set_aspect (conf .plot .ratio / axis .get_data_ratio ())
229212 axis .set_adjustable ('box' )
@@ -283,13 +266,16 @@ def cmd():
283266 conf.core
284267 """
285268 sdat = StagyyData ()
286- sovs = set_of_vars (conf .field .plot )
269+ lovs = misc .list_of_vars (conf .field .plot )
270+ # no more than two fields in a subplot
271+ lovs = [[slov [:2 ] for slov in plov ] for plov in lovs ]
287272 minmax = {}
288273 if conf .plot .cminmax :
289274 conf .plot .vmin = None
290275 conf .plot .vmax = None
276+ sovs = set (slov [0 ] for plov in lovs for slov in plov )
291277 for step in sdat .walk .filter (snap = True ):
292- for var , _ in sovs :
278+ for var in sovs :
293279 if var in step .fields :
294280 if var in phyvars .FIELD :
295281 dim = phyvars .FIELD [var ].dim
@@ -302,18 +288,23 @@ def cmd():
302288 else :
303289 minmax [var ] = np .nanmin (field ), np .nanmax (field )
304290 for step in sdat .walk .filter (snap = True ):
305- for var in sovs :
306- if var [0 ] not in step .fields :
307- print ("'{}' field on snap {} not found" .format (var [0 ],
308- step .isnap ))
309- continue
310- opts = {}
311- if var [0 ] in minmax :
312- opts = dict (vmin = minmax [var [0 ]][0 ], vmax = minmax [var [0 ]][1 ])
313- fig , axis , _ , _ = plot_scalar (step , var [0 ], ** opts )
314- if valid_field_var (var [1 ]):
315- plot_iso (axis , step , var [1 ])
316- elif var [1 ]:
317- plot_vec (axis , step , var [1 ])
318- oname = '{}_{}' .format (* var ) if var [1 ] else var [0 ]
291+ for vfig in lovs :
292+ fig , axes = plt .subplots (ncols = len (vfig ), squeeze = False ,
293+ figsize = (12 * len (vfig ), 9 ))
294+ for axis , var in zip (axes [0 ], vfig ):
295+ if var [0 ] not in step .fields :
296+ print ("'{}' field on snap {} not found" .format (var [0 ],
297+ step .isnap ))
298+ continue
299+ opts = {}
300+ if var [0 ] in minmax :
301+ opts = dict (vmin = minmax [var [0 ]][0 ], vmax = minmax [var [0 ]][1 ])
302+ plot_scalar (step , var [0 ], axis = axis , ** opts )
303+ if len (var ) == 2 :
304+ if valid_field_var (var [1 ]):
305+ plot_iso (axis , step , var [1 ])
306+ elif valid_field_var (var [1 ] + '1' ):
307+ plot_vec (axis , step , var [1 ])
308+ oname = '_' .join (chain .from_iterable (vfig ))
309+ plt .tight_layout (w_pad = 3 )
319310 misc .saveplot (fig , oname , step .isnap )
0 commit comments