[Python-modules-commits] [python-mplexporter] 42/135: Better test function and nicer looking example for subplots.

Wolfgang Borgert debacle at moszumanska.debian.org
Tue Sep 23 21:19:02 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 6de31e104755dfe00e77fc4ee8aa35dd72909b8b
Author: theengineear <andseier at gmail.com>
Date:   Mon Feb 24 22:45:41 2014 -0800

    Better test function and nicer looking example for subplots.
    
    The test function in the 'tests' dir now uses a custom function for
    comparing two dictionaries which outputs the exact dictionary entry upon
    a comparison failure. This is used as a message for the assertions so
    that errors can be found quickly.
    
    Example for subplots now uses matplotlib.gridspec and includes extra
    'wspace' and 'hspace' so that the resulting plot ticks can be clearly
    seen. It shows how complicated subplot structures can be ported to
    Plotly.
---
 examples/plotly_example.py       |  29 +++---
 mplexporter/tests/test_plotly.py | 186 +++++++++++++++++++++++++++++++--------
 2 files changed, 165 insertions(+), 50 deletions(-)

diff --git a/examples/plotly_example.py b/examples/plotly_example.py
index d077bbe..372712d 100644
--- a/examples/plotly_example.py
+++ b/examples/plotly_example.py
@@ -1,6 +1,7 @@
 from mplexporter.renderers import PlotlyRenderer, fig_to_plotly
 import numpy as np
 import matplotlib.pyplot as plt
+import matplotlib.gridspec as gridspec
 username = 'IPython.Demo'
 api_key = '1fw3zw2o13'
 
@@ -35,21 +36,19 @@ def two_plots():
 
 
 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 = plt.figure() # matplotlib.figure.Figure obj
+    gs = gridspec.GridSpec(3, 3)
+    ax1 = fig.add_subplot(gs[0,:])
+    ax1.plot([1,2,3,4,5], [10,5,10,5,10], 'r-')
+    ax2 = fig.add_subplot(gs[1,:-1])
+    ax2.plot([1,2,3,4], [1,4,9,16], 'k-')
+    ax3 = fig.add_subplot(gs[1:, 2])
+    ax3.plot([1,2,3,4], [1,10,100,1000], 'b-')
+    ax4 = fig.add_subplot(gs[2,0])
+    ax4.plot([1,2,3,4], [0,0,1,1], 'g-')
+    ax5 = fig.add_subplot(gs[2,1])
+    ax5.plot([1,2,3,4], [1,0,0,1], 'c-')
+    gs.update(hspace=0.5, wspace=0.5)
     fig_to_plotly(fig, username, api_key)
 
 if __name__ == '__main__':
diff --git a/mplexporter/tests/test_plotly.py b/mplexporter/tests/test_plotly.py
index b447a65..790db1c 100644
--- a/mplexporter/tests/test_plotly.py
+++ b/mplexporter/tests/test_plotly.py
@@ -4,6 +4,8 @@ from ..renderers import PlotlyRenderer
 import matplotlib
 matplotlib.use('Agg')
 import matplotlib.pyplot as plt
+import matplotlib.gridspec as gridspec
+import numbers
 
 
 def test_simple_line():
@@ -14,50 +16,164 @@ def test_simple_line():
     renderer = PlotlyRenderer()
     exporter = Exporter(renderer)
     exporter.run(fig)
-    assert SIMPLE_LINE['data'] == renderer.data
-    assert SIMPLE_LINE['layout'] == renderer.layout
+    for data_no, data_dict in enumerate(renderer.data):
+        equivalent, msg = compare_dict(data_dict, SIMPLE_LINE['data'][data_no])
+        assert equivalent, msg
+    equivalent, msg = compare_dict(renderer.layout, SIMPLE_LINE['layout'])
+    assert equivalent, msg
+
+
+def test_subplots():
+    fig = plt.figure() # matplotlib.figure.Figure obj
+    gs = gridspec.GridSpec(3, 3)
+    ax1 = fig.add_subplot(gs[0,:])
+    ax1.plot([1,2,3,4,5], [10,5,10,5,10], 'r-')
+    ax2 = fig.add_subplot(gs[1,:-1])
+    ax2.plot([1,2,3,4], [1,4,9,16], 'k-')
+    ax3 = fig.add_subplot(gs[1:, 2])
+    ax3.plot([1,2,3,4], [1,10,100,1000], 'b-')
+    ax4 = fig.add_subplot(gs[2,0])
+    ax4.plot([1,2,3,4], [0,0,1,1], 'g-')
+    ax5 = fig.add_subplot(gs[2,1])
+    ax5.plot([1,2,3,4], [1,0,0,1], 'c-')
+
+    renderer = PlotlyRenderer()
+    exporter = Exporter(renderer)
+    exporter.run(fig)
+    for data_no, data_dict in enumerate(renderer.data):
+        equivalent, msg = compare_dict(data_dict, SUBPLOTS['data'][data_no])
+        assert equivalent, msg
+    equivalent, msg = compare_dict(renderer.layout, SUBPLOTS['layout'])
+    assert equivalent, msg
+
+
+def compare_dict(dict1, dict2, equivalent=True, msg='', tol_digits=10):
+    for key in dict1:
+        if key not in dict2:
+            return False, "{} not {}".format(dict1.keys(), dict2.keys())
+    for key in dict1:
+        if isinstance(dict1[key], dict):
+            equivalent, msg = compare_dict(dict1[key], dict2[key], tol_digits=tol_digits)
+        else:
+            if not (dict1[key] == dict2[key]):
+                return False, "['{}'] = {} not {}".format(key, dict1[key], dict2[key])
+        if not equivalent:
+            return False, "['{}']".format(key) + msg
+    return equivalent, msg
 
 
 ## dictionaries for tests
 
 SIMPLE_LINE = {
-    'data': [
-        {'line': {'color': '#000000',
-                  'dash': 'solid',
-                  'opacity': 1,
-                  'width': 1.0
-                  },
-         'mode': 'lines',
-         'x': [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
-         'xaxis': 'x',
-         'y': [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
-         'yaxis': 'y'
-         },
-        {'marker': {'color': '#000000',
-                    'line': {'color': '#000000',
-                             'width': 0.5
-                             },
-                    'opacity': 1,
-                    'symbol': 'dot'
-                    },
-         'mode': 'markers',
-         'x': [0.0, 1.0, 2.0, 3.0, 4.0],
-         'xaxis': 'x',
-         'y': [0.0, 1.0, 2.0, 3.0, 4.0],
-         'yaxis': 'y'}
-    ],
+    'data': [{'line': {'color': '#000000', 'dash': 'solid', 'opacity': 1, 'width': 1.0},
+              'mode': 'lines',
+              'x': [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
+              'xaxis': 'x',
+              'y': [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
+              'yaxis': 'y'},
+             {'marker': {'color': '#000000', 'line': {'color': '#000000', 'width': 0.5},
+                         'opacity': 1,
+                         'symbol': 'dot'},
+              'mode': 'markers',
+              'x': [0.0, 1.0, 2.0, 3.0, 4.0],
+              'xaxis': 'x',
+              'y': [0.0, 1.0, 2.0, 3.0, 4.0],
+              'yaxis': 'y'}],
     'layout': {'height': 480,
                'showlegend': False,
                'title': '',
                'width': 640,
-               'xaxis': {'range': (0.0, 9.0),
+               'xaxis': {'domain': [0.125, 0.90000000000000002],
+                         'range': (0.0, 9.0),
                          'showgrid': False,
-                         'title': ''
-               },
-               'yaxis': {'domain': [0, 1],
+                         'title': '',
+                         'anchor': 'y'},
+               'yaxis': {'domain': [0.099999999999999978, 0.90000000000000002],
                          'range': (0.0, 9.0),
                          'showgrid': False,
-                         'title': ''
-               }
-    }
-}
\ No newline at end of file
+                         'title': '',
+                         'anchor': 'x'}}
+}
+
+SUBPLOTS = {'data': [{'line': {'color': '#FF0000', 'dash': 'solid', 'opacity': 1, 'width': 1.0},
+                      'mode': 'lines',
+                      'x': [1.0, 2.0, 3.0, 4.0, 5.0],
+                      'y': [10.0, 5.0, 10.0, 5.0, 10.0]},
+                     {'line': {'color': '#000000', 'dash': 'solid', 'opacity': 1, 'width': 1.0},
+                      'mode': 'lines',
+                      'x': [1.0, 2.0, 3.0, 4.0],
+                      'xaxis': 'x2',
+                      'y': [1.0, 4.0, 9.0, 16.0],
+                      'yaxis': 'y2'},
+                     {'line': {'color': '#0000FF', 'dash': 'solid', 'opacity': 1, 'width': 1.0},
+                      'mode': 'lines',
+                      'x': [1.0, 2.0, 3.0, 4.0],
+                      'xaxis': 'x3',
+                      'y': [1.0, 10.0, 100.0, 1000.0],
+                      'yaxis': 'y3'},
+                     {'line': {'color': '#007F00', 'dash': 'solid', 'opacity': 1, 'width': 1.0},
+                      'mode': 'lines',
+                      'x': [1.0, 2.0, 3.0, 4.0],
+                      'xaxis': 'x4',
+                      'y': [0.0, 0.0, 1.0, 1.0],
+                      'yaxis': 'y4'},
+                     {'line': {'color': '#00BFBF', 'dash': 'solid', 'opacity': 1, 'width': 1.0},
+                      'mode': 'lines',
+                      'x': [1.0, 2.0, 3.0, 4.0],
+                      'xaxis': 'x5',
+                      'y': [1.0, 0.0, 0.0, 1.0],
+                      'yaxis': 'y5'}],
+            'layout': {'height': 480,
+                     'showlegend': False,
+                     'title': '',
+                     'width': 640,
+                     'xaxis': {'anchor': 'y',
+                      'domain': [0.125, 0.90000000000000013],
+                      'range': (1.0, 5.0),
+                      'showgrid': False,
+                      'title': ''},
+                     'xaxis2': {'anchor': 'y2',
+                      'domain': [0.125, 0.62647058823529411],
+                      'range': (1.0, 4.0),
+                      'showgrid': False,
+                      'title': ''},
+                     'xaxis3': {'anchor': 'y3',
+                      'domain': [0.67205882352941182, 0.90000000000000013],
+                      'range': (1.0, 4.0),
+                      'showgrid': False,
+                      'title': ''},
+                     'xaxis4': {'anchor': 'y4',
+                      'domain': [0.125, 0.35294117647058826],
+                      'range': (1.0, 4.0),
+                      'showgrid': False,
+                      'title': ''},
+                     'xaxis5': {'anchor': 'y5',
+                      'domain': [0.39852941176470591, 0.62647058823529411],
+                      'range': (1.0, 4.0),
+                      'showgrid': False,
+                      'title': ''},
+                     'yaxis': {'anchor': 'x',
+                      'domain': [0.66470588235294115, 0.90000000000000002],
+                      'range': (5.0, 10.0),
+                      'showgrid': False,
+                      'title': ''},
+                     'yaxis2': {'anchor': 'x2',
+                      'domain': [0.38235294117647056, 0.61764705882352944],
+                      'range': (0.0, 16.0),
+                      'showgrid': False,
+                      'title': ''},
+                     'yaxis3': {'anchor': 'x3',
+                      'domain': [0.099999999999999867, 0.61764705882352944],
+                      'range': (0.0, 1000.0),
+                      'showgrid': False,
+                      'title': ''},
+                     'yaxis4': {'anchor': 'x4',
+                      'domain': [0.099999999999999867, 0.33529411764705874],
+                      'range': (0.0, 1.0),
+                      'showgrid': False,
+                      'title': ''},
+                     'yaxis5': {'anchor': 'x5',
+                      'domain': [0.099999999999999867, 0.33529411764705874],
+                      'range': (0.0, 1.0),
+                      'showgrid': False,
+                      'title': ''}}}
\ 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