Esempio n. 1
0
File: plot.py Progetto: biasmv/tap
  def __init__(self, tab, x, y, z, title=None, x_title=None, y_title=None, 
               z_title=None, x_range=None, y_range=None, z_range=None, 
               color=None):

    self._tab = tab

    self.x, self.y, self.z = (x, y, z)

    self._title = title

    self._x_title = x_title
    self._y_title = y_title
    self._z_title = z_title
    self._color = color


    for  r, n in ((x_range, 'x'), (y_range, 'y'), (z_range, 'z')):
      if r and (typeutil.is_scalar(x_range) or len(x_range)!=2):
        raise ValueError('%s_range must contain exactly two items' % n)

    self._x_range, self._y_range, self._z_range = (x_range, y_range, z_range)
Esempio n. 2
0
File: plot.py Progetto: biasmv/tap
def plot_hexbin(self, x, y, title=None, x_title=None, y_title=None, x_range=None, y_range=None, binning='log',
                colormap='jet', show_scalebar=False, scalebar_label=None, clear=True, save=False, show=False):

  """
  Create a heatplot of the data in col x vs the data in col y using matplotlib

  :param x: column name with x data
  :type x: :class:`str`

  :param y: column name with y data
  :type y: :class:`str`

  :param title: title of the plot, will be generated automatically if set to None
  :type title: :class:`str`

  :param x_title: label of x-axis, will be generated automatically if set to None
  :type title: :class:`str`

  :param y_title: label of y-axis, will be generated automatically if set to None
  :type title: :class:`str`

  :param x_range: start and end value for first dimension (e.g. [start_x, end_x])
  :type x_range: :class:`list` of length two

  :param y_range: start and end value for second dimension (e.g. [start_y, end_y])
  :type y_range: :class:`list` of length two

  :param binning: type of binning. If set to None, the value of a hexbin will
                  correspond to the number of datapoints falling into it. If
                  set to 'log', the value will be the log with base 10 of the above
                  value (log(i+1)). If an integer is provided, the number of a 
                  hexbin is equal the number of datapoints falling into it divided 
                  by the integer. If a list of values is provided, these values
                  will be the lower bounds of the bins.
  
  :param colormap: colormap, that will be used. Value can be every colormap defined
                    in matplotlib or an own defined colormap. You can either pass a
                    string with the name of the matplotlib colormap or a colormap
                    object.

  :param show_scalebar: If set to True, a scalebar according to the chosen colormap is shown
  :type show_scalebar: :class:`bool`

  :param scalebar_label: Label of the scalebar
  :type scalebar_label: :class:`str`

  :param clear: clear old data from plot
  :type clear: :class:`bool`

  :param save: filename for saving plot
  :type save: :class:`str`

  :param show: directly show plot
  :type show: :class:`bool`
  
  """

  try:
    import matplotlib.pyplot as plt
    import matplotlib.cm as cm
  except:
    raise ImportError('plot_hexbin relies on matplotlib, but I could not import it')

  idx=self.col_index(x)
  idy=self.col_index(y)
  xdata=[]
  ydata=[]

  for r in self.rows:
    if r[idx]!=None and r[idy]!=None:
      xdata.append(r[idx])
      ydata.append(r[idy])

  if clear:
    plt.clf()
    
  if x_title!=None:
    nice_x=x_title
  else:
    nice_x=make_title(x)
    
  if y_title!=None:
    nice_y=y_title
  else:
    nice_y=make_title(y)

  if title==None:
    title = '%s vs. %s' % (nice_x, nice_y)

  if typeutil.is_string_like(colormap):
    colormap=getattr(cm, colormap)

  if x_range and (typeutil.is_scalar(x_range) or len(x_range)!=2):
    raise ValueError('parameter x_range must contain exactly two elements')
  if y_range and (typeutil.is_scalar(y_range) or len(y_range)!=2):
    raise ValueError('parameter y_range must contain exactly two elements')
  if x_range:
    plt.xlim((x_range[0], x_range[1]))
  if y_range:
    plt.ylim(y_range[0], y_range[1])
  extent = None
  if x_range and y_range:
    extent = [x_range[0], x_range[1], y_range[0], y_range[1]]
  plt.hexbin(xdata, ydata, bins=binning, cmap=colormap, extent=extent)

  plt.title(title, size='x-large', fontweight='bold',
            verticalalignment='bottom')

  plt.xlabel(nice_x)
  plt.ylabel(nice_y)
      
  if show_scalebar:
    cb=plt.colorbar()
    if scalebar_label:
      cb.set_label(scalebar_label)

  if save:
    plt.savefig(save)

  if show:
    plt.show()

  return plt