[Python-modules-commits] [python-mplexporter] 07/135: add basic vega renderer

Wolfgang Borgert debacle at moszumanska.debian.org
Tue Sep 23 21:18:57 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 c473a2f4587388f945a6c83801a0bb26c378ba5f
Author: Jake Vanderplas <vanderplas at astro.washington.edu>
Date:   Mon Feb 17 09:59:36 2014 -0800

    add basic vega renderer
---
 mplexporter/renderers/__init__.py      |  1 +
 mplexporter/renderers/vega_renderer.py | 95 ++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+)

diff --git a/mplexporter/renderers/__init__.py b/mplexporter/renderers/__init__.py
index 4a1e7c7..a459909 100644
--- a/mplexporter/renderers/__init__.py
+++ b/mplexporter/renderers/__init__.py
@@ -1,3 +1,4 @@
 from .base import Renderer
+from .vega_renderer import VegaRenderer, fig_to_vega
 from .vincent_renderer import VincentRenderer, fig_to_vincent
 from .example_renderer import ExampleRenderer
diff --git a/mplexporter/renderers/vega_renderer.py b/mplexporter/renderers/vega_renderer.py
new file mode 100644
index 0000000..e505f41
--- /dev/null
+++ b/mplexporter/renderers/vega_renderer.py
@@ -0,0 +1,95 @@
+import warnings
+import json
+from .base import Renderer
+from ..exporter import Exporter
+
+
+class VegaRenderer(Renderer):
+    def open_figure(self, fig, properties):
+        self.properties = properties
+        self.figwidth = int(properties['figwidth'] * properties['dpi'])
+        self.figheight = int(properties['figheight'] * properties['dpi'])
+        self.data = []
+        self.scales = []
+        self.axes = []
+        self.marks = []
+            
+    def open_axes(self, ax, properties):
+        if len(self.axes) > 0:
+            warnings.warn("multiple axes not yet supported")
+        self.axes = [dict(type="x", scale="x", ticks=10),
+                     dict(type="y", scale="y", ticks=10)]
+        self.scales = [dict(name="x",
+                            domain=properties['xlim'],
+                            type="linear",
+                            range="width",
+                            title=properties['xlabel']
+                        ),
+                       dict(name="y",
+                            domain=properties['ylim'],
+                            type="linear",
+                            range="height",
+                            title=properties['ylabel']
+                        ),]
+
+    def draw_line(self, data, coordinates, style):
+        if coordinates != 'data':
+            warnings.warn("Only data coordinates supported. Skipping this")
+        dataname = "table{0:03d}".format(len(self.data) + 1)
+
+        # TODO: respect the other style settings
+        self.data.append({'name': dataname,
+                          'values': [dict(x=d[0], y=d[1]) for d in data]})
+        self.marks.append({'type': 'line',
+                           'from': {'data': dataname},
+                           'properties': {
+                               "enter": {
+                                   "interpolate": {"value": "monotone"},
+                                   "x": {"scale": "x", "field": "data.x"},
+                                   "y": {"scale": "y", "field": "data.y"},
+                                   "stroke": {"value": style['color']},
+                                   "strokeOpacity": {"value": style['alpha']},
+                                   "strokeWidth": {"value": style['width']},
+                               }
+                           }
+                       })
+
+    def draw_markers(self, data, coordinates, style):
+        if coordinates != 'data':
+            warnings.warn("Only data coordinates supported. Skipping this")
+        dataname = "table{0:03d}".format(len(self.data) + 1)
+
+        # TODO: respect the other style settings
+        self.data.append({'name': dataname,
+                          'values': [dict(x=d[0], y=d[1]) for d in data]})
+        self.marks.append({'type': 'symbol',
+                           'from': {'data': dataname},
+                           'properties': {
+                               "enter": {
+                                   "interpolate": {"value": "monotone"},
+                                   "x": {"scale": "x", "field": "data.x"},
+                                   "y": {"scale": "y", "field": "data.y"},
+                                   "fill": {"value": style['facecolor']},
+                                   "fillOpacity": {"value": style['alpha']},
+                                   "stroke": {"value": style['edgecolor']},
+                                   "strokeOpacity": {"value": style['alpha']},
+                                   "strokeWidth": {"value": style['edgewidth']},
+                               }
+                           }
+                       })
+
+    def dict(self):
+        return dict(width=self.figwidth,
+                    height=self.figheight,
+                    data=self.data,
+                    scales=self.scales,
+                    axes=self.axes,
+                    marks=self.marks)
+
+
+def fig_to_vega(fig):
+    """Convert a matplotlib figure to vega dictionary"""
+    renderer = VegaRenderer()
+    Exporter(renderer).run(fig)
+    return renderer.dict()
+    

-- 
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