Uploaded image for project: 'Zeppelin'
  1. Zeppelin
  2. ZEPPELIN-1318

Add support for matplotlib displaying png images in python interpreter

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 0.6.1, 0.7.0
    • Fix Version/s: 0.7.0
    • Component/s: python-interpreter
    • Labels:
      None

      Description

      The current implementation of z.show() supports matplotlib plotting svg images inline. However with some type of plots in matplotlib, particularly for maps via basemap, this causes the UI in the notebook to severely lag when the output isn't hidden. Saving the images in .png format solves this problem, but a different implementation is required in order to plot the image inline. I have proposed a solution below where the image is saved into a byte array and then displayed directly via html:

      import cStringIO
      import base64
      def show(p):
          img = cStringIO.StringIO()
          p.savefig(img, format='png')
          imgStr = "data:image/png;base64,"
          imgStr += base64.b64encode(img.getvalue().strip())
          print "%html <img src='" + imgStr + "'>" 
      

      I will incorporate this into zeppelin's z.show() function.

        Issue Links

          Activity

          Hide
          githubbot ASF GitHub Bot added a comment -

          GitHub user agoodm opened a pull request:

          https://github.com/apache/zeppelin/pull/1329

          ZEPPELIN-1318 - Add support for png images in z.show()

              1. What is this PR for?
                This PR adds support for plotting png images using the matplotlib helper function within a python interpreter (eg `z.show()`). The primary motivation for this is due to the overhead incurred from svg images, which can lag the notebooks if multiple, complicated images are generated (for example, multiple filled contour plots). png images are more lightweight, but of course come at a cost of image quality due to them being raster rather than vector like svg. The support for png images is incorporated through the use of a new optional argument to `z.show` called `fmt` which can be one of `'svg'` or `'png'`. The same code that is currently used in show is used for svg images while the code for png images relies on converting the image directly to a byte array and then entering the decoded byte string directly into an HTML image tag. Currently `fmt` defaults to `'png'` but I think we should consider discussing the pros and cons of each option in this PR.
              1. What type of PR is it?
                Improvement
              1. What is the Jira issue?
                ZEPPELIN-1318(https://issues.apache.org/jira/browse/ZEPPELIN-1318)
              1. How should this be tested?
                In a notebook cell, enter:
                ```python
                %python
                import matplotlib.pyplot as plt
                import numpy as np

          plt.figure()
          plt.plot(np.arange(10))
          z.show(plt, fmt=fmt)
          ```
          Where `fmt` may be one of `'svg'` or `'png'`, and any other input should result in a `ValueError`. I would also recommend testing the example in the screenshot below.

              1. Screenshots (if appropriate)
                ![zeppelin plot](https://puu.sh/qzc4t/b8fcfe856e.png)
              1. Questions:
          • Does the licenses files need update? No
          • Is there breaking changes for older versions? No
          • Does this needs documentation? Yes (if the changes to the `help()` docstring are not sufficient)

          You can merge this pull request into a Git repository by running:

          $ git pull https://github.com/agoodm/zeppelin ZEPPELIN-1318

          Alternatively you can review and apply these changes as the patch at:

          https://github.com/apache/zeppelin/pull/1329.patch

          To close this pull request, make a commit to your master/trunk branch
          with (at least) the following in the commit message:

          This closes #1329


          commit 1efa0c975e41aa3c8b95d0f5e084619d8e996582
          Author: Alex Goodman <agoodm@users.noreply.github.com>
          Date: 2016-08-12T21:31:40Z

          ZEPPELIN-1318 - Add support for png images in z.show()


          Show
          githubbot ASF GitHub Bot added a comment - GitHub user agoodm opened a pull request: https://github.com/apache/zeppelin/pull/1329 ZEPPELIN-1318 - Add support for png images in z.show() What is this PR for? This PR adds support for plotting png images using the matplotlib helper function within a python interpreter (eg `z.show()`). The primary motivation for this is due to the overhead incurred from svg images, which can lag the notebooks if multiple, complicated images are generated (for example, multiple filled contour plots). png images are more lightweight, but of course come at a cost of image quality due to them being raster rather than vector like svg. The support for png images is incorporated through the use of a new optional argument to `z.show` called `fmt` which can be one of `'svg'` or `'png'`. The same code that is currently used in show is used for svg images while the code for png images relies on converting the image directly to a byte array and then entering the decoded byte string directly into an HTML image tag. Currently `fmt` defaults to `'png'` but I think we should consider discussing the pros and cons of each option in this PR. What type of PR is it? Improvement What is the Jira issue? ZEPPELIN-1318 ( https://issues.apache.org/jira/browse/ZEPPELIN-1318 ) How should this be tested? In a notebook cell, enter: ```python %python import matplotlib.pyplot as plt import numpy as np plt.figure() plt.plot(np.arange(10)) z.show(plt, fmt=fmt) ``` Where `fmt` may be one of `'svg'` or `'png'`, and any other input should result in a `ValueError`. I would also recommend testing the example in the screenshot below. Screenshots (if appropriate) ! [zeppelin plot] ( https://puu.sh/qzc4t/b8fcfe856e.png ) Questions: Does the licenses files need update? No Is there breaking changes for older versions? No Does this needs documentation? Yes (if the changes to the `help()` docstring are not sufficient) You can merge this pull request into a Git repository by running: $ git pull https://github.com/agoodm/zeppelin ZEPPELIN-1318 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/zeppelin/pull/1329.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #1329 commit 1efa0c975e41aa3c8b95d0f5e084619d8e996582 Author: Alex Goodman <agoodm@users.noreply.github.com> Date: 2016-08-12T21:31:40Z ZEPPELIN-1318 - Add support for png images in z.show()
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user felixcheung commented on the issue:

          https://github.com/apache/zeppelin/pull/1329

          could you please update this PR and the JIRA to say `python` interpreter?

          Show
          githubbot ASF GitHub Bot added a comment - Github user felixcheung commented on the issue: https://github.com/apache/zeppelin/pull/1329 could you please update this PR and the JIRA to say `python` interpreter?
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user agoodm commented on the issue:

          https://github.com/apache/zeppelin/pull/1329

          @felixcheung Done.

          Show
          githubbot ASF GitHub Bot added a comment - Github user agoodm commented on the issue: https://github.com/apache/zeppelin/pull/1329 @felixcheung Done.
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user bzz commented on the issue:

          https://github.com/apache/zeppelin/pull/1329

          @agoodm thank you for improving matplotlib integration!

          Please correct me, but right now PR does 2 things:

          • add PNG support, as well as SVG
          • switches default to PNG

          I see you point on performance, but do you think changing default can bring any compatibility issue for existing notebooks?

          Other then that - the changes looks great to me!

          Show
          githubbot ASF GitHub Bot added a comment - Github user bzz commented on the issue: https://github.com/apache/zeppelin/pull/1329 @agoodm thank you for improving matplotlib integration! Please correct me, but right now PR does 2 things: add PNG support, as well as SVG switches default to PNG I see you point on performance, but do you think changing default can bring any compatibility issue for existing notebooks? Other then that - the changes looks great to me!
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user agoodm commented on the issue:

          https://github.com/apache/zeppelin/pull/1329

          @bzz The way I see it the pros and cons of each format can be summed up as follows:

              1. SVG
                Pros:
          • Vector so image quality will be consistent regardless of resolution
          • Displays more quickly for simple plots (eg. single line plots)

          Cons:

          • Very slow for complex plots involving many data points
              1. PNG
                Pros:
          • Lightweight regardless of the complexity of the plot

          Cons:

          • Does not scale to higher resolutions as well due to being raster
          • Slightly slower to display simple plots

          To be honest, I am not really sure which option would be best to default to. png is the default format in most matplotlib backends so that is why I have chosen it for now, but I think it's worth thinking about. Either way though I don't think the choice of default format will matter in terms of backwards compatibility as the change should be seamless for users. I am fine with either option honestly.

          Show
          githubbot ASF GitHub Bot added a comment - Github user agoodm commented on the issue: https://github.com/apache/zeppelin/pull/1329 @bzz The way I see it the pros and cons of each format can be summed up as follows: SVG Pros: Vector so image quality will be consistent regardless of resolution Displays more quickly for simple plots (eg. single line plots) Cons: Very slow for complex plots involving many data points PNG Pros: Lightweight regardless of the complexity of the plot Cons: Does not scale to higher resolutions as well due to being raster Slightly slower to display simple plots To be honest, I am not really sure which option would be best to default to. png is the default format in most matplotlib backends so that is why I have chosen it for now, but I think it's worth thinking about. Either way though I don't think the choice of default format will matter in terms of backwards compatibility as the change should be seamless for users. I am fine with either option honestly.
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user bzz commented on the issue:

          https://github.com/apache/zeppelin/pull/1329

          Thank you for prompt and thoughtful comparison @agoodm !

          > the choice of default format will matter in terms of backwards compatibility as the change should be seamless for users

          This sounds great to me, let's keep PNG as default.

          Thank you for taking care of the docs in `help()` as well, could you also add it to docs/interpreter/python.md#matplotlib-integration(https://github.com/apache/zeppelin/blob/master/docs/interpreter/python.md#matplotlib-integration) so it will show up on the [doc website](http://zeppelin.apache.org/docs/0.7.0-SNAPSHOT/interpreter/python.html#matplotlib-integration)?

          Here you can find more details on how to [update the website](http://zeppelin.apache.org/docs/0.7.0-SNAPSHOT/development/howtocontributewebsite.html).

          Show
          githubbot ASF GitHub Bot added a comment - Github user bzz commented on the issue: https://github.com/apache/zeppelin/pull/1329 Thank you for prompt and thoughtful comparison @agoodm ! > the choice of default format will matter in terms of backwards compatibility as the change should be seamless for users This sounds great to me, let's keep PNG as default. Thank you for taking care of the docs in `help()` as well, could you also add it to docs/interpreter/python.md#matplotlib-integration ( https://github.com/apache/zeppelin/blob/master/docs/interpreter/python.md#matplotlib-integration ) so it will show up on the [doc website] ( http://zeppelin.apache.org/docs/0.7.0-SNAPSHOT/interpreter/python.html#matplotlib-integration)? Here you can find more details on how to [update the website] ( http://zeppelin.apache.org/docs/0.7.0-SNAPSHOT/development/howtocontributewebsite.html ).
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user agoodm commented on the issue:

          https://github.com/apache/zeppelin/pull/1329

          @bzz Done!

          Show
          githubbot ASF GitHub Bot added a comment - Github user agoodm commented on the issue: https://github.com/apache/zeppelin/pull/1329 @bzz Done!
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user bzz commented on the issue:

          https://github.com/apache/zeppelin/pull/1329

          Looks great to me, thank you @agoodm!

          Will merge to master later today, if there is no further discussion

          Show
          githubbot ASF GitHub Bot added a comment - Github user bzz commented on the issue: https://github.com/apache/zeppelin/pull/1329 Looks great to me, thank you @agoodm! Will merge to master later today, if there is no further discussion
          Hide
          githubbot ASF GitHub Bot added a comment -

          Github user asfgit closed the pull request at:

          https://github.com/apache/zeppelin/pull/1329

          Show
          githubbot ASF GitHub Bot added a comment - Github user asfgit closed the pull request at: https://github.com/apache/zeppelin/pull/1329

            People

            • Assignee:
              agoodman Alex Goodman
              Reporter:
              agoodman Alex Goodman
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development