Skip to content

Commit 667668f

Browse files
author
alishdipani
committed
Added width and color options to error bar plot and improved tests
1 parent 1c08554 commit 667668f

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

lib/rubyplot/artist/plot/error_bar.rb

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ module Rubyplot
22
module Artist
33
module Plot
44
class ErrorBar < Artist::Plot::Base
5-
attr_accessor :xerr
6-
attr_accessor :yerr, :xuplims, :xlolims, :yuplims, :ylolims
7-
5+
attr_accessor :xerr, :yerr, :xuplims, :xlolims, :yuplims, :ylolims, :line_width, :xerr_width, :yerr_width, :xerr_color, :yerr_color
6+
87
def initialize(*)
98
super
9+
@line_width = 1.0
10+
@xerr_width = 1.0
11+
@yerr_width = 1.0
12+
@xerr_color = nil
13+
@yerr_color = nil
1014
end
1115

1216
def process_data
1317
super
1418
preprocess_err_values!
15-
19+
1620
check_lims_sizes
1721
check_err_sizes
1822

@@ -21,7 +25,8 @@ def process_data
2125
self,
2226
x: @data[:x_values],
2327
y: @data[:y_values],
24-
color: @data[:color]
28+
color: @data[:color],
29+
width: @line_width
2530
)
2631
generate_yerr if @yerr
2732
generate_xerr if @xerr
@@ -40,7 +45,7 @@ def adjust_axes_ranges!
4045
@axes.x_axis.max_val = @data[:x_values].max + @xerr.max
4146
@axes.x_axis.min_val = @data[:x_values].min - @xerr.min
4247
end
43-
48+
4449
if @yerr
4550
@axes.y_axis.max_val = @data[:y_values].max + @yerr.max
4651
@axes.y_axis.min_val = @data[:y_values].min - @yerr.min
@@ -71,7 +76,8 @@ def generate_xerr
7176
self,
7277
x: [xcoord - xe, xcoord + xe],
7378
y: [ycoord, ycoord],
74-
color: @data[:color]
79+
color: @xerr_color.nil? ? @data[:color] : @xerr_color,
80+
width: @xerr_width
7581
)
7682
else
7783
arrows = []
@@ -89,7 +95,7 @@ def generate_xerr
8995
x1: xcoord,
9096
y1: ycoord,
9197
x2: xcoord - xe,
92-
y2: ycoord
98+
y2: ycoord
9399
)
94100
end
95101
arrows
@@ -108,7 +114,8 @@ def generate_yerr
108114
self,
109115
x: [xcoord, xcoord],
110116
y: [ycoord - ye, ycoord + ye],
111-
color: @data[:color]
117+
color: @yerr_color.nil? ? @data[:color] : @yerr_color,
118+
width: @yerr_width
112119
)
113120
else
114121
arrows = []
@@ -126,7 +133,7 @@ def generate_yerr
126133
x1: xcoord,
127134
y1: ycoord,
128135
x2: xcoord,
129-
y2: ycoord - ye
136+
y2: ycoord - ye
130137
)
131138
end
132139
arrows
@@ -136,7 +143,7 @@ def generate_yerr
136143
end
137144

138145
def check_lims_sizes
139-
146+
140147
end
141148

142149
def check_err_sizes

spec/axes_spec.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@
780780
@x = [1,2,3,4,5,6]
781781
@y = [3,4,5,6,7,8]
782782
end
783-
783+
784784
it "adds a simple xerr to error bar plot" do
785785
@figure = Rubyplot::Figure.new
786786
axes = @figure.add_subplot! 0,0
@@ -798,7 +798,8 @@
798798
axes.error_bar! do |p|
799799
p.data @x, @y
800800
p.xerr = [0.1,0.3,0.5,0.1,0.2,0.4]
801-
end
801+
p.color = :red
802+
end
802803
end
803804

804805
it "adds a simple yerr to the error bar plot" do
@@ -818,7 +819,8 @@
818819
axes.error_bar! do |p|
819820
p.data @x, @y
820821
p.yerr = [0.6,0.5,0.1,0.8,0.3,0.1]
821-
end
822+
p.color = :green
823+
end
822824
end
823825

824826
it "adds an asymmetric collection of yerr to the error bar plot" do
@@ -850,7 +852,24 @@
850852
end
851853
end
852854

853-
it "adds error bar with upper limit and lower limit with collection xerr & yerr" do
855+
it "adds both xerr and yerr to the error bar plot with colour and width" do
856+
@figure = Rubyplot::Figure.new
857+
axes = @figure.add_subplot! 0,0
858+
axes.title = "Simple error bar plot with collection xerr and yerr."
859+
axes.error_bar! do |p|
860+
p.data [1,2,3,4], [1,4,9,16]
861+
p.xerr = [0.5,1.0,1.5,0.3]
862+
p.yerr = [0.6,0.2,0.8,0.1]
863+
p.line_width = 5
864+
p.xerr_width = 2
865+
p.yerr_width = 2
866+
p.xerr_color = :blue
867+
p.yerr_color = :red
868+
p.color = :yellow
869+
end
870+
end
871+
872+
it "adds error bar with upper limit and lower limit with collection xerr & yerr" do
854873
@figure = Rubyplot::Figure.new
855874
axes = @figure.add_subplot! 0,0
856875
axes.title = "Error bar plot with lots of options"

0 commit comments

Comments
 (0)