@@ -23,10 +23,14 @@ class Axes < Base
2323 attr_reader :y_axis
2424 # Array of X ticks.
2525 attr_reader :x_ticks
26+ # Array of Y ticks.
27+ attr_reader :y_ticks
2628 # Array denoting co-ordinates in pixels of the origin of X and Y axes.
2729 attr_reader :origin
2830 # Number of X ticks.
2931 attr_accessor :num_x_ticks
32+ # Number of Y ticks.
33+ attr_accessor :num_y_ticks
3034 # Position of the legend box.
3135 attr_accessor :legend_box_position
3236 # Set true if title is to be hidden.
@@ -78,7 +82,9 @@ def initialize(figure)
7882 @x_axis = Rubyplot ::Artist ::XAxis . new ( self )
7983 @y_axis = Rubyplot ::Artist ::YAxis . new ( self )
8084 @x_ticks = nil
85+ @y_ticks = nil
8186 @num_x_ticks = 5
87+ @num_y_ticks = 4
8288 @legend_box_position = :top
8389 end
8490
@@ -107,6 +113,7 @@ def draw
107113 configure_title
108114 configure_legends
109115 assign_x_ticks
116+ assign_y_ticks
110117 actually_draw
111118 end
112119
@@ -179,6 +186,10 @@ def x_ticks= x_ticks
179186 @x_ticks = x_ticks
180187 end
181188
189+ def y_ticks = y_ticks
190+ @y_ticks = y_ticks
191+ end
192+
182193 def x_title = x_title
183194 @x_axis . title = x_title
184195 end
@@ -221,6 +232,26 @@ def assign_x_ticks
221232 end
222233 end
223234
235+ def assign_y_ticks
236+ unless @y_ticks
237+ val_distance = ( @y_range [ 1 ] - @y_range [ 0 ] ) . abs / @num_y_ticks . to_f
238+ @y_ticks = ( @y_range [ 0 ] ..@y_range [ 1 ] ) . step ( val_distance ) . map { |i | i }
239+ end
240+ unless @y_ticks . all? { |t | t . is_a? ( Rubyplot ::Artist ::YTick ) }
241+ inter_ticks_distance = @y_axis . length / ( @num_y_ticks - 1 )
242+ @y_ticks . map! . with_index do |tick_label , i |
243+ Rubyplot ::Artist ::YTick . new (
244+ self ,
245+ abs_x : @origin [ 0 ] ,
246+ abs_y : @y_axis . abs_y1 - ( i * inter_ticks_distance ) ,
247+ label : Rubyplot ::Utils . format_label ( tick_label ) ,
248+ length : 6 ,
249+ label_distance : 50
250+ )
251+ end
252+ end
253+ end
254+
224255 def add_plot plot_type , *args , &block
225256 plot = with_backend plot_type , *args
226257 yield ( plot ) if block_given?
@@ -271,6 +302,7 @@ def normalize_plotting_data
271302 def actually_draw
272303 @x_axis . draw
273304 @x_ticks . each ( &:draw )
305+ @y_ticks . each ( &:draw )
274306 @y_axis . draw
275307 @texts . each ( &:draw )
276308 @legend_box . draw
0 commit comments