Nyaplot Tutorial 3: Picking colors suitable for your plots

Introduction

Nyaplot has an interface to various prepared colorsets. All of those colorsets are provided by Colorbrewer that is licensed under Apache License Version 2.0.

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

Use Nyaplot::Colors#lists to show the lists of prepared colorsets.

In [2]:
Nyaplot::Colors.lists
Out[2]:
["Spectral", "RdYlGn", "RdBu", "PiYG", "PRGn", "RdYlBu", "BrBG", "RdGy", "PuOr", "Set2", "Accent", "Set1", "Set3", "Dark2", "Paired", "Pastel2", "Pastel1", "OrRd", "PuBu", "BuPu", "Oranges", "BuGn", "YlOrBr", "YlGn", "Reds", "RdPu", "Greens", "YlGnBu", "Purples", "GnBu", "Greys", "YlOrRd", "PuRd", "Blues", "PuBuGn"]

Try some of those colorsets.

In [3]:
Nyaplot::Colors.Pastel2
Out[3]:
rgb(179,226,205)rgb(253,205,172)rgb(203,213,232)rgb(244,202,228)rgb(230,245,201)rgb(255,242,174)rgb(241,226,204)rgb(204,204,204)
        
In [4]:
Nyaplot::Colors.jet
Out[4]:
rgb(215,48,39)rgb(244,109,67)rgb(253,174,97)rgb(254,224,144)rgb(255,255,191)rgb(224,243,248)rgb(171,217,233)rgb(116,173,209)rgb(69,117,180)
         
In [5]:
Nyaplot::Colors.GnBu
Out[5]:
rgb(247,252,240)rgb(224,243,219)rgb(204,235,197)rgb(168,221,181)rgb(123,204,196)rgb(78,179,211)rgb(43,140,190)rgb(8,104,172)rgb(8,64,129)
         
In [6]:
Nyaplot::Colors.binary
Out[6]:
rgb(255,255,255)rgb(240,240,240)rgb(217,217,217)rgb(189,189,189)rgb(150,150,150)rgb(115,115,115)rgb(82,82,82)rgb(37,37,37)rgb(0,0,0)
         

Nyaplot::Colors#seq return random colors that is suitable for sequential data. Similarly, Nyaplot::Colors#div returns colors for diverging data, and Nyaplot::Colors#qual is for qualitative data.

In [7]:
Nyaplot::Colors.seq
Out[7]:
rgb(247,244,249)rgb(231,225,239)rgb(212,185,218)rgb(201,148,199)rgb(223,101,176)rgb(231,41,138)rgb(206,18,86)rgb(152,0,67)rgb(103,0,31)
         
In [8]:
Nyaplot::Colors.div
Out[8]:
rgb(215,48,39)rgb(244,109,67)rgb(253,174,97)rgb(254,224,139)rgb(255,255,191)rgb(217,239,139)rgb(166,217,106)rgb(102,189,99)rgb(26,152,80)
         
In [9]:
Nyaplot::Colors.qual
Out[9]:
rgb(141,211,199)rgb(255,255,179)rgb(190,186,218)rgb(251,128,114)rgb(128,177,211)rgb(253,180,98)rgb(179,222,105)rgb(252,205,229)rgb(217,217,217)
         

Those methods can have one argument to specify required number of colors.

In [10]:
Nyaplot::Colors.seq(3)
Out[10]:
rgb(231,225,239)rgb(201,148,199)rgb(221,28,119)
   
In [11]:
Nyaplot::Colors.Spectral(11)
Out[11]:
rgb(158,1,66)rgb(213,62,79)rgb(244,109,67)rgb(253,174,97)rgb(254,224,139)rgb(255,255,191)rgb(230,245,152)rgb(171,221,164)rgb(102,194,165)rgb(50,136,189)rgb(94,79,162)
           

Use Nyaplot::Color to try your favorite color (Credits: fatalflaws)

In [12]:
Nyaplot::Color.new(["#DEDEDE", "#ACACAC", "#1F141C", "#4A173D", "#8C547E"])
Out[12]:
#DEDEDE#ACACAC#1F141C#4A173D#8C547E
     

Examples

Then try those colors in some plots.

In [13]:
plot = Nyaplot::Plot.new
bar = plot.add(:bar, ['Persian', 'Maine Coon', 'American Shorthair'], [10,20,30])
plot.show
Out[13]:
In [14]:
colors = Nyaplot::Colors.qual(3)
Out[14]:
rgb(27,158,119)rgb(217,95,2)rgb(117,112,179)
   
In [15]:
bar.color(colors)
plot.show
Out[15]:
In [18]:
x=[]; y=[]; fill=[]
-5.step(5, 1) do |i|
  -5.step(5, 1) do |j|
    x.push(i)
    y.push(j)
    val = Math.sin(Math.sqrt(i*i+j*j))/Math.sqrt(i*i+j*j)
    fill.push((val.nan? ? 0 : val))
  end
end

plot2 = Nyaplot::Plot.new
hm = plot2.add(:heatmap, x, y, fill)
hm.width(1)
hm.height(1)
plot2.legend(true)
plot2.show
Out[18]:
In [39]:
colors = Nyaplot::Colors.GnBu
Out[39]:
rgb(247,252,240)rgb(224,243,219)rgb(204,235,197)rgb(168,221,181)rgb(123,204,196)rgb(78,179,211)rgb(43,140,190)rgb(8,104,172)rgb(8,64,129)
         
In [41]:
hm.color(colors)
plot2.show
Out[41]: