Skip to content

Commit f9cde9a

Browse files
committed
fix some bugs in scatter with negative values
1 parent 799ed02 commit f9cde9a

File tree

7 files changed

+27
-13
lines changed

7 files changed

+27
-13
lines changed

lib/rubyplot/artist/axes.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,6 @@ def legend_box_iy
9999
end
100100

101101
# Write an image to a file by communicating with the backend.
102-
# FIXME: (refactor) Currently draw first assigns default colors and then
103-
# performs consolidation etc of the data and then assigns the default X ticks.
104-
# The reason for this is that default labels are needed by the consolidated
105-
# plots when they create the required rectangles etc. However assigning
106-
# default ticks should be a part of the 'assign defaults' step and should
107-
# therefore should be clubbed with the assign labels step.
108102
def draw
109103
set_axes_ranges
110104
normalize_plotting_data
@@ -207,8 +201,12 @@ def assign_default_label_colors
207201
def assign_x_ticks
208202
@inter_x_ticks_distance = @x_axis.length / (@num_x_ticks.to_f-1)
209203
unless @x_ticks
210-
@x_ticks = (@x_range[0]..@x_range[1]).step(@inter_x_ticks_distance).map { |i| i }
204+
value_distance = (@x_range[1] - @x_range[0]) / (@num_x_ticks.to_f - 1)
205+
@x_ticks = @num_x_ticks.times.map do |i|
206+
@x_range[0] + i * value_distance
207+
end
211208
end
209+
212210
unless @x_ticks.all? { |t| t.is_a?(Rubyplot::Artist::XTick) }
213211
@x_ticks.map!.with_index do |tick_label, i|
214212
Rubyplot::Artist::XTick.new(

lib/rubyplot/artist/plot/scatter.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ def draw
1717
abs_x = ix * @axes.x_axis.length + @axes.abs_x + @axes.y_axis_margin
1818
abs_y = (@axes.y_axis.length - iy * @axes.y_axis.length) + @axes.abs_y
1919
Rubyplot::Artist::Circle.new(
20-
self, abs_x: abs_x, abs_y: abs_y, radius: @circle_radius, stroke_opacity: @stroke_opacity,
21-
stroke_width: @stroke_width
20+
self, abs_x: abs_x, abs_y: abs_y, radius: @circle_radius,
21+
stroke_opacity: @stroke_opacity,
22+
stroke_width: @stroke_width
2223
).draw
2324
end
2425
end

lib/rubyplot/utils.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Utils
33
THOUSAND_SEPARATOR = ','.freeze
44
class << self
55
def format_label label
6-
if label.is_a? Float
6+
if label.is_a? Numeric
77
'%0.2f' % label
88
elsif label.is_a? String
99
label

spec/axes_spec.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@
270270
axes.title = "Random bar numbers"
271271
end
272272

273-
it "adds bar plot with title margin", focus: true do
273+
it "adds bar plot with title margin" do
274274
@figure = Rubyplot::Figure.new
275275
axes = @figure.add_subplot 0,0
276276
axes.bar! do |p|axes =
@@ -281,7 +281,7 @@
281281
axes.title = "Bar with title margin = 100"
282282
end
283283

284-
it "checks if Geometry adjusts for large numbers" do
284+
it "plots large numbers" do
285285
@figure = Rubyplot::Figure.new
286286
axes = @figure.add_subplot 0,0
287287
axes.bar! do |p|
@@ -315,7 +315,7 @@
315315
end
316316
end
317317

318-
it "plots both positive and negative values" do
318+
it "plots both positive and negative values", focus: true do
319319
@figure = Rubyplot::Figure.new
320320
axes = @figure.add_subplot 0,0
321321
axes.bar! do |p|
@@ -382,6 +382,11 @@
382382
end
383383

384384
context "#scatter!" do
385+
before do
386+
@x1 = [1, 2, 3, 4, 5]
387+
@y1 = [11, 2, 33, 4, 65]
388+
end
389+
385390
it "adds a simple scatter plot." do
386391
@figure = Rubyplot::Figure.new
387392
axes = @figure.add_subplot 0,0
@@ -404,6 +409,16 @@
404409
end
405410
axes.title = "all negative scatter graph test."
406411
end
412+
413+
it "adds scatter with positive and negative values" do
414+
@figure = Rubyplot::Figure.new
415+
axes = @figure.add_subplot 0,0
416+
axes.scatter! do |p|
417+
p.data [-2,0,2,4,6], [-3,-1, 1, 4, 8]
418+
p.label = "values"
419+
end
420+
axes.title = "positive + negative test."
421+
end
407422
end
408423

409424
context "#top_margin=" do
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)