module Matplotlib::IRuby
Constants
- AGG_FORMATS
- BACKEND_GUI_MAP
- GUI_BACKEND_MAP
Public Class Methods
NOTE: This method is translated from `IPython.core.activate_matplotlib` function.
# File lib/matplotlib/iruby.rb, line 110 def activate(gui=:inline) enable_matplotlib(gui) end
# File lib/matplotlib/iruby.rb, line 133 def available_gui_names GUI_BACKEND_MAP.keys end
Private Class Methods
Activate the given backend and set interactive to true. This method is based on IPython.core.pylabtools.activate_matplotlib function.
@param [String, Symbol] backend a name of matplotlib backend
# File lib/matplotlib/iruby.rb, line 193 def activate_matplotlib(backend) require 'matplotlib' Matplotlib.interactive(true) backend = backend.to_s Matplotlib.rcParams['backend'] = backend require 'matplotlib/pyplot' Matplotlib::Pyplot.switch_backend(backend) # TODO: should support wrapping python function # plt = Matplotlib::Pyplot # plt.__pyobj__.show._needmain = false # plt.__pyobj__.draw_if_interactive = flag_calls(plt.__pyobj__.draw_if_interactive) end
This method is based on IPython.core.pylabtools.configure_inline_support function.
@param shell an instance of IRuby
shell @param backend a name of matplotlib backend
# File lib/matplotlib/iruby.rb, line 213 def configure_inline_support(backend) # Temporally monky-patching IRuby kernel to enable flushing and closing figures. # TODO: Make this feature a pull-request for sciruby/iruby. kernel = ::IRuby::Kernel.instance kernel.extend HookExtension unless kernel.respond_to?(:events) if backend == GUI_BACKEND_MAP[:inline] if kernel.respond_to?(:register_event) kernel.register_event(:post_execute, method(:flush_figures)) else @post_execute_func = kernel.events.register(:post_execute, &method(:flush_figures)) end # TODO: save original rcParams and overwrite rcParams with IRuby-specific configuration new_backend_name = :inline else if kernel.respond_to?(:unregister_event) kernel.unregister_event(:post_execute, method(:flush_figures)) elsif @post_execute_func kernel.events.unregister(:post_execute, @post_execute_func) @post_execute_func = nil end # TODO: restore saved original rcParams new_backend_name = :not_inline end if new_backend_name != @current_backend # TODO: select figure formats @current_backend = new_backend_name end end
This method is based on IPython.core.interactiveshell.InteractiveShell.enable_matplotlib function.
# File lib/matplotlib/iruby.rb, line 140 def enable_matplotlib(gui=nil) gui, backend = find_gui_and_backend(gui, @gui_select) if gui != :inline if @gui_select.nil? @gui_select = gui elsif gui != @gui_select $stderr.puts "Warning: Cannot change to a different GUI toolkit: #{gui}. Using #{@gui_select} instead." gui, backend = find_gui_and_backend(@gui_select) end end activate_matplotlib(backend) configure_inline_support(backend) # self.enable_gui(gui) # register matplotlib-aware execution runner for ExecutionMagics [gui, backend] end
Given a gui string return the gui and matplotlib backend. This method is based on IPython.core.pylabtools.find_gui_and_backend function.
@param [String, Symbol, nil] gui can be one of (:tk, :gtk, :wx, :qt, :qt4, :inline, :agg). @param [String, Symbol, nil] gui_select can be one of (:tk, :gtk, :wx, :qt, :qt4, :inline, :agg).
@return A pair of (gui, backend) where backend is one of (:TkAgg, :GTKAgg, :WXAgg, :Qt4Agg, :agg).
# File lib/matplotlib/iruby.rb, line 167 def find_gui_and_backend(gui=nil, gui_select=nil) gui = gui.to_sym if gui.kind_of? String if gui && gui != :auto # select backend based on requested gui backend = GUI_BACKEND_MAP[gui] gui = nil if gui == :agg return [gui, backend] end backend = Matplotlib.rcParamsOrig['backend']&.to_sym gui = BACKEND_GUI_MAP[backend] # If we have already had a gui active, we need it and inline are the ones allowed. if gui_select && gui != gui_select gui = gui_select backend = backend[gui] end [gui, backend] end
This method is based on ipykernel.pylab.backend_inline.flush_figures function.
# File lib/matplotlib/iruby.rb, line 245 def flush_figures # TODO: I want to allow users to turn on/off automatic figure closing. show_figures(true) end
This method is based on ipykernel.pylab.backend_inline.show function.
@param [true, false] close If true, a `plt.close('all')` call is automatically issued after sending all the figures.
# File lib/matplotlib/iruby.rb, line 253 def show_figures(close=false) _pylab_helpers = PyCall.import_module('matplotlib._pylab_helpers') gcf = _pylab_helpers.Gcf kernel = ::IRuby::Kernel.instance gcf.get_all_fig_managers.each do |fig_manager| data = ::IRuby::Display.display(fig_manager.canvas.figure) kernel.session.send(:publish, :display_data, data: data, metadata: {}) end ensure unless gcf.get_all_fig_managers.nil? Matplotlib::Pyplot.close('all') end end