[Python-modules-commits] [python-mplexporter] 36/135: add mplobj to renderers

Wolfgang Borgert debacle at moszumanska.debian.org
Tue Sep 23 21:19:01 UTC 2014


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to branch master
in repository python-mplexporter.

commit c400eaf24ca051e526bde09977c61e60dc2bd811
Author: Jake Vanderplas <vanderplas at astro.washington.edu>
Date:   Mon Feb 24 13:36:21 2014 -0800

    add mplobj to renderers
---
 mplexporter/exporter.py                         | 16 ++++++++-----
 mplexporter/renderers/base.py                   | 30 +++++++++++++++++--------
 mplexporter/renderers/example_renderer.py       |  4 ++--
 mplexporter/renderers/plotly/plotly_renderer.py |  4 ++--
 mplexporter/renderers/vega_renderer.py          |  4 ++--
 mplexporter/renderers/vincent_renderer.py       |  4 ++--
 mplexporter/tests/test_utils.py                 |  2 +-
 notebooks/PlotlyTest.ipynb                      | 16 ++++++-------
 notebooks/VegaTest.ipynb                        | 14 ++++++------
 notebooks/VincentTest.ipynb                     | 22 ++++++++++++------
 10 files changed, 70 insertions(+), 46 deletions(-)

diff --git a/mplexporter/exporter.py b/mplexporter/exporter.py
index 1b1948d..1643569 100644
--- a/mplexporter/exporter.py
+++ b/mplexporter/exporter.py
@@ -132,13 +132,13 @@ class Exporter(object):
             if linestyle['dasharray'] not in ['None', 'none', None]:
                 self.renderer.draw_line(data,
                                         coordinates=code,
-                                        style=linestyle)
+                                        style=linestyle, mplobj=line)
 
             markerstyle = utils.get_marker_style(line)
             if markerstyle['marker'] not in ['None', 'none', None]:
                 self.renderer.draw_markers(data,
                                            coordinates=code,
-                                           style=markerstyle)
+                                           style=markerstyle, mplobj=line)
 
     def _extract_texts(self, ax):
         for text in ax.texts:
@@ -156,7 +156,8 @@ class Exporter(object):
                 code, position = self._process_transform(transform, ax,
                                                          position)
                 style = utils.get_text_style(text)
-                self.renderer.draw_text(content, position, code, style)
+                self.renderer.draw_text(content, position, code,
+                                        style, mplobj=text)
 
     def _extract_patches(self, ax):
         for patch in ax.patches:
@@ -168,7 +169,8 @@ class Exporter(object):
             self.renderer.draw_path(vertices,
                                     coordinates=coordinates,
                                     pathcodes=pathcodes,
-                                    style=linestyle)
+                                    style=linestyle,
+                                    mplobj=patch)
 
     def _extract_collections(self, ax):
         for collection in ax.collections:
@@ -200,7 +202,8 @@ class Exporter(object):
                                                offsets,
                                                offset_coordinates,
                                                offset_order,
-                                               styles)
+                                               styles,
+                                               mplobj=collection)
 
     def _extract_images(self, ax):
         for image in ax.images:
@@ -209,4 +212,5 @@ class Exporter(object):
                                      extent=image.get_extent(),
                                      coordinates="data",
                                      style={"alpha": image.get_alpha(),
-                                            "zorder": image.get_zorder()})
+                                            "zorder": image.get_zorder()},
+                                     mplobj=image)
diff --git a/mplexporter/renderers/base.py b/mplexporter/renderers/base.py
index 7ee0ea6..a8382bd 100644
--- a/mplexporter/renderers/base.py
+++ b/mplexporter/renderers/base.py
@@ -74,7 +74,7 @@ class Renderer(object):
     def close_axes(self, ax):
         pass
 
-    def draw_line(self, data, coordinates, style):
+    def draw_line(self, data, coordinates, style, mplobj=None):
         """
         Draw a line. By default, draw the line via the draw_path() command.
         Some renderers might wish to override this and provide more
@@ -92,12 +92,14 @@ class Renderer(object):
             or 'figure' for figure (pixel) coordinates.
         style : dictionary
             a dictionary specifying the appearance of the line.
+        mplobj : matplotlib object
+            the matplotlib plot element which generated this line
         """
         pathcodes = ['M'] + (data.shape[0] - 1) * ['L']
         pathstyle = dict(facecolor='none', **style)
         pathstyle['edgecolor'] = pathstyle.pop('color')
         pathstyle['edgewidth'] = pathstyle.pop('linewidth')
-        self.draw_path(data, coordinates, pathcodes, pathstyle)
+        self.draw_path(data, coordinates, pathcodes, pathstyle, mplobj=mplobj)
 
     def _iter_path_collection(self, paths, path_transforms, offsets, styles):
         """Build an iterator over the elements of the path collection"""
@@ -115,7 +117,7 @@ class Renderer(object):
 
     def draw_path_collection(self, paths, path_coordinates, path_transforms,
                              offsets, offset_coordinates, offset_order,
-                             styles):
+                             styles, mplobj=None):
         """
         Draw a collection of paths. The paths, offsets, and styles are all
         iterables, and the number of paths is max(len(paths), len(offsets)).
@@ -153,6 +155,8 @@ class Renderer(object):
         styles: dictionary
             A dictionary in which each value is a list of length N, containing
             the style(s) for the paths.
+        mplobj : matplotlib object
+            the matplotlib plot element which generated this collection
         """
         if offset_order == "before":
             raise NotImplementedError("offset before transform")
@@ -171,9 +175,9 @@ class Renderer(object):
                    "edgewidth":lw,
                    "dasharray":"10,0", "alpha":styles['alpha']}
             self.draw_path(vertices, path_coordinates, pathcodes, style,
-                           offset, offset_coordinates)
+                           offset, offset_coordinates, mplobj=mplobj)
 
-    def draw_markers(self, data, coordinates, style):
+    def draw_markers(self, data, coordinates, style, mplobj=None):
         """
         Draw a set of markers. By default, this is done by repeatedly
         calling draw_path(), but renderers should generally overload
@@ -190,6 +194,8 @@ class Renderer(object):
             or 'figure' for figure (pixel) coordinates.
         style : dictionary
             a dictionary specifying the appearance of the markers.
+        mplobj : matplotlib object
+            the matplotlib plot element which generated this marker collection
         """
         vertices, pathcodes = style['markerpath']
         pathstyle = dict((key, style[key]) for key in ['alpha', 'edgecolor',
@@ -198,9 +204,9 @@ class Renderer(object):
         pathstyle['dasharray'] = "10,0"
         for vertex in data:
             self.draw_path(vertices, "points", pathcodes, pathstyle,
-                           vertex, coordinates)
+                           vertex, coordinates, mplobj=mplobj)
 
-    def draw_text(self, text, position, coordinates, style):
+    def draw_text(self, text, position, coordinates, style, mplobj=None):
         """
         Draw text on the image.
 
@@ -215,11 +221,13 @@ class Renderer(object):
             or 'figure' for figure (pixel) coordinates.
         style : dictionary
             a dictionary specifying the appearance of the text.
+        mplobj : matplotlib object
+            the matplotlib plot element which generated this text
         """
         raise NotImplementedError()
 
     def draw_path(self, data, coordinates, pathcodes, style,
-                  offset=None, offset_coordinates="data"):
+                  offset=None, offset_coordinates="data", mplobj=None):
         """
         Draw a path.
 
@@ -250,10 +258,12 @@ class Renderer(object):
         offset_coordinates : string (optional)
             A string code, which should be either 'data' for data coordinates,
             or 'figure' for figure (pixel) coordinates.
+        mplobj : matplotlib object
+            the matplotlib plot element which generated this path
         """
         raise NotImplementedError()
 
-    def draw_image(self, imdata, extent, coordinates, style):
+    def draw_image(self, imdata, extent, coordinates, style, mplobj=None):
         """
         Draw an image.
 
@@ -268,5 +278,7 @@ class Renderer(object):
             or 'figure' for figure (pixel) coordinates.
         style : dictionary
             a dictionary specifying the appearance of the image
+        mplobj : matplotlib object
+            the matplotlib plot object which generated this image
         """
         raise NotImplementedError()
diff --git a/mplexporter/renderers/example_renderer.py b/mplexporter/renderers/example_renderer.py
index 3abde7a..a8834c5 100644
--- a/mplexporter/renderers/example_renderer.py
+++ b/mplexporter/renderers/example_renderer.py
@@ -22,8 +22,8 @@ class ExampleRenderer(Renderer):
     def close_axes(self, ax):
         self.output += "  closing axes\n"
 
-    def draw_line(self, data, coordinates, style):
+    def draw_line(self, data, coordinates, style, mplobj=None):
         self.output += "    draw line with {0} points\n".format(data.shape[0])
 
-    def draw_markers(self, data, coordinates, style):
+    def draw_markers(self, data, coordinates, style, mplobj=None):
         self.output += "    draw {0} markers\n".format(data.shape[0])
diff --git a/mplexporter/renderers/plotly/plotly_renderer.py b/mplexporter/renderers/plotly/plotly_renderer.py
index 5c50f3d..625a38a 100644
--- a/mplexporter/renderers/plotly/plotly_renderer.py
+++ b/mplexporter/renderers/plotly/plotly_renderer.py
@@ -47,7 +47,7 @@ class PlotlyRenderer(Renderer):
     def close_axes(self, ax):
         self.output += "  closing axes\n"
 
-    def draw_line(self, data, coordinates, style):
+    def draw_line(self, data, coordinates, style, mplobj=None):
         self.output += "    draw line with {0} points\n".format(data.shape[0])
         data_dict = {'x': [], 'y': []}
         for xy_pair in data:
@@ -60,7 +60,7 @@ class PlotlyRenderer(Renderer):
         data_dict['line']['dash'] = plotly_utils.convert_dash(style['dasharray'])
         self.data += data_dict,
 
-    def draw_markers(self, data, coordinates, style):
+    def draw_markers(self, data, coordinates, style, mplobj=None):
         self.output += "    draw {0} markers\n".format(data.shape[0])
         data_dict = {'x': [], 'y': []}
         for xy_pair in data:
diff --git a/mplexporter/renderers/vega_renderer.py b/mplexporter/renderers/vega_renderer.py
index f6db4fe..1680f8b 100644
--- a/mplexporter/renderers/vega_renderer.py
+++ b/mplexporter/renderers/vega_renderer.py
@@ -33,7 +33,7 @@ class VegaRenderer(Renderer):
                             range="height",
                         ),]
 
-    def draw_line(self, data, coordinates, style):
+    def draw_line(self, data, coordinates, style, mplobj=None):
         if coordinates != 'data':
             warnings.warn("Only data coordinates supported. Skipping this")
         dataname = "table{0:03d}".format(len(self.data) + 1)
@@ -55,7 +55,7 @@ class VegaRenderer(Renderer):
                            }
                        })
 
-    def draw_markers(self, data, coordinates, style):
+    def draw_markers(self, data, coordinates, style, mplobj=None):
         if coordinates != 'data':
             warnings.warn("Only data coordinates supported. Skipping this")
         dataname = "table{0:03d}".format(len(self.data) + 1)
diff --git a/mplexporter/renderers/vincent_renderer.py b/mplexporter/renderers/vincent_renderer.py
index 657d3d4..a6a7a6b 100644
--- a/mplexporter/renderers/vincent_renderer.py
+++ b/mplexporter/renderers/vincent_renderer.py
@@ -9,7 +9,7 @@ class VincentRenderer(Renderer):
         self.figwidth = int(properties['figwidth'] * properties['dpi'])
         self.figheight = int(properties['figheight'] * properties['dpi'])
 
-    def draw_line(self, data, coordinates, style):
+    def draw_line(self, data, coordinates, style, mplobj=None):
         import vincent  # only import if VincentRenderer is used
         if coordinates != 'data':
             warnings.warn("Only data coordinates supported. Skipping this")
@@ -26,7 +26,7 @@ class VincentRenderer(Renderer):
         else:
             warnings.warn("Multiple plot elements not yet supported")
 
-    def draw_markers(self, data, coordinates, style):
+    def draw_markers(self, data, coordinates, style, mplobj=None):
         import vincent  # only import if VincentRenderer is used
         if coordinates != 'data':
             warnings.warn("Only data coordinates supported. Skipping this")
diff --git a/mplexporter/tests/test_utils.py b/mplexporter/tests/test_utils.py
index ac22b86..20b7aaf 100644
--- a/mplexporter/tests/test_utils.py
+++ b/mplexporter/tests/test_utils.py
@@ -7,5 +7,5 @@ def test_path_data():
     circle = plt.Circle((0, 0), 1)
     vertices, codes = utils.SVG_path(circle.get_path())
 
-    assert_allclose(vertices.shape, (26, 2))
+    assert_allclose(vertices.shape, (25, 2))
     assert_equal(codes, ['M', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'Z'])
diff --git a/notebooks/PlotlyTest.ipynb b/notebooks/PlotlyTest.ipynb
index 4910c2b..3e3c1a0 100644
--- a/notebooks/PlotlyTest.ipynb
+++ b/notebooks/PlotlyTest.ipynb
@@ -19,7 +19,7 @@
      "language": "python",
      "metadata": {},
      "outputs": [],
-     "prompt_number": 9
+     "prompt_number": 1
     },
     {
      "cell_type": "code",
@@ -31,7 +31,7 @@
      "language": "python",
      "metadata": {},
      "outputs": [],
-     "prompt_number": 11
+     "prompt_number": 2
     },
     {
      "cell_type": "code",
@@ -57,11 +57,11 @@
        "output_type": "display_data",
        "png": "iVBORw0KGgoAAAANSUhEUgAAAY0AAAEZCAYAAABrUHmEAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XtcFdXaB/DfRvCOgKiEwAndeBRFETMVfRW0EEQlu6m9\nnaOmmaV5qc57NNC8lL6a1ZuXjpnlpZOV5fHKxcwS9KSItyNeUUgNUFATRVFDYb1/DCCXvdnD3nNZ\nM/N8P5/94bJnzzyz1mIWM7PWMybGGAMhhBAigpPaARBCCNEO6jQIIYSIRp0GIYQQ0ajTIIQQIhp1\nGoQQQkSjToMQQoho1GkQwpG9e/eiQ4cOaodBiFXUaRBD8ff3x88//wwAWLt2Lfr27Vvl/TFjxmDd\nunWyxnDy5EkMHDgQnp6e8PDwQPfu3ZGUlAQA6Nu3L86cOSPr9glxhLPaARCiJJPJ5ND7Uhg6dCgm\nTZqExMREM [...]
        "text": [
-        "<matplotlib.figure.Figure at 0x10f855c90>"
+        "<matplotlib.figure.Figure at 0x1062a6710>"
        ]
       }
      ],
-     "prompt_number": 12
+     "prompt_number": 3
     },
     {
      "cell_type": "code",
@@ -74,17 +74,17 @@
      "outputs": [
       {
        "html": [
-        "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/1190/600/450\" width=\"650\"></iframe>"
+        "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/1249/600/450\" width=\"650\"></iframe>"
        ],
        "metadata": {},
        "output_type": "pyout",
-       "prompt_number": 13,
+       "prompt_number": 4,
        "text": [
-        "<IPython.core.display.HTML at 0x10f86c1d0>"
+        "<IPython.core.display.HTML at 0x10629dcd0>"
        ]
       }
      ],
-     "prompt_number": 13
+     "prompt_number": 4
     },
     {
      "cell_type": "code",
diff --git a/notebooks/VegaTest.ipynb b/notebooks/VegaTest.ipynb
index b86e7c1..fb02f7e 100644
--- a/notebooks/VegaTest.ipynb
+++ b/notebooks/VegaTest.ipynb
@@ -77,7 +77,7 @@
        "metadata": {},
        "output_type": "display_data",
        "text": [
-        "<IPython.core.display.HTML at 0x102691e10>"
+        "<IPython.core.display.HTML at 0x10258be10>"
        ]
       }
      ],
@@ -106,7 +106,7 @@
      "outputs": [
       {
        "html": [
-        "<div id=\"vis35909\"></div><script>\n",
+        "<div id=\"vis37347\"></div><script>\n",
         "\n",
         "( function() {\n",
         "  var _do_plot = function() {\n",
@@ -115,7 +115,7 @@
         "      return;\n",
         "    }\n",
         "    vg.parse.spec({\"width\": 480, \"scales\": [{\"range\": \"width\", \"type\": \"linear\", \"domain\": [0.0, 10.0], \"name\": \"x\"}, {\"range\": \"height\", \"type\": \"linear\", \"domain\": [-1.3999999999999999, 1.3999999999999999], \"name\": \"y\"}], \"marks\": [{\"from\": {\"data\": \"table001\"}, \"type\": \"line\", \"properties\": {\"enter\": {\"strokeWidth\": {\"value\": 3}, \"strokeOpacity\": {\"value\": 0.5}, \"stroke\": {\"value\": \"#FF0000\"}, \"interpolate\": {\"v [...]
-        "      chart({el: \"#vis35909\"}).update();\n",
+        "      chart({el: \"#vis37347\"}).update();\n",
         "    });\n",
         "  };\n",
         "  _do_plot();\n",
@@ -126,7 +126,7 @@
        "output_type": "pyout",
        "prompt_number": 3,
        "text": [
-        "<mplexporter.renderers.vega_renderer.VegaHTML at 0x102691bd0>"
+        "<mplexporter.renderers.vega_renderer.VegaHTML at 0x10258bbd0>"
        ]
       }
      ],
@@ -153,7 +153,7 @@
        "output_type": "stream",
        "stream": "stdout",
        "text": [
-        "<div id=\"vis33550\"></div><script>\n",
+        "<div id=\"vis43350\"></div><script>\n",
         "\n",
         "( function() {\n",
         "  var _do_plot = function() {\n",
@@ -162,7 +162,7 @@
         "      return;\n",
         "    }\n",
         "    vg.parse.spec({\"width\": 480, \"scales\": [{\"range\": \"width\", \"type\": \"linear\", \"domain\": [0.0, 10.0], \"name\": \"x\"}, {\"range\": \"height\", \"type\": \"linear\", \"domain\": [-1.3999999999999999, 1.3999999999999999], \"name\": \"y\"}], \"marks\": [{\"from\": {\"data\": \"table001\"}, \"type\": \"line\", \"properties\": {\"enter\": {\"strokeWidth\": {\"value\": 3}, \"strokeOpacity\": {\"value\": 0.5}, \"stroke\": {\"value\": \"#FF0000\"}, \"interpolate\": {\"v [...]
-        "      chart({el: \"#vis33550\"}).update();\n",
+        "      chart({el: \"#vis43350\"}).update();\n",
         "    });\n",
         "  };\n",
         "  _do_plot();\n",
@@ -172,7 +172,7 @@
        ]
       }
      ],
-     "prompt_number": 5
+     "prompt_number": 4
     },
     {
      "cell_type": "code",
diff --git a/notebooks/VincentTest.ipynb b/notebooks/VincentTest.ipynb
index 1d8c6e3..f614d31 100644
--- a/notebooks/VincentTest.ipynb
+++ b/notebooks/VincentTest.ipynb
@@ -90,7 +90,7 @@
        "metadata": {},
        "output_type": "display_data",
        "text": [
-        "<IPython.core.display.HTML at 0x106114110>"
+        "<IPython.core.display.HTML at 0x105f9b110>"
        ]
       }
      ],
@@ -150,7 +150,7 @@
      "outputs": [
       {
        "html": [
-        "<div id=\"vis22164\"></div><script>\n",
+        "<div id=\"vis46104\"></div><script>\n",
         "\n",
         "( function() {\n",
         "  var _do_plot = function() {\n",
@@ -159,7 +159,7 @@
         "      return;\n",
         "    }\n",
         "    vg.parse.spec({\"axes\": [{\"scale\": \"x\", \"type\": \"x\"}, {\"scale\": \"y\", \"type\": \"y\"}], \"data\": [{\"name\": \"table\", \"values\": [{\"col\": \"y\", \"idx\": 0.0, \"val\": 0.0}, {\"col\": \"y\", \"idx\": 0.10101010101010101, \"val\": 0.10083842025810461}, {\"col\": \"y\", \"idx\": 0.20202020202020202, \"val\": 0.20064885652268541}, {\"col\": \"y\", \"idx\": 0.30303030303030304, \"val\": 0.2984138044476411}, {\"col\": \"y\", \"idx\": 0.40404040404040403, \"val\ [...]
-        "      chart({el: \"#vis22164\"}).update();\n",
+        "      chart({el: \"#vis46104\"}).update();\n",
         "    });\n",
         "  };\n",
         "  _do_plot();\n",
@@ -171,7 +171,7 @@
        "output_type": "pyout",
        "prompt_number": 6,
        "text": [
-        "<IPython.core.display.HTML at 0x105ea28d0>"
+        "<IPython.core.display.HTML at 0x107142b50>"
        ]
       }
      ],
@@ -195,7 +195,7 @@
      "outputs": [
       {
        "html": [
-        "<div id=\"vis14560\"></div><script>\n",
+        "<div id=\"vis10437\"></div><script>\n",
         "\n",
         "( function() {\n",
         "  var _do_plot = function() {\n",
@@ -204,7 +204,7 @@
         "      return;\n",
         "    }\n",
         "    vg.parse.spec({\"axes\": [{\"scale\": \"x\", \"type\": \"x\"}, {\"scale\": \"y\", \"type\": \"y\"}], \"data\": [{\"name\": \"table\", \"values\": [{\"col\": \"y\", \"idx\": 1.764052345967664, \"val\": -0.89546656119367563}, {\"col\": \"y\", \"idx\": 0.40015720836722329, \"val\": 0.38690249785926201}, {\"col\": \"y\", \"idx\": 0.9787379841057392, \"val\": -0.51080513756887302}, {\"col\": \"y\", \"idx\": 2.2408931992014578, \"val\": -1.1806321841224121}, {\"col\": \"y\", \"idx [...]
-        "      chart({el: \"#vis14560\"}).update();\n",
+        "      chart({el: \"#vis10437\"}).update();\n",
         "    });\n",
         "  };\n",
         "  _do_plot();\n",
@@ -216,11 +216,19 @@
        "output_type": "pyout",
        "prompt_number": 7,
        "text": [
-        "<IPython.core.display.HTML at 0x107091f90>"
+        "<IPython.core.display.HTML at 0x10718d250>"
        ]
       }
      ],
      "prompt_number": 7
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
     }
    ],
    "metadata": {}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-mplexporter.git



More information about the Python-modules-commits mailing list