[Python-modules-commits] [python-mplexporter] 27/135: Changed renderer to include colors on lines (whoops) + examples.

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 a09e7c8c2975d29f106407efb127281f24b6ad74
Author: theengineear <andseier at gmail.com>
Date:   Thu Feb 20 19:38:17 2014 -0800

    Changed renderer to include colors on lines (whoops) + examples.
---
 examples/plotly_example.py                      | 35 ++++++++-
 mplexporter/renderers/plotly/plotly_renderer.py | 99 +++++++++++++++++++------
 2 files changed, 106 insertions(+), 28 deletions(-)

diff --git a/examples/plotly_example.py b/examples/plotly_example.py
index abb4db0..17eeb8e 100644
--- a/examples/plotly_example.py
+++ b/examples/plotly_example.py
@@ -10,10 +10,7 @@ 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.subplot(212)
     plt.plot(x, y2, 'go', label='cos')
     plt.title("It's a Sign")
     plt.xlabel('time')
@@ -22,7 +19,37 @@ def plot_sin():
 
     # export info from matplotlib fig and render with plotly!
     fig = plt.gcf()
-    plt.show()
+    fig_to_plotly(fig, username, api_key)
+
+
+def two_plots():
+    plt.figure(1)
+    plt.subplot(211)
+    plt.plot([1,2,3],[4,5,6])
+    plt.subplot(212)
+    plt.plot([1,2,3],[3,6,2])
+    plt.title('two subplots')
+
+    fig = plt.gcf()
+    fig_to_plotly(fig, username, api_key)
+
+
+def four_plots():
+    plt.figure(1)
+    plt.subplot(411)
+    plt.plot([1,2,3],[4,5,6], 'k-', label='first')
+    plt.xlabel('x1')
+    plt.subplot(412)
+    plt.plot([1,2,3],[3,6,2], 'b--', label='second')
+    plt.xlabel('x2')
+    plt.subplot(413)
+    plt.plot([10,11,12,13], [1,0,1,0], 'g-.', label='third')
+    plt.xlabel('x3')
+    plt.subplot(414)
+    plt.plot([20,21,22,23], [0,1,0,1], 'r-', label = 'fourth')
+    plt.xlabel('x4')
+    plt.title('four subplots')
+    fig = plt.gcf()
     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 d83af15..485b568 100644
--- a/mplexporter/renderers/plotly/plotly_renderer.py
+++ b/mplexporter/renderers/plotly/plotly_renderer.py
@@ -26,43 +26,79 @@ class PlotlyRenderer(Renderer):
 
     def close_figure(self, fig):
         self.output += "closing figure\n"
+        self.configure_subplots()
+        self.layout['showlegend'] = False
 
     def open_axes(self, ax, properties):
-        self.output += "  opening axes\n"
         self.axis_ct += 1
-        layout = {
-            'title': properties['title'],
-            'xaxis': {
-                'range': properties['xlim'],
-                'title': properties['xlabel'],
-                'showgrid': properties['xgrid']
-            },
-            'yaxis': {
-                'range': properties['ylim'],
-                'title': properties['ylabel'],
-                'showgrid': properties['ygrid'],
+        self.output += "  opening axis {}\n".format(self.axis_ct)
+        if self.axis_ct == 1:
+            layout = {
+                'title': properties['title'],
+                'xaxis': {
+                    'range': properties['xlim'],
+                    'title': properties['xlabel'],
+                    'showgrid': properties['xgrid']
+                },
+                'yaxis': {
+                    'domain': [0,1],
+                    'range': properties['ylim'],
+                    'title': properties['ylabel'],
+                    'showgrid': properties['ygrid'],
+                }
+            }
+        else:
+            layout = {
+                'title': properties['title'],
+                'xaxis{}'.format(self.axis_ct): {
+                    'range': properties['xlim'],
+                    'title': properties['xlabel'],
+                    'showgrid': properties['xgrid']
+                },
+                'yaxis{}'.format(self.axis_ct): {
+                    'domain': [0,1],
+                    '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"
+        self.output += "  closing axis {}\n".format(self.axis_ct)
 
     def draw_line(self, data, coordinates, style):
         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'])
+            if self.axis_ct == 1:
+                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'],
+                        'color': style['color'],
+                        'width': style['linewidth'],
+                        'dash': plotly_utils.convert_dash(style['dasharray'])
+                    }
                 }
-            }
-            self.data += trace,
+                self.data += trace,
+            else:
+                trace = {
+                    'mode': 'lines',
+                    'x': [xy_pair[0] for xy_pair in data],
+                    'y': [xy_pair[1] for xy_pair in data],
+                    'xaxis': 'x{}'.format(self.axis_ct),
+                    'yaxis': 'y{}'.format(self.axis_ct),
+                    'line': {
+                        'opacity': style['alpha'],
+                        'color': style['color'],
+                        '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])
 
@@ -88,6 +124,21 @@ class PlotlyRenderer(Renderer):
         else:
             self.output += "    received {} markers with 'figure' coordinates, skipping!".format(data.shape[0])
 
+    def configure_subplots(self):
+        num_plots = self.axis_ct
+        if num_plots > 1:
+            spacing = 0.3/num_plots # magic numbers! change this!
+            plot_dim = (1 - spacing*(num_plots-1))/num_plots
+            self.layout['yaxis']['domain'] = [1-plot_dim, 1]
+            self.layout['xaxis']['anchor'] = 'y'
+            for subplot_num in range(1, num_plots):
+                domain_end = 1 - (plot_dim + spacing)*subplot_num
+                domain_start = domain_end - plot_dim
+                if domain_start < 0:
+                    domain_start = 0
+                self.layout['yaxis{}'.format(subplot_num + 1)]['domain'] = [domain_start, domain_end]
+                self.layout['xaxis{}'.format(subplot_num + 1)]['anchor'] = 'y{}'.format(subplot_num + 1)
+
 
 def fig_to_plotly(fig, username=None, api_key=None, notebook=False):
     """Convert a matplotlib figure to plotly dictionary

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