Nyaplot Tutorial 1: Getting Started with Nyaplot

This notebook is written to demonstrate Nyaplot. Nyaplot is an interactive plots generator for Ruby users. It can create various types of plots, from basic ones like scatter or line, to more practical ones like 3D charts and Map visualization. This tutorial will introduce the basic use of Nyaplot.

In [1]:
require 'nyaplot'
Out[1]:
Out[1]:
true

Let's make some diagrams with Nyaplot. The code shown below is the minimal code to create bar chart.

In [2]:
plot = Nyaplot::Plot.new
bar = plot.add(:bar, ['Persian', 'Maine Coon', 'American Shorthair'], [10,20,30])
Out[2]:
#<Nyaplot::Diagram:0xb8f3fc60 @properties={:type=>:bar, :options=>{:x=>"data0", :y=>"data1"}, :data=>"229ade56-2c0d-46d7-a378-d356bccdf2c1"}, @xrange=["Persian", "Maine Coon", "American Shorthair"], @yrange=[0, 30]>

That's all. Plot will appear on the notebook using Nyaplot::Plot#show.

In [3]:
plot.show
Out[3]:

That's looks good, but I noticed that x and y labels have no sence. So fix them to be correct name.

In [4]:
plot.x_label("Species")
plot.y_label("Number")
plot.show
Out[4]:

Good. Nyaplot::Plot has various options except x_label and y_label, and you can find them by reading documents and some other notebooks.

Next the color is not so pretty, so let's try to change colors. Nyaplot has various default colorset and it's quite easy to find your favorite one.

In [5]:
colors = Nyaplot::Colors.qual
Out[5]:
rgb(251,180,174)rgb(179,205,227)rgb(204,235,197)rgb(222,203,228)rgb(254,217,166)rgb(255,255,204)rgb(229,216,189)rgb(253,218,236)rgb(242,242,242)
         

Nyaplot::Colors#qual finds random colorset for qualitative data and it may be usable when creating bar chart or scatter. To leran more about Nyaplot::Colors, visit Tutorial3.
Then fill bar charts in the selected color.

In [6]:
bar.color(colors)
plot.show
Out[6]:

The example above, I prepared array and use Nyaplot::Plot.add to add bar chart to the plot. But Nyaplot has more useful API.
Let's make the same plot using Nyaplot::DataFrame.

In [10]:
df = Nyaplot::DataFrame.new({species: ['Persian', 'Maine Coon', 'American Shorthair'], number: [10,20,30]})
Out[10]:
speciesnumber
Persian10
Maine Coon20
American Shorthair30

Use Nyaplot::Plot#add_with_df instead of #add to create plot from DataFrame.

In [11]:
plot2 = Nyaplot::Plot.new
plot2.add_with_df(df, :bar, :species, :number) # x-> column :species, y-> column :number
plot2.show
Out[11]:

DataFrame is useful when you create more complicated plots. Go to Tutorial2.

This tutorial was written by Naoki Nishida, as a part of products in GSoC 2014.