Creating Visualizations with DataFrame

Daru uses nyaplot in the background to generate interactive plots, which can be viewed in your browser.

In this tutorial we'll see how we can create some interesting plots with Daru::DataFrame using the #plot function.


In [1]:
require 'daru'


Out[1]:
true

Scatter Plot

Generating a scatter plot is easy with daru.

The #plot function accepts the following options:

  • :type - Specify the type of plot as a symbol. In this example we set it to :scatter.
  • :x - The Vector to be used as the X axis.
  • :y - The Vector to be used as the Y axis.

To demonstrate:


In [2]:
df = Daru::DataFrame.new({
  a: Array.new(100) {|i| i}, 
  b: 100.times.map{rand}
})
df.plot type: :scatter, x: :a, y: :b


Just specifying the options to plot yields a very simple graph without much customization.

But what if you want to enhance your scatter plot with colors, add tooltips for each point and change the label of the X and Y axes. Also you may be faced with a situation where you want to see two different scatter plots on the same graph, each with a different color.

All this can be done by combining #plot with a block. The #plot method yields the corresponding Nyaplot::Plot and Nyaplot::Diagram objects for the graph, which can be used for many varied customizations. Lets see some examples:


In [3]:
# DataFrame denoting Ice Cream sales of a particular food chain in a city
# according to the maximum recorded temperature in that city. It also lists
# the staff strength present in each city.

df = Daru::DataFrame.new({
  :temperature => [30.4, 23.5, 44.5, 20.3, 34, 24, 31.45, 28.34, 37, 24],
  :sales       => [350, 150, 500, 200, 480, 250, 330, 400, 420, 560],
  :city        => ['Pune', 'Delhi']*5,
  :staff       => [15,20]*5
  })
df


Out[3]:
Daru::DataFrame:23219600 rows: 10 cols: 4
citysalesstafftemperature
0Pune3501530.4
1Delhi1502023.5
2Pune5001544.5
3Delhi2002020.3
4Pune4801534
5Delhi2502024
6Pune3301531.45
7Delhi4002028.34
8Pune4201537
9Delhi5602024

In [4]:
# Generating a scatter plot with tool tips, colours and different shapes.

df.plot(type: :scatter, x: :temperature, y: :sales) do |plot, diagram|
  plot.x_label "Temperature"
  plot.y_label "Sales"
  plot.yrange [100, 600]
  plot.xrange [15, 50]
  diagram.tooltip_contents([:city, :staff])
  diagram.color(Nyaplot::Colors.qual) # set the color scheme for this diagram. See Nyaplot::Colors for more info.
  diagram.fill_by(:city) # Change color of each point WRT to the city that it belongs to.
  diagram.shape_by(:city) # Shape each point WRT to the city that it belongs to.
end

# Move the mouse pointer over the points to see the tool tips.


Bar Graph

Generating a bar graph requires passing :bar into the :type option.


In [5]:
# A Bar Graph denoting the age at which various Indian Kings died.

df = Daru::DataFrame.new({
  name: ['Emperor Asoka', 'Akbar The Great', 'Rana Pratap', 'Shivaji Maharaj', 'Krishnadevaraya'],
  age:  [72,63,57,53,58] 
  }, order: [:name, :age])

df.sort!([:age])


Out[5]:
Daru::DataFrame:22577080 rows: 5 cols: 2
nameage
3Shivaji Maharaj53
2Rana Pratap57
4Krishnadevaraya58
1Akbar The Great63
0Emperor Asoka72

In [6]:
df.plot type: :bar, x: :name, y: :age do |plot, diagram|
  plot.x_label "Name"
  plot.y_label "Age"
  plot.yrange [20,80]
end


It is also possible to simply pass in the :x parameter if you want to the frequency of occurence of each element in a Vector.


In [7]:
a = ['A', 'C', 'G', 'T']
v = 1000.times.map { a.sample }

df = Daru::DataFrame.new({
  a: v
  })
df.plot type: :bar, x: :a do |plot, diagram|
  plot.yrange [0,350]
  plot.y_label "Frequency"
  plot.x_label "Letter"
end


Box Plots

A box plot can be generated of the numerical vectors in the DataFrame by simply passing :box to the :type argument.

To demonstrate, I'll prepare some data using the distribution gem to get a bunch of normally distributed random variables. We'll then plot in a Box plot after creating a DataFrame with the data.


In [8]:
require 'distribution'
rng = Distribution::Normal.rng
Daru.lazy_update = true

arr = []
1000.times {arr.push(rng.call)}

arr1 = arr.map{|val| val/0.8-2}
arr2 = arr.map{|val| val*1.1+0.3}
arr3 = arr.map{|val| val*1.3+0.3}

df = Daru::DataFrame.new({ a: arr, b: arr1, c: arr2, d: arr3 })
df.plot type: :box


Line Graphs

Line graphs can be easily generated by passing :line to the :type option.

For example, lets plot a simple line graph showing the temperature of New York City over a week.


In [9]:
df = Daru::DataFrame.new({
  temperature: [43,53,50,57,59,47],
  day:         [1,2,3,4,5,6]
})

df.plot(type: :line, x: :day, y: :temperature) do |plot, diagram|
  plot.x_label "Day"
  plot.y_label "Temperature"
  plot.yrange [20,60]
  plot.xrange [1,6]
  plot.legend true
  diagram.title "Temperature in NYC"
end


Histogram

Specify :histogram to :type will make a histogram from the data.

Histograms dont need a X axis label (because they show the frequency of elements in each bin) so you need to specify the name of the vector you want to plot by passing its name into the :x option.


In [10]:
v = 1000.times.map { rand }

df = Daru::DataFrame.new({
  a: v
  })

df.plot type: :histogram, x: :a do |plot, diagram|
  plot.yrange [0,150]
  plot.y_label "Frequency"
  plot.x_label "Bins"
end


Multiple Diagrams on the same Plot

Scatter Diagrams on the same Plot

Daru allows you to plot as many columns of your dataframe as you want on the same plot.

This can allow you to plot data from the dataframe onto the same graph and visually compare results from observations. You can individually set the color or point shape of each diagram on the plot.

As a first demostration, lets create a DataFrame of the temperatures of three different cities over the period of a week. Then, we'll plot them all on the same graph by passing options to the plot method which tell it the Vectors that are to be used for each of the diagrams.


In [11]:
df = Daru::DataFrame.new({
  nyc_temp:     [43,53,50,57,59,47],
  chicago_temp: [23,30,35,20,26,38],
  sf_temp:      [60,65,73,67,55,52],
  day:          [1 ,2 ,3 ,4 ,5 , 6]
  })

# As you can see, the options passed denote the x and y axes that are to be used by each diagram.
# You can add as many x any y axes as you want, just make sure the relevant vectors are present
# in your DataFrame!
#
# Heres an explanation of all the options passed:
#
# * type - The type of graph to be drawn. All the diagrams will be of the same type in this case.
# * x1/x2/x3 - The Vector from the DataFrame that is to be treated as the X axis for each of the 
# three diagrams. In this case all of them need the :day Vector.
# * y1/y2/y3 - The Vector from the DataFrame that is to be treated as the Y axis for each of the
# three diagrams. As you can see the 1st diagram will plot nyc_temp, the 2nd chicago_temp and the
# the 3rd sf_temp.
# 
# The values yielded in the block are also slightly different in this case.
# The first argument ('plot') is the same as in all the above examples (Nyaplot::Plot), but the 
# second argument ('diagrams') is now an Array of Nyaplot::Diagram objects. Each of the elements
# in the Array represents the diagrams that you want to plot according to the sorting sequence
# of the options specifying the axes.
df.plot type: :scatter, x1: :day, y1: :nyc_temp, x2: :day, y2: :chicago_temp, x3: :day, y3: :sf_temp do |plot, diagrams|
  nyc     = diagrams[0]
  chicago = diagrams[1]
  sf      = diagrams[2]
    
  nyc.title "Temprature in NYC"
  nyc.color "#00FF00"
  
  chicago.title "Temprature in Chicago"
  chicago.color "#FFFF00"
  
  sf.title "Temprature in SF"
  sf.color "#0000FF"
  
  plot.legend true
  plot.yrange [0,100]
  plot.x_label "Day"
  plot.y_label "Temperature"
end


Scatter and Line Diagram on the same Plot

It is also possible to plot two different kinds of diagrams on the same plot. To show you how this works, I'll plot a scatter graph and a line graph on the same plot.

To elaborate, we'll be plotting the a set of points on a scatter plot alongwith their line of best fit.


In [12]:
df = Daru::DataFrame.new({
  burger: ["Hamburger","Cheeseburger","Quarter Pounder","Quarter Pounder with Cheese","Big Mac","Arch Sandwich Special","Arch Special with Bacon","Crispy Chicken","Fish Fillet","Grilled Chicken","Grilled Chicken Light"],
  fat: [9,13 ,21 ,30 ,31 ,31 ,34 ,25 ,28 ,20 ,5],
  calories: [260,320,420,530,560,550,590,500,560,440,300]
  },
  order: [:burger, :fat, :calories])


Out[12]:
Daru::DataFrame:7179960 rows: 11 cols: 3
burgerfatcalories
0Hamburger9260
1Cheeseburger13320
2Quarter Pounder21420
3Quarter Pounder with Cheese30530
4Big Mac31560
5Arch Sandwich Special31550
6Arch Special with Bacon34590
7Crispy Chicken25500
8Fish Fillet28560
9Grilled Chicken20440
10Grilled Chicken Light5300

We'll now write a small algorithm to compute the slope of the line of best fit by placing the fat content as the X co-ordinates and calories as Y co-ordinates.

The line of best fit will be a line graph of red color and the fat and calorie contents will be plotted as usual using a scatter plot.


In [13]:
# Algorithm for computing the line of best fit

sum_x  = df[:fat].sum
sum2_x = (df[:fat]*df[:fat]).sum 
sum_xy = (df[:fat]*df[:calories]).sum
mean_x = df[:fat].mean
mean_y = df[:calories].mean

slope = (sum_xy - sum_x * mean_y) / (sum2_x - sum_x * mean_x)
yint  = mean_y - slope * mean_x

# Assign the computed Y co-ordinates of the line of best fit to a column 
# in the DataFrame called :y_coords
df[:y_coords] = df[:fat].map {|f| f*slope + yint }

# As you can see the options passed into plot are slightly different this time.
#
# Instead of passing Vector names into :x1, :x2... separately, this time we pass
# the relevant names of the X and Y axes co-ordinates as an Array into the :x and 
# :y options.This is a simpler and easier way to plot multiple diagrams.
# 
# As is demonstrated in the previous example, the first argument yields a Nyaplot::Plot
# object and the second an Array of Nyaplot::Diagram objects. The diagrams are ordered
# according to the types specified in the `:type` option.
df.plot type: [:scatter, :line], x: [:fat, :fat], y: [:calories, :y_coords] do |plot, diagrams|
  plot.x_label "Fat"
  plot.y_label "Calories"
  plot.xrange [0,50]
  
  scatter = diagrams[0]
  line    = diagrams[1]
  
  line.color "#FF0000" #set color of the line to 'red'
  scatter.tooltip_contents [:burger] # set tool tip to :burger
end



In [ ]: