Esempio n. 1
0
def showcol(vs, size=(4.1,2), **kwargs):
# {{{
  ''' 
  Plot variable, showing a contour plot for 2d variables or a line plot for 1d variables.

  Parameters
  ----------
  v :  list of lists of :class:`Var`
     The variables to plot. Should have either 1 or 2 non-degenerate axes.

  Notes
  -----
  This function is intended as the simplest way to display the contents of a variable,
  choosing appropriate parameter values as automatically as possible.
  '''

  Z = [v.squeeze() for v in vs]

  assert Z[0].naxes in [1, 2], 'Variables %s has %d non-generate axes; must have 1 or 2.' % (Z.name, Z.naxes)

  for z in Z[1:]:
    assert Z[0].naxes == z.naxes, 'All variables must have the same number of non-generate dimensions'
    #assert all([a == b for a, b in zip(Z[0].axes, z.axes)])

  fig = kwargs.pop('fig', None)

  if Z[0].naxes == 1:
    axs = []
    ydat = []
    for v in vs:
      lblx = (v is vs[-1])
      ax = vplot(v, lblx = lblx, **kwargs)
      ax.size = size
      axs.append([ax])
      ydat.append(ax.find_plot(wr.Plot).plot_args[1])

    Ax = wr.grid(axs)
    ylim = (np.min([np.min(y) for y in ydat]), np.max([np.max(y) for y in ydat]))
    Ax.setp(ylim = ylim, children=True)

  elif Z[0].naxes == 2:
    axs = []
    for v in vs:
      lblx = (v is vs[-1])
      ax = vcontour(v, lblx = lblx, **kwargs)
      ax.size = size
      axs.append([ax])

    Ax = wr.grid(axs)

    cbar = kwargs.pop('colorbar', dict(orientation='vertical'))
    cf = Ax.axes[0].find_plot(wr.Contourf)
    if cbar and cf is not None:
      Ax = wr.colorbar(Ax, cf, **cbar)

  import pylab as pyl
  if pyl.isinteractive(): Ax.render(fig)
  return Ax
Esempio n. 2
0
def showvar(var, *args, **kwargs):
# {{{
  ''' 
  Plot variable, showing a contour plot for 2d variables or a line plot for 1d variables.

  Parameters
  ----------
  var :  :class:`Var`
     The variable to plot. Should have either 1 or 2 non-degenerate axes.

  *args, **kwargs : arguments to pass on to underlying plotting routines, see
      Notes.

  Notes
  -----
  This function is intended as the simplest way to display the contents of a
  variable, choosing appropriate parameter values as automatically as possible.
  For 1d variables it calls :func:`Var.vplot()`, and for 2d variables
  :func:`Var.vcontour`. In the latter case, if filled contours were produced, it
  calls `AxesWrapper.colorbar()'. A dictionary ``colorbar`` can be provided to
  pass arguments through. Setting ``colorbar`` to ``False`` suppresses the
  colorbar.

  See Also
  --------
  vplot, vcontour, colorbar
  '''

  Z = var.squeeze()
  assert Z.naxes in [1, 2], 'Variable %s has %d non-generate axes; must have 1 or 2.' % (var.name, Z.naxes)

  fig = kwargs.pop('fig', None)

  if Z.naxes == 1:
    ax = vplot(var, *args, **kwargs)

  elif Z.naxes == 2:
    ax = vcontour(var, *args, **kwargs)

    cbar = kwargs.pop('colorbar', dict(orientation='vertical'))
    cf = ax.find_plot(wr.Contourf)
    if cbar and cf is not None:
      ax = wr.colorbar(ax, cf, **cbar)

  import pylab as pyl
  if pyl.isinteractive():
    ax.render(fig)
  return ax
Esempio n. 3
0
def showgrid(vf, vl=[], ncol=1, size=(3.5,1.5), lbl=True, **kwargs):
# {{{
  ''' 
  Plot contours

  Parameters
  ----------
  v :  list of lists of :class:`Var`
     The variables to plot. Should have either 1 or 2 non-degenerate axes.

  Notes
  -----
  This function is intended as the simplest way to display the contents of a variable,
  choosing appropriate parameter values as automatically as possible.
  '''

  from pygeode import Var
  if isinstance(vf, Var): vf = [vf]
  if isinstance(vl, Var): vl = [vl]

  assert all([v.squeeze().naxes == 2 for v in vf]), 'Variables (vf) should have 2 degenerate axes.'
  nVf = len(vf)

  assert all([v.squeeze().naxes == 2 for v in vl]), 'Variables (vl) should have 2 degenerate axes.'
  nVl = len(vl)
  if nVf > 1 and nVl > 1: assert nVl == nVf, 'Must have the same number of filled and contour-line variables.'
    
  fig = kwargs.pop('fig', None)
  cbar = kwargs.pop('colorbar', dict(orientation='vertical'))
  xpad = 0.
  ypad = 0.

  kwlines = {}
  if nVl > 0:
    kwlines['colors'] = kwargs.pop('colors', 'k')
    kwlines['clines'] = kwargs.pop('clines', 11)
    for k in ['linewidths', 'linestyles']:
      if kwargs.has_key(k): kwlines[k] = kwargs.pop(k)

  kwfill = {}
  if nVf > 0:
    kwfill['clevs'] = kwargs.pop('clevs', 31)
    kwfill['cmap'] = kwargs.pop('cmap', None)
    kwlines['label'] = False
    if cbar: 
      if cbar.get('orientation', 'vertical') == 'vertical':
        ypad = cbar.get('width', 0.8)
      else:
        xpad = cbar.get('height', 0.4)

  
  kwcb = {}

  nV = max(nVl, nVf)
  nrow = np.ceil(nV / float(ncol))

  axpad = 0.2
  aypad = 0.4
  if lbl:
    axpadl = 0.9
    aypadl = 0.55
  else:
    axpadl = axpad
    aypadl = aypad

  axw, axh = size
  ypad = ypad + aypadl + aypad * (nrow-1)
  xpad = xpad + axpadl + axpad * (ncol-1)

  axs = []
  row = []
  for i in range(nV):
    lblx = (i / ncol == nrow - 1)
    lbly = (i % ncol == 0)
    ax = None
    if nVf > 0: 
      v = vf[i % nVf]
      kwfill.update(kwargs)
      ax = vcontour(v, axes=ax, lblx = lblx, lbly = lbly, **kwfill)
    if nVl > 0:
      v = vl[i % nVl]
      kwlines.update(kwargs)
      ax = vcontour(v, axes=ax, lblx = lblx, lbly = lbly, **kwlines)

    if lblx: h = axh + aypadl
    else: h = axh + aypad
    if lbly: w = axw + axpadl
    else: w = axw + axpad

    ax.size = (w, h)
    row.append(ax)
    if i % ncol == ncol - 1:
      axs.append(row)
      row = []

  if len(row) > 0: 
    row.extend([None] * (ncol - len(row)))
    axs.append(row)

  Ax = wr.grid(axs)

  cf = Ax.axes[0].find_plot(wr.Contourf)
  if cbar and cf is not None:
    Ax = wr.colorbar(Ax, cf, **cbar)

  import pylab as pyl
  if pyl.isinteractive(): Ax.render(fig)
  return Ax