Beispiel #1
0
def make_lines(aggregate,
               yerror_aggregate=None,
               series_order=None,
               x_labels=None,
               x_ticks=None,
               series_labels=False,
               point_yoffs=None,
               legend_labels=None,
               styles=None,
               title="Title",
               xlabel="X",
               ylabel="Y",
               x_range=None,
               y_range=None,
               y_res=None,
               legend_pos=None,
               x_log=False,
               y_log=False):

    if series_order == None:
        series_order = common.graph_default_order(aggregate)

    y_data_series, yerror_series = common.graph_series(aggregate,
                                                       yerror_aggregate,
                                                       series_order)

    x_data_series = None
    if x_ticks != None:
        x_data_series = x_ticks
    else:
        x_data_series = range(0, len(y_data_series))

    data_series = []
    yerror = []

    for i in xrange(0, len(y_data_series[0])):
        data_series.append([])
        yerror.append([])

    for i in xrange(0, len(y_data_series)):
        xs = [i] * len(y_data_series[i])
        pts = zip(xs, y_data_series[i])

        k = 0
        for j in xrange(0, len(data_series)):
            data_series[j].append(pts[k])
            yerror[j].append(yerror_series[j][k])
            k += 1

    fig = plt.figure()
    ax = fig.add_subplot(111)
    lines = []

    if styles == None:
        styles = default_styles(len(data_series))

    for i in xrange(0, len(data_series)):
        x_series = [x for (x, y) in data_series[i]]
        y_series = [y for (x, y) in data_series[i]]

        style = 'k'
        if styles != None:
            if styles[i] != None:
                style = styles[i]

        ll, = ax.plot(x_series, y_series, style, markersize=10)
        lines.append(ll)

        if yerror != None:
            if yerror[i] != None:
                ax.errorbar(x_series, y_series, yerr=yerror[i])

    # apply labels
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_title(title)

    if legend_labels == None:
        legend_labels = common.graph_legend_labels(series_order)

    if legend_labels != None:
        kwargs = {}
        if legend_pos != None:
            kwargs['loc'] = legend_pos
        ax.legend(lines, legend_labels, **kwargs)

    if x_ticks == None:
        x_ticks = x_data_series

    if x_labels == None:
        x_labels = [str(x) for x in x_ticks]

    if x_labels != None and x_ticks != None:
        ax.set_xticks(x_ticks)
        ax.set_xticklabels(x_labels)
        ax.autoscale()

    if y_res != None and y_range != None:
        ax.set_yticks(np.arange(y_range[0], y_range[1], y_res))

    if x_range != None:
        ax.set_autoscalex_on(False)
        ax.set_xlim([x_range[0], x_range[1]])

    if y_range != None:
        ax.set_autoscaley_on(False)
        ax.set_ylim([y_range[0], y_range[1]])

    # label points
    if series_labels:
        j = 0
        for ll in lines:
            x_series = ll.get_xdata()
            y_series = ll.get_ydata()
            i = 0
            for (x, y) in zip(x_series, y_series):
                yoff = y
                if point_yoffs:
                    yoff = y + point_yoffs[j][i]

                i += 1
                ax.text(x, yoff, '%3.2f' % float(y), ha='left', va='bottom')
            j += 1

    if x_log:
        ax.set_xscale('log')

    if y_log:
        ax.set_yscale('log', nonposy="clip")
Beispiel #2
0
def make_bars( aggregate, yerror_aggregate=None, series_order=None, x_labels=None, legend_labels=None, legend_cols=None, legend_pos=None, series_labels=False, hatches=None, colors=None, title="Title", xlabel="X", ylabel="Y", x_range=None, y_range=None, y_res=None, x_log=False, y_log=False ):
   # aggregate = {step: {key: [method_result_1, method_result_2, ...], ...}, ...}
   # yerror_aggregate = {step: {key: [yerror_1, yerror_2, ...], ...}, ...}
   # series_order = [(step1, key1), (step2, key2), ...]

   # extract data series into [[bar11, bar12, bar13, ...], [bar21, bar22, bar23, ...], ...]
   # extract yerror series into [[yerror11, yerror12, yerror13, ...], [yerror21, yerror22, yerror23, ...], ...]

   if series_order == None:
      series_order = common.graph_default_order( aggregate )

   if legend_labels == None:
      legend_labels = common.graph_legend_labels( series_order )
      
   data_series, yerror_series = common.graph_series( aggregate, yerror_aggregate, series_order )

   width = 0.8 / len(data_series)
   N = len(data_series[0])
   ind = np.arange(N)
   fig = plt.figure()
   ax = fig.add_subplot( 111 )
   rects = []

   y_max = 0
   for op in data_series:
      y_max = max( y_max, max(op) )

   if hatches == None and colors == None:
      hatches = default_hatches( len(data_series) )

   if colors == None:
      colors = default_colors( len(data_series) )
      
   for i in xrange(0,len(data_series)):
      rect = None
      kwargs = {}
      if yerror_series != None:
         if yerror_series[i] != None:
            kwargs['yerr'] = yerror_series[i]

      if hatches != None:
         if hatches[i] != None:
            kwargs['hatch'] = hatches[i]

      if colors != None:
         if colors[i] != None:
            kwargs['color'] = colors[i]

      rect = ax.bar( ind + i * width, data_series[i], width, log=y_log, **kwargs )
      rects.append( rect )

   # apply labels
   ax.set_xlabel( xlabel )
   ax.set_ylabel( ylabel )
   ax.set_title( title )

   if legend_labels != None:
      kwargs={}
      if legend_pos != None:
         kwargs['loc'] = legend_pos
      if legend_cols != None:
         kwargs['ncol'] = legend_cols

      ax.legend( rects, legend_labels, **kwargs )


   if x_labels != None:
      ax.set_xticks( ind + width * len(data_series) / 2.0 )
      ax.set_xticklabels( x_labels )
      ax.autoscale()

   if y_res != None and y_range != None:
      ax.set_yticks( np.arange(y_range[0], y_range[1], y_res) )

   # label bars
   if series_labels:
      for rect_series in rects:
         for rect in rect_series:
            height = rect.get_height()
            y_off = 0
            if y_log:
               y_off = height * 1.05
            else:
               y_off = height + y_max * 0.005

            ax.text( rect.get_x() + rect.get_width() / 2.0, y_off, '%3.2f' % float(height), ha = 'center', va = 'bottom' )

   if x_range != None:
      ax.set_autoscalex_on(False)
      ax.set_xlim( [x_range[0], x_range[1]] )

   if y_range != None:
      ax.set_autoscaley_on(False)
      ax.set_ylim( [y_range[0], y_range[1]] )

   if x_log:
      ax.set_xscale('log')

   if y_log:
      ax.set_yscale('log', nonposy="clip")
Beispiel #3
0
def make_bars(aggregate,
              yerror_aggregate=None,
              series_order=None,
              x_labels=None,
              legend_labels=None,
              legend_cols=None,
              legend_pos=None,
              series_labels=False,
              hatches=None,
              colors=None,
              title="Title",
              xlabel="X",
              ylabel="Y",
              x_range=None,
              y_range=None,
              y_res=None,
              x_log=False,
              y_log=False):
    # aggregate = {step: {key: [method_result_1, method_result_2, ...], ...}, ...}
    # yerror_aggregate = {step: {key: [yerror_1, yerror_2, ...], ...}, ...}
    # series_order = [(step1, key1), (step2, key2), ...]

    # extract data series into [[bar11, bar12, bar13, ...], [bar21, bar22, bar23, ...], ...]
    # extract yerror series into [[yerror11, yerror12, yerror13, ...], [yerror21, yerror22, yerror23, ...], ...]

    if series_order == None:
        series_order = common.graph_default_order(aggregate)

    if legend_labels == None:
        legend_labels = common.graph_legend_labels(series_order)

    data_series, yerror_series = common.graph_series(aggregate,
                                                     yerror_aggregate,
                                                     series_order)

    width = 0.8 / len(data_series)
    N = len(data_series[0])
    ind = np.arange(N)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    rects = []

    y_max = 0
    for op in data_series:
        y_max = max(y_max, max(op))

    if hatches == None and colors == None:
        hatches = default_hatches(len(data_series))

    if colors == None:
        colors = default_colors(len(data_series))

    for i in xrange(0, len(data_series)):
        rect = None
        kwargs = {}
        if yerror_series != None:
            if yerror_series[i] != None:
                kwargs['yerr'] = yerror_series[i]

        if hatches != None:
            if hatches[i] != None:
                kwargs['hatch'] = hatches[i]

        if colors != None:
            if colors[i] != None:
                kwargs['color'] = colors[i]

        rect = ax.bar(ind + i * width,
                      data_series[i],
                      width,
                      log=y_log,
                      **kwargs)
        rects.append(rect)

    # apply labels
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_title(title)

    if legend_labels != None:
        kwargs = {}
        if legend_pos != None:
            kwargs['loc'] = legend_pos
        if legend_cols != None:
            kwargs['ncol'] = legend_cols

        ax.legend(rects, legend_labels, **kwargs)

    if x_labels != None:
        ax.set_xticks(ind + width * len(data_series) / 2.0)
        ax.set_xticklabels(x_labels)
        ax.autoscale()

    if y_res != None and y_range != None:
        ax.set_yticks(np.arange(y_range[0], y_range[1], y_res))

    # label bars
    if series_labels:
        for rect_series in rects:
            for rect in rect_series:
                height = rect.get_height()
                y_off = 0
                if y_log:
                    y_off = height * 1.05
                else:
                    y_off = height + y_max * 0.005

                ax.text(rect.get_x() + rect.get_width() / 2.0,
                        y_off,
                        '%3.2f' % float(height),
                        ha='center',
                        va='bottom')

    if x_range != None:
        ax.set_autoscalex_on(False)
        ax.set_xlim([x_range[0], x_range[1]])

    if y_range != None:
        ax.set_autoscaley_on(False)
        ax.set_ylim([y_range[0], y_range[1]])

    if x_log:
        ax.set_xscale('log')

    if y_log:
        ax.set_yscale('log', nonposy="clip")
Beispiel #4
0
def make_lines( aggregate, yerror_aggregate=None, series_order=None, x_labels=None, x_ticks=None, series_labels=False, point_yoffs=None, legend_labels=None, styles=None, title="Title", xlabel="X", ylabel="Y", x_range=None, y_range=None, y_res=None, legend_pos=None, x_log=False, y_log=False ):

   if series_order == None:
      series_order = common.graph_default_order( aggregate )
      
   y_data_series, yerror_series = common.graph_series( aggregate, yerror_aggregate, series_order )
   
   x_data_series = None
   if x_ticks != None:
      x_data_series = x_ticks
   else:
      x_data_series = range(0, len(y_data_series))

   data_series = []
   yerror = []
   
   for i in xrange(0, len(y_data_series[0])):
      data_series.append( [] )
      yerror.append( [] )
      
   for i in xrange(0, len(y_data_series)):
      xs = [i] * len(y_data_series[i])
      pts = zip(xs, y_data_series[i])

      k = 0
      for j in xrange(0,len(data_series)):
         data_series[j].append( pts[k] )
         yerror[j].append( yerror_series[j][k] )
         k += 1
   
   fig = plt.figure()
   ax = fig.add_subplot( 111 )
   lines = []

   if styles == None:
      styles = default_styles( len(data_series) )

   for i in xrange(0,len(data_series)):
      x_series = [x for (x,y) in data_series[i]]
      y_series = [y for (x,y) in data_series[i]]

      style = 'k'
      if styles != None:
         if styles[i] != None:
            style = styles[i]

      ll, = ax.plot( x_series, y_series, style, markersize=10 )
      lines.append(ll)

      if yerror != None:
         if yerror[i] != None:
            ax.errorbar( x_series, y_series, yerr=yerror[i] )

   # apply labels
   ax.set_xlabel( xlabel )
   ax.set_ylabel( ylabel )
   ax.set_title( title )

   if legend_labels == None:
      legend_labels = common.graph_legend_labels( series_order )
      
   if legend_labels != None:
      kwargs={}
      if legend_pos != None:
         kwargs['loc'] = legend_pos
      ax.legend( lines, legend_labels, **kwargs )


   if x_ticks == None:
      x_ticks = x_data_series
      
   if x_labels == None:
      x_labels = [str(x) for x in x_ticks]
      
   if x_labels != None and x_ticks != None:
      ax.set_xticks( x_ticks )
      ax.set_xticklabels( x_labels )
      ax.autoscale()

   if y_res != None and y_range != None:
      ax.set_yticks( np.arange(y_range[0], y_range[1], y_res) )

   if x_range != None:
      ax.set_autoscalex_on(False)
      ax.set_xlim( [x_range[0], x_range[1]] )

   if y_range != None:
      ax.set_autoscaley_on(False)
      ax.set_ylim( [y_range[0], y_range[1]] )

   # label points
   if series_labels:
      j=0
      for ll in lines:
         x_series = ll.get_xdata()
         y_series = ll.get_ydata()
         i = 0
         for (x,y) in zip(x_series, y_series):
            yoff = y
            if point_yoffs:
               yoff = y + point_yoffs[j][i]

            i+=1
            ax.text( x, yoff, '%3.2f' % float(y), ha = 'left', va = 'bottom' )
         j+=1

   if x_log:
      ax.set_xscale('log')

   if y_log:
      ax.set_yscale('log', nonposy="clip")