mrkn/matplotlib.rb

How to use in IRuby notebook

Shekharrajak opened this issue · 9 comments

Hello,

I want to use this gem in IRuby notebook. IRuby notebook is vey useful when we want to check/try our small code and display output. So If we can use this gem in IRuby notebook to plot charts, then it will be very useful for Ruby developers.

Can we use matplotlib gem in IRuby to display charts ?

Ping @mrkn .

After require 'matplotlib/iruby',when I run this Matplotlib::IRuby.activate I get error message :

PyCall::PyError: pytype(ImportError): ImportError('No module named ruby.matplotlib.backend_inline',)

I tried it from Readme : https://github.com/mrkn/matplotlib.rb#iruby-integration But not able to run Matplotlib::IRuby.activate . Let me know if I have to setup something.

mrkn commented

Could you please describe the complete steps to reproduce this issue?

I created new folder and Gemfile inside the folder:

Gemfile contains :

source 'https://rubygems.org'

# Added at 2017-04-05 10:18:20 +0530 by shekharrajak:
gem 'iruby'
gem 'rbczmq'

gem 'matplotlib', git: 'https://github.com/mrkn/matplotlib.rb.git'

run bundle install to install dependencies.
Then I run iruby notebook .

require 'matplotlib/iruby' returns true but Matplotlib::IRuby.activate returns PyCall::PyError: pytype(ImportError): ImportError('No module named ruby.matplotlib.backend_inline',)

Is it possible to use the chart image directly in web application (Like rails), without saving the image file ?

mrkn commented

Please tell me the version of Python you use.

Is it possible to use the chart image directly in web application (Like rails), without saving the image file ?

First, you can generate a chart image without saving it as a file by passing a cStringIO.StringIO object to pyplot.savefig method instead of a filename.

Then, you can get thw byte sequence of the chart image from that StringIO object. Note that you have to rewind the StringIO before reading.

Finally, you can send the bytes of the chart image back as a response body.

Also getting this with the following stack trace:

/anaconda/envs/q8/lib/python2.7/site-packages/matplotlib/__init__.py:1350: UserWarning:  This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

  warnings.warn(_use_error_msg)
PyCall::PyError: <type 'exceptions.ImportError'>: No module named ruby.matplotlib.backend_inline
  File "/anaconda/envs/q8/lib/python2.7/site-packages/matplotlib/pyplot.py", line 224, in switch_backend
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "anaconda/envs/q8/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
mrkn commented

@baarkerlounger Please open a new issue with the code to reproduce your issue.

mrkn commented

@Shekharrajak I closed this issue. Please open a new issue if you still have problems.