Class: Polars::DataFramePlot

Inherits:
Object
  • Object
show all
Defined in:
lib/polars/data_frame_plot.rb

Overview

DataFrame.plot namespace.

Instance Method Summary collapse

Instance Method Details

#area(x, y, color: nil) ⇒ Vega::LiteChart

Draw area plot.

Parameters:

  • x (String)

    Column with x-coordinates of lines.

  • y (String)

    Column with y-coordinates of lines.

  • color (String) (defaults to: nil)

    Column to color lines by.

Returns:

  • (Vega::LiteChart)


57
58
59
# File 'lib/polars/data_frame_plot.rb', line 57

def area(x, y, color: nil)
  line(x, y, color: color, _type: "area")
end

#bar(x, y, color: nil, stacked: nil) ⇒ Vega::LiteChart

Draw bar plot.

Parameters:

  • x (String)

    Column with x-coordinates of bars.

  • y (String)

    Column with y-coordinates of bars.

  • color (String) (defaults to: nil)

    Column to color bars by.

  • stacked (Boolean) (defaults to: nil)

    Stack bars.

Returns:

  • (Vega::LiteChart)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/polars/data_frame_plot.rb', line 125

def bar(x, y, color: nil, stacked: nil)
  data = @df[[x, y, color].compact.map(&:to_s).uniq].rows(named: true)

  encoding = {
    # TODO determine label angle
    y: {field: x, type: "nominal", sort: "none", axis: {labelAngle: 0}},
    x: {field: y, type: "quantitative"}
  }
  if color
    encoding[:color] = {field: color}
    encoding[:yOffset] = {field: color} unless stacked
  end

  Vega.lite
    .data(data)
    .mark(type: "bar", tooltip: true)
    .encoding(encoding)
    .config(axis: {labelFontSize: 12})
end

#column(x, y, color: nil, stacked: nil) ⇒ Vega::LiteChart

Draw column plot.

Parameters:

  • x (String)

    Column with x-coordinates of columns.

  • y (String)

    Column with y-coordinates of columns.

  • color (String) (defaults to: nil)

    Column to color columns by.

  • stacked (Boolean) (defaults to: nil)

    Stack columns.

Returns:

  • (Vega::LiteChart)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/polars/data_frame_plot.rb', line 94

def column(x, y, color: nil, stacked: nil)
  data = @df[[x, y, color].compact.map(&:to_s).uniq].rows(named: true)

  encoding = {
    x: {field: x, type: "nominal", sort: "none", axis: {labelAngle: 0}},
    y: {field: y, type: "quantitative"}
  }
  if color
    encoding[:color] = {field: color}
    encoding[:xOffset] = {field: color} unless stacked
  end

  Vega.lite
    .data(data)
    .mark(type: "bar", tooltip: true)
    .encoding(encoding)
    .config(axis: {labelFontSize: 12})
end

#line(x, y, color: nil, _type: "line") ⇒ Vega::LiteChart

Draw line plot.

Parameters:

  • x (String)

    Column with x-coordinates of lines.

  • y (String)

    Column with y-coordinates of lines.

  • color (String) (defaults to: nil)

    Column to color lines by.

Returns:

  • (Vega::LiteChart)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/polars/data_frame_plot.rb', line 21

def line(x, y, color: nil, _type: "line")
  data = @df[[x, y, color].compact.map(&:to_s).uniq].rows(named: true)

  x_type =
    if @df[x].dtype.numeric?
      "quantitative"
    elsif @df[x].dtype.temporal?
      "temporal"
    else
      "nominal"
    end

  scale = x_type == "temporal" ? {type: "utc"} : {}
  encoding = {
    x: {field: x, type: x_type, scale: scale},
    y: {field: y, type: "quantitative"}
  }
  encoding[:color] = {field: color} if color

  Vega.lite
    .data(data)
    .mark(type: _type, tooltip: true, interpolate: "cardinal", point: {size: 60})
    .encoding(encoding)
    .config(axis: {labelFontSize: 12})
end

#pie(x, y) ⇒ Vega::LiteChart

Draw pie chart.

Parameters:

  • x (String)

    Column with label of slice.

  • y (String)

    Column with size of slice.

Returns:

  • (Vega::LiteChart)


69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/polars/data_frame_plot.rb', line 69

def pie(x, y)
  data = @df[[x, y].map(&:to_s).uniq].rows(named: true)

  Vega.lite
    .data(data)
    .mark(type: "arc", tooltip: true)
    .encoding(
      color: {field: x, type: "nominal", sort: "none", axis: {title: nil}, legend: {labelFontSize: 12}},
      theta: {field: y, type: "quantitative"}
    )
    .view(stroke: nil)
end

#scatter(x, y, color: nil) ⇒ Vega::LiteChart Also known as: point

Draw scatter plot.

Parameters:

  • x (String)

    Column with x-coordinates of points.

  • y (String)

    Column with y-coordinates of points.

  • color (String) (defaults to: nil)

    Column to color points by.

Returns:

  • (Vega::LiteChart)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/polars/data_frame_plot.rb', line 155

def scatter(x, y, color: nil)
  data = @df[[x, y, color].compact.map(&:to_s).uniq].rows(named: true)

  encoding = {
    x: {field: x, type: "quantitative", scale: {zero: false}},
    y: {field: y, type: "quantitative", scale: {zero: false}},
    size: {value: 60}
  }
  encoding[:color] = {field: color} if color

  Vega.lite
    .data(data)
    .mark(type: "circle", tooltip: true)
    .encoding(encoding)
    .config(axis: {labelFontSize: 12})
end