# File mrplot.rb, line 243
    def draw(context, rect, border, plots=Array.new, axes_top = true, grid_top = false)
      # Exchange top/bottom in the border, because the window space itself is set in context space coordinates
      border.top, border.bottom   = border.bottom, border.top
      # Set the windowspace to the rect and use it
      context.windowspace         = rect.cut(border)
      # And change back because now everything is in window space
      border.top, border.bottom   = border.bottom, border.top
      
      # Sort the figures
      grid_figures                = Array.new
      plot_figures                = Array.new
      axes_figures                = Array.new
      top_figures                 = Array.new
      
      @figures.each do |figure|  
        case figure.layer
          when :grid
            grid_figures << figure
          when :plots
            plot_figures << figure
          when :axes
            axes_figures << figure
          when :figure
            top_figures << figure
        end
      end
      
      # Draw the grid at first (if not set to top)
      if !grid_top
        grid_figures.each{ |figure| figure.draw(context, @space)}
        @grid.draw(context, @space) if @grid
      end
      
      if !axes_top
        axes_figures.each{ |figure| figure.draw(context, @space)}
        @axes.draw(context, @space) if @axes
      end
      
      # Draw all plots
      plot_figures.each{ |figure| figure.draw(context, @space)}
      context.clip = true
      draw_naked(context)
      plots.each { |plot| plot.draw_naked(context) }
      context.clip = false
      
      if grid_top
        grid_figures.each{ |figure| figure.draw(context, @space)}
        @grid.draw(context, @space) if @grid
      end
      
      # Draw axes
      if axes_top
        axes_figures.each{ |figure| figure.draw(context, @space)}
        @axes.draw(context, @space) if @axes
      end
      
      # Draw all figures and labels
      top_figures.each{ |figure| figure.draw(context, @space)}
  
      # Draw the history
      if @history
        all_plots = Array.new
        
        all_plots << self
        all_plots.insert(1, *plots)
        
        @history.plots = all_plots
        @history.draw(context, @space) 
      end
              
      # Draw
      if @title
        neworigin = context.inverse_transform(Point.new(rect.x,rect.y))
        @title.rect = Rect.new(neworigin.x,neworigin.y-border.top, rect.width, border.top)
        @title.draw(context, @space)
      end
      
      if @description
        neworigin = context.inverse_transform(Point.new(rect.x,rect.y+rect.height))
        @description.rect = Rect.new(neworigin.x,neworigin.y, rect.width, border.bottom)  
        @description.draw(context, @space)
      end
      
    end