Skip to content

Commit a7e8a4e

Browse files
committed
working error bars
1 parent 0655680 commit a7e8a4e

File tree

6 files changed

+54
-16
lines changed

6 files changed

+54
-16
lines changed

ext/grruby/grruby.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,8 +1817,8 @@ static VALUE setarrowstyle(VALUE self,VALUE style){
18171817
* The default arrow size is 1.
18181818
*/
18191819
static VALUE setarrowsize(VALUE self,VALUE size) {
1820-
int sizec = NUM2INT(size);
1821-
gr_setarrowsize(size);
1820+
gr_setarrowsize(NUM2DBL(size));
1821+
18221822
return Qtrue;
18231823
}
18241824

@@ -1839,7 +1839,7 @@ static VALUE drawarrow (VALUE self,VALUE x1,VALUE y1,VALUE x2,VALUE y2) {
18391839
double x2c = NUM2DBL(x2);
18401840
double y1c = NUM2DBL(y1);
18411841
double y2c = NUM2DBL(y2);
1842-
gr_drawarrow(x1c, x2c, y1c, y2c);
1842+
gr_drawarrow(x1c, y1c, x2c, y2c);
18431843

18441844
return Qtrue;
18451845
}

lib/rubyplot/artist/arrow.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Rubyplot
22
module Artist
33
class Arrow < Artist::Base
4-
def initialize x1:, y1:, x2:, y2:, size: 1.0, style: :simple_single_ended
4+
def initialize x1:, y1:, x2:, y2:, size: 0.5, style: :simple_single_ended
55
@x1 = x1
66
@y1 = y1
77
@x2 = x2

lib/rubyplot/artist/plot/error_bar.rb

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ module Artist
33
module Plot
44
class ErrorBar < Artist::Plot::Base
55
attr_accessor :xerr
6-
attr_accessor :yerr, :xuplims, :xlolims, :yuplims, :xlolims
6+
attr_accessor :yerr, :xuplims, :xlolims, :yuplims, :ylolims
77

88
def initialize(*)
99
super
1010
end
1111

1212
def process_data
1313
super
14-
1514
preprocess_err_values!
1615

1716
check_lims_sizes
1817
check_err_sizes
1918

19+
adjust_axes_ranges!
2020
@line = Rubyplot::Artist::Line2D.new(
2121
self,
2222
x: @data[:x_values],
@@ -29,12 +29,25 @@ def process_data
2929

3030
def draw
3131
@line.draw
32-
@yerr_lines.each(&:draw) if @yerr
33-
@xerr_lines.each(&:draw) if @xerr
32+
@yerr_lines.each(&:draw) if @yerr_lines
33+
@xerr_lines.each(&:draw) if @xerr_lines
3434
end
3535

3636
private
3737

38+
def adjust_axes_ranges!
39+
if @xerr
40+
@axes.x_axis.max_val = @data[:x_values].max + @xerr.max
41+
@axes.x_axis.min_val = @data[:x_values].min - @xerr.min
42+
end
43+
44+
if @yerr
45+
@axes.y_axis.max_val = @data[:y_values].max + @yerr.max
46+
@axes.y_axis.min_val = @data[:y_values].min - @yerr.min
47+
end
48+
49+
end
50+
3851
def preprocess_err_values!
3952
if @yerr
4053
if @yerr.is_a?(Float)
@@ -74,12 +87,13 @@ def generate_xerr
7487

7588
if @xlolims && @xlolims[idx]
7689
arrows << Rubyplot::Artist::Arrow.new(
77-
x1: xcoord - xe,
90+
x1: xcoord,
7891
y1: ycoord,
79-
x2: xcoord,
92+
x2: xcoord - xe,
8093
y2: ycoord
8194
)
8295
end
96+
arrows
8397
end
8498
end
8599
@xerr_lines.flatten!
@@ -97,8 +111,29 @@ def generate_yerr
97111
y: [ycoord - ye, ycoord + ye],
98112
color: @data[:color]
99113
)
114+
else
115+
arrows = []
116+
if @yuplims && @yuplims[idx]
117+
arrows << Rubyplot::Artist::Arrow.new(
118+
x1: xcoord,
119+
y1: ycoord,
120+
x2: xcoord,
121+
y2: ycoord + ye
122+
)
123+
end
124+
125+
if @ylolims && @ylolims[idx]
126+
arrows << Rubyplot::Artist::Arrow.new(
127+
x1: xcoord,
128+
y1: ycoord,
129+
x2: xcoord,
130+
y2: ycoord - ye
131+
)
132+
end
133+
arrows
100134
end
101135
end
136+
@yerr_lines.flatten!
102137
end
103138

104139
def check_lims_sizes

lib/rubyplot/backend/gr_wrapper.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,11 @@ def draw_circle(x:, y:, radius:, border_width:, border_color:, border_type:,
321321
end
322322

323323
def draw_arrow(x1:, y1:, x2:, y2:, size:, style:)
324-
GR.setarrowstyle(ARROW_STYLE_MAP[style])
325-
GR.setarrowsize(size)
326-
GR.drawarrow(x1, y1, x2, y2)
324+
within_window do
325+
GR.setarrowstyle(ARROW_STYLE_MAP[style])
326+
GR.setarrowsize(size)
327+
GR.drawarrow(x1, y1, x2, y2)
328+
end
327329
end
328330

329331
def draw

rubyplot.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
2727
spec.add_development_dependency 'rspec'
2828
spec.add_development_dependency 'pry'
2929
spec.add_development_dependency 'rubocop'
30+
spec.add_development_dependency 'parallel_tests'
3031

3132
spec.add_runtime_dependency 'rmagick', '>= 2.13.4'
3233
end

spec/axes_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@
496496
end
497497
end
498498

499-
context "#error_bar!", focus: true do
499+
context "#error_bar!" do
500500
before do
501501
@x = [1,2,3,4,5,6]
502502
@y = [3,4,5,6,7,8]
@@ -571,14 +571,14 @@
571571
end
572572
end
573573

574-
it "adds error bar with upper limit and lower limit with collection xerr & yerr" do
574+
it "adds error bar with upper limit and lower limit with collection xerr & yerr", focus: true do
575575
@figure = Rubyplot::Figure.new
576576
axes = @figure.add_subplot! 0,0
577577
axes.title = "Error bar plot with lots of options"
578578
axes.error_bar! do |p|
579579
p.data [1,2,3,4], [1,4,9,16]
580580
p.xerr = [0.5,1.0,1.5,0.3]
581-
p.yerr = [0.6,0.2,0.8,0.1]
581+
p.yerr = [0.6,1.0,0.8,0.5]
582582
p.xuplims = [true, false, true, false]
583583
p.xlolims = [false, true, false, true]
584584
p.yuplims = [true, false, true, false]

0 commit comments

Comments
 (0)