Class: Charty::Backends::Pyplot
- Inherits:
-
Object
- Object
- Charty::Backends::Pyplot
- Defined in:
- lib/charty/backends/pyplot.rb
Class Method Summary collapse
Instance Method Summary collapse
- #bar(bar_pos, values, color: nil, width:, align: :center, orient: :v) ⇒ Object
-
#begin_figure ⇒ Object
NEW PLOTTING API ====.
- #box_plot(plot_data, positions, color:, gray:, width:, flier_size: 5, whisker: 1.5, notch: false) ⇒ Object
- #disable_xaxis_grid ⇒ Object
-
#initialize ⇒ Pyplot
constructor
A new instance of Pyplot.
- #label(x, y) ⇒ Object
- #plot(ax, context, subplot: false) ⇒ Object
- #render(context, filename) ⇒ Object
- #render_layout(layout) ⇒ Object
- #save(context, filename, finish: true) ⇒ Object
- #series=(series) ⇒ Object
- #set_xlabel(label) ⇒ Object
- #set_xlim(min, max) ⇒ Object
- #set_xtick_labels(labels) ⇒ Object
- #set_xticks(values) ⇒ Object
- #set_ylabel(label) ⇒ Object
- #show ⇒ Object
Constructor Details
#initialize ⇒ Pyplot
Returns a new instance of Pyplot.
14 15 16 |
# File 'lib/charty/backends/pyplot.rb', line 14 def initialize @pyplot = ::Matplotlib::Pyplot end |
Class Method Details
.activate_iruby_integration ⇒ Object
18 19 20 21 |
# File 'lib/charty/backends/pyplot.rb', line 18 def self.activate_iruby_integration require 'matplotlib/iruby' ::Matplotlib::IRuby.activate end |
.prepare ⇒ Object
9 10 11 |
# File 'lib/charty/backends/pyplot.rb', line 9 def prepare require 'matplotlib/pyplot' end |
Instance Method Details
#bar(bar_pos, values, color: nil, width:, align: :center, orient: :v) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/charty/backends/pyplot.rb', line 168 def (, values, color: nil, width: 0.8r, align: :center, orient: :v) = Array() values = Array(values) color = Array(color).map(&:to_hex_string) width = Float(width) if orient == :v @pyplot.(, values, width: width, color: color, align: align) else @pyplot.(, values, width: width, color: color, align: align) end end |
#begin_figure ⇒ Object
NEW PLOTTING API ====
164 165 166 |
# File 'lib/charty/backends/pyplot.rb', line 164 def begin_figure # do nothing end |
#box_plot(plot_data, positions, color:, gray:, width:, flier_size: 5, whisker: 1.5, notch: false) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/charty/backends/pyplot.rb', line 180 def box_plot(plot_data, positions, color:, gray:, width: 0.8r, flier_size: 5, whisker: 1.5, notch: false) color = Array(color).map(&:to_hex_string) gray = gray.to_hex_string width = Float(width) flier_size = Float(flier_size) whisker = Float(whisker) plot_data.each_with_index do |group_data, i| next if group_data.nil? || group_data.empty? artist_dict = @pyplot.boxplot(group_data, vert: :v, patch_artist: true, positions: [i], widths: width, whis: whisker, ) artist_dict["boxes"].each do |box| box.update({facecolor: color[i], zorder: 0.9, edgecolor: gray}, {}) end artist_dict["whiskers"].each do |whisker| whisker.update({color: gray, linestyle: "-"}, {}) end artist_dict["caps"].each do |cap| cap.update({color: gray}, {}) end artist_dict["medians"].each do |median| median.update({color: gray}, {}) end artist_dict["fliers"].each do |flier| flier.update({ markerfacecolor: gray, marker: "d", markeredgecolor: gray, markersize: flier_size }, {}) end end end |
#disable_xaxis_grid ⇒ Object
239 240 241 |
# File 'lib/charty/backends/pyplot.rb', line 239 def disable_xaxis_grid @pyplot.gca.xaxis.grid(false) end |
#label(x, y) ⇒ Object
23 24 |
# File 'lib/charty/backends/pyplot.rb', line 23 def label(x, y) end |
#plot(ax, context, subplot: false) ⇒ Object
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/charty/backends/pyplot.rb', line 59 def plot(ax, context, subplot: false) # TODO: Since it is not required, research and change conditions. # case # when @pyplot.respond_to?(:xlim) # @pyplot.xlim(context.range_x.begin, context.range_x.end) # @pyplot.ylim(context.range_y.begin, context.range_y.end) # when @pyplot.respond_to?(:set_xlim) # @pyplot.set_xlim(context.range_x.begin, context.range_x.end) # @pyplot.set_ylim(context.range_y.begin, context.range_y.end) # end ax.title(context.title) if context.title if !subplot ax.xlabel(context.xlabel) if context.xlabel ax.ylabel(context.ylabel) if context.ylabel end palette = Charty::Palette.default colors = palette.colors.map {|c| c.to_rgb.to_hex_string }.cycle case context.method when :bar context.series.each do |data| ax.(data.xs.to_a.map(&:to_s), data.ys.to_a, label: data.label, color: colors.next) end ax.legend() when :barh context.series.each do |data| ax.(data.xs.to_a.map(&:to_s), data.ys.to_a, color: colors.next) end when :box_plot min_l = palette.colors.map {|c| c.to_rgb.to_hsl.l }.min lum = min_l*0.6 gray = Colors::RGB.new(lum, lum, lum).to_hex_string draw_box_plot(context, subplot, colors, gray) when :bubble context.series.each do |data| ax.scatter(data.xs.to_a, data.ys.to_a, s: data.zs.to_a, alpha: 0.5, color: colors.next, label: data.label) end ax.legend() when :curve context.series.each do |data| ax.plot(data.xs.to_a, data.ys.to_a, color: colors.next) end when :scatter context.series.each do |data| ax.scatter(data.xs.to_a, data.ys.to_a, label: data.label, color: colors.next) end ax.legend() when :error_bar context.series.each do |data| ax.( data.xs.to_a, data.ys.to_a, data.xerr, data.yerr, label: data.label, color: colors.next ) end ax.legend() when :hist data = Array(context.data) ax.hist(data, color: colors.take(data.length), alpha: 0.4) end end |
#render(context, filename) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/charty/backends/pyplot.rb', line 41 def render(context, filename) plot(@pyplot, context) if filename FileUtils.mkdir_p(File.dirname(filename)) @pyplot.savefig(filename) end @pyplot.show end |
#render_layout(layout) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/charty/backends/pyplot.rb', line 30 def render_layout(layout) _fig, axes = @pyplot.subplots(nrows: layout.num_rows, ncols: layout.num_cols) layout.rows.each_with_index do |row, y| row.each_with_index do |cel, x| ax = layout.num_rows > 1 ? axes[y][x] : axes[x] plot(ax, cel, subplot: true) end end @pyplot.show end |
#save(context, filename, finish: true) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/charty/backends/pyplot.rb', line 50 def save(context, filename, finish: true) plot(context) if filename FileUtils.mkdir_p(File.dirname(filename)) @pyplot.savefig(filename) end @pyplot.clf if finish end |
#series=(series) ⇒ Object
26 27 28 |
# File 'lib/charty/backends/pyplot.rb', line 26 def series=(series) @series = series end |
#set_xlabel(label) ⇒ Object
219 220 221 |
# File 'lib/charty/backends/pyplot.rb', line 219 def set_xlabel(label) @pyplot.gca.set_xlabel(String(label)) end |
#set_xlim(min, max) ⇒ Object
235 236 237 |
# File 'lib/charty/backends/pyplot.rb', line 235 def set_xlim(min, max) @pyplot.gca.set_xlim(Float(min), Float(max)) end |
#set_xtick_labels(labels) ⇒ Object
231 232 233 |
# File 'lib/charty/backends/pyplot.rb', line 231 def set_xtick_labels(labels) @pyplot.gca.set_xticklabels(Array(labels).map(&method(:String))) end |
#set_xticks(values) ⇒ Object
227 228 229 |
# File 'lib/charty/backends/pyplot.rb', line 227 def set_xticks(values) @pyplot.gca.set_xticks(Array(values)) end |
#set_ylabel(label) ⇒ Object
223 224 225 |
# File 'lib/charty/backends/pyplot.rb', line 223 def set_ylabel(label) @pyplot.gca.set_ylabel(String(label)) end |
#show ⇒ Object
243 244 245 |
# File 'lib/charty/backends/pyplot.rb', line 243 def show @pyplot.show end |