import h5tools
 
 import os
+import matplotlib
+matplotlib.use('Agg')
 
-if 'DISPLAY' not in os.environ:
-    import matplotlib
-    matplotlib.use('Agg')
+from matplotlib.figure import Figure
+from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
+
+#if 'DISPLAY' not in os.environ:
+#    import matplotlib
+#    matplotlib.use('Agg')
 
 import matplotlib.pyplot as plt
 
     def _render_plot(self, ax):
         return NotImplemented
 
-    def render(self, size=(1024, 256), dpi=80):
-
+    def _render_PIL(self, size=(1024, 256), dpi=80):
+        from ..grapher.core import Image
         image_width, image_height = size
 
         xSize = image_width / dpi
         ySize = image_height / dpi
 
-        fig = plt.figure(figsize=(xSize, ySize), dpi=dpi)
+        fig = Figure(figsize=(xSize, ySize), dpi=dpi)
+
+        ax = fig.add_axes([0, 0, 1, 1], frame_on=False)
 
-        ax = plt.Axes(fig, [0, 0, 1, 1])
-        ax.set_frame_on(False)
         self._render_plot(ax)
 
-#        ax.axis('off')
-       # ax.axis('tight')
         ax.autoscale(axis='x', tight=True)
-        fig.add_axes(ax)
 
-        return fig
+        # Export to PIL image
+        from StringIO import StringIO
+        imgdata = StringIO()
+        canvas = FigureCanvas(fig)
+        canvas.print_png(imgdata, dpi=dpi)
+        imgdata.seek(0)  # rewind the data
+
+        return Image.open(imgdata)
 
     @property
     def data_mode(self):
 
     def __init__(self, width=1024, height=256, bg_color=(0, 0, 0),
                  color_scheme='default'):
         super(DisplayAnalyzer, self).__init__(width, height, bg_color,
-                                               color_scheme)
-        #self.dpi = 72
+                                              color_scheme)
 
         self._result_id = None
         self._id = NotImplemented
     def post_process(self):
         parent_result = self.process_pipe.results[self._result_id]
 
-        fig = parent_result.render((self.image_width,
-                                    self.image_height), self.dpi)
-
-        # Export to PIL image
-        import StringIO
-        imgdata = StringIO.StringIO()
-        fig.savefig(imgdata, format='png', dpi=self.dpi)
-        imgdata.seek(0)  # rewind the data
-        self.image = Image.open(imgdata)
+        self.image = parent_result._render_PIL((self.image_width,
+                                                self.image_height), self.dpi)
 
     @classmethod
     def create(cls, analyzer, result_id, grapher_id, grapher_name):
 
         class NewGrapher(cls):
 
-
             _id = grapher_id
 
             implements(IGrapher)