Skip to content

Commit 1a3f4f3

Browse files
author
alishdipani
committed
Added median width to box plot and improved tests
1 parent 667668f commit 1a3f4f3

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

lib/rubyplot/artist/plot/box_plot.rb

+9-6
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ class BoxPlot < Artist::Plot::Base
1414
attr_accessor :outlier_marker_type
1515
attr_accessor :outlier_marker_color
1616
attr_accessor :outlier_marker_size
17-
17+
attr_accessor :median_width
18+
1819
def initialize(*)
1920
super
2021
@whiskers = 1.5
2122
@x_left_box = []
22-
@median_color = :yellow
23+
@median_color = :black
2324
@outlier_marker_type = :plus
2425
@outlier_marker_color = nil
2526
@outlier_marker_size = 1.0
27+
@median_width = 3.0
2628
end
2729

2830
def process_data
@@ -45,7 +47,7 @@ def draw
4547
draw_box x_left, i
4648
draw_whiskers x_left, i
4749
draw_outliers x_left, i
48-
draw_median x_left, i
50+
draw_median x_left, i
4951
end
5052
end
5153

@@ -111,7 +113,7 @@ def draw_outliers x_left, index
111113

112114
def first_max_outlier_index vector, max
113115
return nil if vector.last >= max
114-
vector.size.times do |i|
116+
vector.size.times do |i|
115117
if vector[i] > max
116118
return i
117119
end
@@ -131,7 +133,8 @@ def draw_median x_left, index
131133
Rubyplot::Artist::Line2D.new(self,
132134
x: [x_left, x_left + @box_width],
133135
y: [@medians[index], @medians[index]],
134-
color: @median_color
136+
color: @median_color,
137+
width: @median_width
135138
).draw
136139
end
137140

@@ -141,7 +144,7 @@ def calculate_ranges!
141144
@medians = []
142145
@mins = []
143146
@maxs = []
144-
147+
145148
@vectors.each do |sorted_vec|
146149
m = get_percentile 50, sorted_vec
147150
q1 = get_percentile 25, sorted_vec

spec/axes_spec.rb

+26-7
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,26 @@
901901
axes.y_title = "bar"
902902
end
903903

904+
it "adds a simple box plot with outliers" do
905+
@figure = Rubyplot::Figure.new
906+
axes = @figure.add_subplot! 0,0
907+
axes.title = "A simple box plot."
908+
axes.box_plot! do |p|
909+
p.data [
910+
[60,70,80,70,50],
911+
[100,40,20,80,70],
912+
[30, 10]
913+
]
914+
p.whiskers = 0.1
915+
p.outlier_marker_type = :diamond
916+
p.outlier_marker_size = 3.0
917+
p.outlier_marker_color = :red
918+
p.median_width = 4.0
919+
end
920+
axes.x_title = "foo"
921+
axes.y_title = "bar"
922+
end
923+
904924
it "adds a simple horizontal box plot" do
905925
skip "Leave for after initial box plot setup is complete."
906926
end
@@ -913,7 +933,7 @@
913933
p.data [
914934
[-48, 2,60,70,80,70,50],
915935
[4,100,40,20,80,70],
916-
[1,30, 10]
936+
[1,30, 10]
917937
]
918938
end
919939
axes.box_plot! do |p|
@@ -935,7 +955,7 @@
935955
p.data [
936956
[-48,60,70,80,70,50],
937957
[4,100,40,20,80,70],
938-
[1,30, 10]
958+
[1,30, 10]
939959
]
940960
p.whiskers = 0.3
941961
end
@@ -954,25 +974,25 @@
954974

955975
context "#top_margin=" do
956976
it "sets the top margin in pixels" do
957-
977+
958978
end
959979
end
960980

961981
context "#left_margin=" do
962982
it "sets the left margin in pixels" do
963-
983+
964984
end
965985
end
966986

967987
context "#bottom_margin=" do
968988
it "sets the bottom margin in pixels" do
969-
989+
970990
end
971991
end
972992

973993
context "#right_margin=" do
974994
it "sets the right margin in pixels" do
975-
995+
976996
end
977997
end
978998

@@ -988,4 +1008,3 @@
9881008
end
9891009
end # context "#x_ticks="
9901010
end # Rubyplot::Axes
991-

0 commit comments

Comments
 (0)