[Python-modules-commits] [python-mplexporter] 11/135: Added plotly subpackage to renderers package for PlotlyRenderer obj.
Wolfgang Borgert
debacle at moszumanska.debian.org
Tue Sep 23 21:18:58 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 c159eb5f75be83b5195fe108260e596a1e38a646
Author: theengineear <andseier at gmail.com>
Date: Tue Feb 18 18:25:32 2014 -0800
Added plotly subpackage to renderers package for PlotlyRenderer obj.
Plotly subpackage includes the plotly_renderer module which contains the
PlotlyRenderer object which inherits from the renderers.base.Renderer
class. It reimplements the suggested methods commented on in
renderers.base.Renderer.
The plotly_utils module is included to separate basic style and
translation functionality from the main objective of the plotly_renderer
module which is to construct the data and layout dictionaries.
The PlotlyRenderer may differ slightly from the functionality of other
renderers as it just 'updates' its member dictionaries (self.data and
self.layout) as the Exporter object crawls around the matplotlib figure.
When the Exporter reaches the end of its crawl and the renderer 'closes'
the figure, PlotlyRenderer makes a call to plotly with the data and
layout dictionaries. This causes your browser to open a page pointing to
your new plotly plot!
It may or may not be of use to keep the renderers separated by
subpackages as was implemented with the plotly subpackage. TBD.
---
mplexporter/renderers/plotly/__init__.py | 1 +
mplexporter/renderers/plotly/plotly_renderer.py | 78 +++++++++++++++++++++++++
mplexporter/renderers/plotly/plotly_utils.py | 36 ++++++++++++
3 files changed, 115 insertions(+)
diff --git a/mplexporter/renderers/plotly/__init__.py b/mplexporter/renderers/plotly/__init__.py
new file mode 100644
index 0000000..5b6144b
--- /dev/null
+++ b/mplexporter/renderers/plotly/__init__.py
@@ -0,0 +1 @@
+from .plotly_renderer import PlotlyRenderer
\ No newline at end of file
diff --git a/mplexporter/renderers/plotly/plotly_renderer.py b/mplexporter/renderers/plotly/plotly_renderer.py
new file mode 100644
index 0000000..efbabbb
--- /dev/null
+++ b/mplexporter/renderers/plotly/plotly_renderer.py
@@ -0,0 +1,78 @@
+"""
+Plotly Renderer
+================
+This is a renderer class to be used with an exporter for rendering plots in Plotly!
+"""
+import plotly
+
+from . import plotly_utils
+from .. base import Renderer
+
+
+class PlotlyRenderer(Renderer):
+ def __init__(self, username=None, api_key=None):
+ self.output = ""
+ self.username = username
+ self.api_key = api_key
+ self.data = []
+ self.layout = {}
+ self.axis_ct = 0
+
+ def open_figure(self, fig, properties):
+ self.output += "opening figure\n"
+ self.layout['width'] = int(properties['figwidth']*properties['dpi'])
+ self.layout['height'] = int(properties['figheight']*properties['dpi'])
+
+ def close_figure(self, fig):
+ self.output += "closing figure\n"
+ print self.output
+ py = plotly.plotly(self.username, self.api_key)
+ py.plot(self.data, layout=self.layout) # make call to plotly!
+
+ def open_axes(self, ax, properties):
+ self.output += " opening axes\n"
+ self.axis_ct += 1
+ layout = {}
+ layout['title'] = properties['title']
+ xaxis = {}
+ xaxis['range'] = properties['xlim']
+ xaxis['title'] = properties['xlabel']
+ xaxis['showgrid'] = properties['xgrid']
+ yaxis = {}
+ yaxis['range'] = properties['ylim']
+ yaxis['title'] = properties['ylabel']
+ yaxis['showgrid'] = properties['ygrid']
+ layout['xaxis'] = xaxis
+ layout['yaxis'] = yaxis
+ self.layout = layout
+
+ def close_axes(self, ax):
+ self.output += " closing axes\n"
+
+ def draw_line(self, data, coordinates, style):
+ self.output += " draw line with {0} points\n".format(data.shape[0])
+ data_dict = {'x': [], 'y': []}
+ for xy_pair in data:
+ data_dict['x'] += [xy_pair[0]]
+ data_dict['y'] += [xy_pair[1]]
+ data_dict['mode'] = 'lines'
+ data_dict['line'] = {}
+ data_dict['line']['opacity'] = style['alpha']
+ data_dict['line']['width'] = style['linewidth']
+ data_dict['line']['dash'] = plotly_utils.convert_dash(style['dasharray'])
+ self.data += data_dict,
+
+ def draw_markers(self, data, coordinates, style):
+ self.output += " draw {0} markers\n".format(data.shape[0])
+ data_dict = {'x': [], 'y': []}
+ for xy_pair in data:
+ data_dict['x'] += [xy_pair[0]]
+ data_dict['y'] += [xy_pair[1]]
+ data_dict['mode'] = 'markers'
+ data_dict['marker'] = {}
+ data_dict['marker']['opacity'] = style['alpha']
+ data_dict['marker']['color'] = style['facecolor']
+ # need to incorporate style['edgecolor']
+ data_dict['marker']['symbol'] = plotly_utils.convert_symbol(style['marker'])
+ # not sure whether we need to incorporate style['markerpath']
+ self.data += data_dict,
\ No newline at end of file
diff --git a/mplexporter/renderers/plotly/plotly_utils.py b/mplexporter/renderers/plotly/plotly_utils.py
new file mode 100644
index 0000000..7028ffd
--- /dev/null
+++ b/mplexporter/renderers/plotly/plotly_utils.py
@@ -0,0 +1,36 @@
+def convert_symbol(mpl_symbol):
+ if mpl_symbol in symbol_map:
+ return symbol_map[mpl_symbol]
+ else:
+ return 'dot' # default
+
+def convert_dash(mpl_dash):
+ if mpl_dash in dash_map:
+ return dash_map[mpl_dash]
+ else:
+ return 'solid' # default
+
+
+dash_map = {
+ '10,0': 'solid',
+ '6,6': 'dash',
+ '2,2': 'dot',
+ '4,4,2,4': 'dashdot',
+ 'none': 'solid'
+}
+
+symbol_map = {
+ 'o': 'dot',
+ 'v': 'triangle-down',
+ '^': 'triangle-up',
+ '<': 'triangle-left',
+ '>': 'triangle-right',
+ 's': 'square',
+ '+': 'cross',
+ 'x': 'x',
+ 'D': 'diamond',
+ 'd': 'diamond',
+ '-': 'solid',
+ '--': 'dash',
+ '-.': 'dashdot'
+}
\ No newline at end of file
--
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