Class: Charty::PyPlot

Inherits:
PlotterAdapter show all
Defined in:
lib/charty/backends/pyplot.rb

Constant Summary collapse

Name =
"pyplot"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PlotterAdapter

create, inherited

Constructor Details

#initializePyPlot

Returns a new instance of PyPlot


7
8
9
# File 'lib/charty/backends/pyplot.rb', line 7

def initialize
  @plot = Matplotlib::Pyplot
end

Class Method Details

.activate_iruby_integrationObject


11
12
13
14
# File 'lib/charty/backends/pyplot.rb', line 11

def self.activate_iruby_integration
  require 'matplotlib/iruby'
  Matplotlib::IRuby.activate
end

Instance Method Details

#label(x, y) ⇒ Object


16
17
# File 'lib/charty/backends/pyplot.rb', line 16

def label(x, y)
end

#plot(plot, context, subplot: false) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/charty/backends/pyplot.rb', line 51

def plot(plot, context, subplot: false)
  # TODO: Since it is not required, research and change conditions.
  # case
  # when plot.respond_to?(:xlim)
  #   plot.xlim(context.range_x.begin, context.range_x.end)
  #   plot.ylim(context.range_y.begin, context.range_y.end)
  # when plot.respond_to?(:set_xlim)
  #   plot.set_xlim(context.range_x.begin, context.range_x.end)
  #   plot.set_ylim(context.range_y.begin, context.range_y.end)
  # end

  plot.title(context.title) if context.title
  if !subplot
    plot.xlabel(context.xlabel) if context.xlabel
    plot.ylabel(context.ylabel) if context.ylabel
  end

  case context.method
  when :bar
    context.series.each do |data|
      plot.bar(data.xs.to_a.map(&:to_s), data.ys.to_a, label: data.label)
    end
    plot.legend()
  when :barh
    context.series.each do |data|
      plot.barh(data.xs.to_a.map(&:to_s), data.ys.to_a)
    end
  when :box_plot
    plot.boxplot(context.data.to_a, labels: context.labels)
  when :bubble
    context.series.each do |data|
      plot.scatter(data.xs.to_a, data.ys.to_a, s: data.zs.to_a, alpha: 0.5, label: data.label)
    end
    plot.legend()
  when :curve
    context.series.each do |data|
      plot.plot(data.xs.to_a, data.ys.to_a)
    end
  when :scatter
    context.series.each do |data|
      plot.scatter(data.xs.to_a, data.ys.to_a, label: data.label)
    end
    plot.legend()
  when :error_bar
    context.series.each do |data|
      plot.errorbar(
        data.xs.to_a,
        data.ys.to_a,
        data.xerr,
        data.yerr,
        label: data.label,
      )
    end
    plot.legend()
  when :hist
    plot.hist(context.data.to_a)
  end
end

#render(context, filename) ⇒ Object


34
35
36
37
38
39
40
41
# File 'lib/charty/backends/pyplot.rb', line 34

def render(context, filename)
  plot(@plot, context)
  if filename
    FileUtils.mkdir_p(File.dirname(filename))
    @plot.savefig(filename)
  end
  @plot.show
end

#render_layout(layout) ⇒ Object


23
24
25
26
27
28
29
30
31
32
# File 'lib/charty/backends/pyplot.rb', line 23

def render_layout(layout)
  (fig, axes) = *@plot.subplots(nrows: layout.num_rows, ncols: layout.num_cols)
  layout.rows.each_with_index do |row, y|
    row.each_with_index do |cel, x|
      plot = layout.num_rows > 1 ? axes[y][x] : axes[x]
      plot(plot, cel, subplot: true)
    end
  end
  @plot.show
end

#save(context, filename) ⇒ Object


43
44
45
46
47
48
49
# File 'lib/charty/backends/pyplot.rb', line 43

def save(context, filename)
  plot(@plot, context)
  if filename
    FileUtils.mkdir_p(File.dirname(filename))
    @plot.savefig(filename)
  end
end

#series=(series) ⇒ Object


19
20
21
# File 'lib/charty/backends/pyplot.rb', line 19

def series=(series)
  @series = series
end