[Python-modules-commits] [python-mplexporter] 26/135: Reformatted PlotlyRenderer in plotly_renderer to be a bit cleaner.

Wolfgang Borgert debacle at moszumanska.debian.org
Tue Sep 23 21:19:00 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 ce4616b24c6a24b576d22c23831e3e98cf8d033f
Author: theengineear <andseier at gmail.com>
Date:   Thu Feb 20 15:22:43 2014 -0800

    Reformatted PlotlyRenderer in plotly_renderer to be a bit cleaner.
    
    Dictionaries are more explicitly created to make them more readable/editable.
---
 examples/plotly_example.py                      |  8 ++-
 mplexporter/renderers/plotly/plotly_renderer.py | 87 ++++++++++++++-----------
 2 files changed, 56 insertions(+), 39 deletions(-)

diff --git a/examples/plotly_example.py b/examples/plotly_example.py
index 12fe1f3..abb4db0 100644
--- a/examples/plotly_example.py
+++ b/examples/plotly_example.py
@@ -1,4 +1,4 @@
-from mplexporter.renderers.plotly import PlotlyRenderer, fig_to_plotly
+from mplexporter.renderers import PlotlyRenderer, fig_to_plotly
 import numpy as np
 import matplotlib.pyplot as plt
 username = 'IPython.Demo'
@@ -10,8 +10,11 @@ def plot_sin():
     x = np.arange(0,1.0,0.01)
     y1 = np.sin(2*np.pi*x)
     y2 = np.cos(4*np.pi*x)
+    plt.figure(1)
+    plt.subplot(211)
     plt.plot(x, y1, 'b--', label='sin')
-    plt.plot(x, y2, 'ro', label='cos')
+    plt.subplot(212)
+    plt.plot(x, y2, 'go', label='cos')
     plt.title("It's a Sign")
     plt.xlabel('time')
     plt.ylabel('amplitude')
@@ -19,6 +22,7 @@ def plot_sin():
 
     # export info from matplotlib fig and render with plotly!
     fig = plt.gcf()
+    plt.show()
     fig_to_plotly(fig, username, api_key)
 
 if __name__ == '__main__':
diff --git a/mplexporter/renderers/plotly/plotly_renderer.py b/mplexporter/renderers/plotly/plotly_renderer.py
index 5c50f3d..d83af15 100644
--- a/mplexporter/renderers/plotly/plotly_renderer.py
+++ b/mplexporter/renderers/plotly/plotly_renderer.py
@@ -30,50 +30,63 @@ class PlotlyRenderer(Renderer):
     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
+        layout = {
+            'title': properties['title'],
+            'xaxis': {
+                'range': properties['xlim'],
+                'title': properties['xlabel'],
+                'showgrid': properties['xgrid']
+            },
+            'yaxis': {
+                'range': properties['ylim'],
+                'title': properties['ylabel'],
+                'showgrid': properties['ygrid'],
+            }
+        }
+        for key, value in layout.items():
+            self.layout[key] = value
 
     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,
+        if coordinates == 'data':
+            self.output += "    draw line with {0} points\n".format(data.shape[0])
+            trace = {
+                'mode': 'lines',
+                'x': [xy_pair[0] for xy_pair in data],
+                'y': [xy_pair[1] for xy_pair in data],
+                'line': {
+                    'opacity': style['alpha'],
+                    'width': style['linewidth'],
+                    'dash': plotly_utils.convert_dash(style['dasharray'])
+                }
+            }
+            self.data += trace,
+        else:
+            self.output += "    received {}-point line with 'figure' coordinates, skipping!".format(data.shape[0])
 
     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,
+        if coordinates == 'data':
+            self.output += "    draw {0} markers\n".format(data.shape[0])
+            trace = {
+                'mode': 'markers',
+                'x': [xy_pair[0] for xy_pair in data],
+                'y': [xy_pair[1] for xy_pair in data],
+                'marker': {
+                    'opacity': style['alpha'],
+                    'color': style['facecolor'],
+                    'symbol': plotly_utils.convert_symbol(style['marker']),
+                    'line': {
+                        'color': style['edgecolor'],
+                        'width': style['edgewidth']
+                    }
+                }
+            }
+            # not sure whether we need to incorporate style['markerpath']
+            self.data += trace,
+        else:
+            self.output += "    received {} markers with 'figure' coordinates, skipping!".format(data.shape[0])
 
 
 def fig_to_plotly(fig, username=None, api_key=None, notebook=False):

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