[Python-modules-commits] [python-mplexporter] 22/135: Added option for iplot or plot in plotly_renderer for IPython notebooks.

Wolfgang Borgert debacle at moszumanska.debian.org
Tue Sep 23 21:18:59 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 4a92f54f0053cf06d59f2d5f1247898035802ac4
Author: theengineear <andseier at gmail.com>
Date:   Wed Feb 19 18:54:25 2014 -0800

    Added option for iplot or plot in plotly_renderer for IPython notebooks.
    
    You can now toggle the fig_to_plotly function by adding the 'notebook'
    keyword (i.e., notebook=True) so that the plot will be dropped directly
    into your ipython notebook.
---
 mplexporter/renderers/plotly/plotly_renderer.py |  7 ++-
 notebooks/PlotlyTest.ipynb                      | 57 +++++++++++++------------
 2 files changed, 34 insertions(+), 30 deletions(-)

diff --git a/mplexporter/renderers/plotly/plotly_renderer.py b/mplexporter/renderers/plotly/plotly_renderer.py
index 2282275..5c50f3d 100644
--- a/mplexporter/renderers/plotly/plotly_renderer.py
+++ b/mplexporter/renderers/plotly/plotly_renderer.py
@@ -76,11 +76,14 @@ class PlotlyRenderer(Renderer):
         self.data += data_dict,
 
 
-def fig_to_plotly(fig, username=None, api_key=None):
+def fig_to_plotly(fig, username=None, api_key=None, notebook=False):
     """Convert a matplotlib figure to plotly dictionary
 
     """
     renderer = PlotlyRenderer(username=username, api_key=api_key)
     Exporter(renderer).run(fig)
     py = plotly.plotly(renderer.username, renderer.api_key)
-    py.plot(renderer.data, layout=renderer.layout)
+    if notebook:
+        return py.iplot(renderer.data, layout=renderer.layout)
+    else:
+        py.plot(renderer.data, layout=renderer.layout)
diff --git a/notebooks/PlotlyTest.ipynb b/notebooks/PlotlyTest.ipynb
index 21f215f..4910c2b 100644
--- a/notebooks/PlotlyTest.ipynb
+++ b/notebooks/PlotlyTest.ipynb
@@ -8,22 +8,6 @@
   {
    "cells": [
     {
-     "cell_type": "heading",
-     "level": 2,
-     "metadata": {},
-     "source": [
-      "Re-Creating matplotlib Plots in Plotly"
-     ]
-    },
-    {
-     "cell_type": "heading",
-     "level": 5,
-     "metadata": {},
-     "source": [
-      "Many users are familiar with creating plots from the matplotlib package, the mplexporter package gives users access to an Exporter class that will crawl figure structures created from matplotlib. This exporter can be used in conjunction with one of many renderers to pass the properties and data along to another program. The following is an example using the PlotlyRenderer."
-     ]
-    },
-    {
      "cell_type": "code",
      "collapsed": false,
      "input": [
@@ -35,22 +19,19 @@
      "language": "python",
      "metadata": {},
      "outputs": [],
-     "prompt_number": 1
+     "prompt_number": 9
     },
     {
      "cell_type": "code",
      "collapsed": false,
      "input": [
-      "# from mplexporter.tools import ipynb_vega_init\n",
-      "# ipynb_vega_init()\n",
-      "# from mplexporter.tools import ipynb_plotly_init\n",
       "username = 'IPython.Demo'\n",
       "api_key = '1fw3zw2o13'"
      ],
      "language": "python",
      "metadata": {},
      "outputs": [],
-     "prompt_number": 3
+     "prompt_number": 11
     },
     {
      "cell_type": "code",
@@ -65,7 +46,7 @@
       "plt.title(\"It's a Sign\")\n",
       "plt.xlabel('time')\n",
       "plt.ylabel('amplitude')\n",
-      "plt.legend().set_visible(True)\n",
+      "# plt.legend().set_visible(True)\n",
       "fig = plt.gcf()"
      ],
      "language": "python",
@@ -74,24 +55,44 @@
       {
        "metadata": {},
        "output_type": "display_data",
-       "png": "iVBORw0KGgoAAAANSUhEUgAAAY0AAAEZCAYAAABrUHmEAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XlcVdX6P/DPYXJERFRiSvTgVRRFzFT0a2CFKCrZpPj1\namZ5rcyhur+rgYbD1TSrbw5lZo5lZnkdGcwsQUsRpyuOqCQKqDihOCOc9ftjCzKcI4dz9rD23s/7\n9Tov4Zx99n72Wou93Huv9WwDY4yBEEIIsYKD0gEQQghRD+o0CCGEWI06DUIIIVajToMQQojVqNMg\nhBBiNeo0CCGEWI06DUI4snPnTrRu3VrpMAixiDoNoiv+/v74/fffAQDLly9Hjx49Knw+fPhwrFix\nQtIYjh49il69esHDwwPu7u7o1KkTkpOTAQA9evTAiRMnJN0+IfZwUjoAQuRkMBjs+lwM/fv3x+jR\no5GUlATGG [...]
+       "png": "iVBORw0KGgoAAAANSUhEUgAAAY0AAAEZCAYAAABrUHmEAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XtcFdXaB/DfRvCOgKiEwAndeBRFETMVfRW0EEQlu6m9\nnaOmmaV5qc57NNC8lL6a1ZuXjpnlpZOV5fHKxcwS9KSItyNeUUgNUFATRVFDYb1/DCCXvdnD3nNZ\nM/N8P5/94bJnzzyz1mIWM7PWMybGGAMhhBAigpPaARBCCNEO6jQIIYSIRp0GIYQQ0ajTIIQQIhp1\nGoQQQkSjToMQQoho1GkQwpG9e/eiQ4cOaodBiFXUaRBD8ff3x88//wwAWLt2Lfr27Vvl/TFjxmDd\nunWyxnDy5EkMHDgQnp6e8PDwQPfu3ZGUlAQA6Nu3L86cOSPr9glxhLPaARCiJJPJ5ND7Uhg6dCgm\nTZqExMREM [...]
        "text": [
-        "<matplotlib.figure.Figure at 0x106ff2dd0>"
+        "<matplotlib.figure.Figure at 0x10f855c90>"
        ]
       }
      ],
-     "prompt_number": 8
+     "prompt_number": 12
     },
     {
      "cell_type": "code",
      "collapsed": false,
      "input": [
-      "fig_to_plotly(fig, username, api_key)"
+      "fig_to_plotly(fig, username, api_key, notebook=True)"
      ],
      "language": "python",
      "metadata": {},
-     "outputs": [],
-     "prompt_number": 9
+     "outputs": [
+      {
+       "html": [
+        "<iframe height=\"500\" id=\"igraph\" scrolling=\"no\" seamless=\"seamless\" src=\"https://plot.ly/~IPython.Demo/1190/600/450\" width=\"650\"></iframe>"
+       ],
+       "metadata": {},
+       "output_type": "pyout",
+       "prompt_number": 13,
+       "text": [
+        "<IPython.core.display.HTML at 0x10f86c1d0>"
+       ]
+      }
+     ],
+     "prompt_number": 13
+    },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [],
+     "language": "python",
+     "metadata": {},
+     "outputs": []
     }
    ],
    "metadata": {}

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