Display multiple graphs in iRuby

64 Views Asked by At

I'm trying to display multiple graphs, and each graph have two subplots, with horizontal layout, in one output in iRuby, running on google Colaboratory.

I first used Nyaplot, but it doesn't work. I don't know why, but it simply gave me no response.

This is my source to check it works:

require 'nyaplot'

plot = Nyaplot::Plot.new
plot.add(:bar, ['Persian', 'Maine Coon', 'American Shorthair'], [10,20,30])
plot.x_label("Species")
plot.y_label("Number")
p = Nyaplot::Frame.new
p.add(plot)
p.show

and this is output :

#<CZTop::Socket::PUB:0x5609e3406f80 last_endpoint="tcp://127.0.0.1:42375">

and nothing more.

So I changed to Matplotlib :: Pyplot, and it worked fine. (Except it was too slow when I try to use subplot.) But when I tried to display multiple graph in one cell, every output was merged, so I can't use it.

Tested this example code:

def a
    plt = Matplotlib::pyplot

    xs = [*1..100]

    ys = xs.map {|x| Math.sin(Math::PI * x / 20.0) + 0.1 * (rand - 0.5) }
    ys2 = xs.map {|x| Math.cos(Math::PI * x / 20.0) + 0.1 * (rand - 0.5) }

    plt.subplot(2,1,1)
    plt.plot(xs, ys, "r", label: "Hello")
    plt.title("a")
    plt.plot(xs, ys2, "b", label: "GoodBye")
    plt.legend
    plt.subplot(2,1,2)
    plt.plot(ys, xs, "r", label: "hello?")
    plt.plot(ys2, xs, "b", label: "goodbye?")
    plt.title("b")
    plt.legend

    plt.show
end

a()
a()
a()

and got this image

So I changed to daru-view, but it can't display multiple graph in one output, with simply call show_in_iruby more than one time. Additionally, I need something like subplot in matplotlib, but I cannot find some with horizontal layout.

with this code:


Daru::View.plotting_library = :googlecharts
idx = Daru::Index.new ['a', 'b', 'c']
df_1 = Daru::DataFrame.new([[1,2,3], [1,2,3], [1,3,5]])
df_1.vectors = idx
idx2 = Daru::Index.new ['a', 'b', 'c']
df_2 = Daru::DataFrame.new([[1,2,3], [1,2,3], [1,2,3]])
df_2.vectors = idx2

options = {title: "name", colors: ["red", "blue"]}
plt1 = Daru::View::Plot.new(df_1, options)
plt2 = Daru::View::Plot.new(df_2, options)
plt1.show_in_iruby
plt2.show_in_iruby

got image like this

What should I do to display multiple graph in one output?

0

There are 0 best solutions below