@@ -294,24 +294,27 @@ end
294294export stimulustiming!
295295
296296"""
297- @recipe(ScaleBar)
297+ scalebar!(ax; origin=(0.0, 0.0), length_x=nothing, length_y=nothing,
298+ color=:black, linewidth=2.0, text_color=:black, fontsize=12,
299+ x_label=nothing, y_label=nothing, offset_x_ratio=0.02, offset_y_ratio=0.02)
298300
299- A recipe for adding scale bars to plots . This recipe adds:
301+ Add scale bars to a plot . This function adds:
3003021. An optional x-scale bar (horizontal)
3013032. An optional y-scale bar (vertical)
302304
303- # Attributes
304- - `start_x `: Starting x-position for the x- scale bar (default: nothing, meaning no x-scale bar)
305- - `length_x `: Length of the x- scale bar in data units (default: 1.0 )
306- - `start_y `: Starting y-position for the y -scale bar (default: nothing, meaning no y -scale bar)
307- - `length_y`: Length of the y-scale bar in data units (default: 1.0 )
305+ # Arguments
306+ - `ax `: The axis to add scale bars to
307+ - `origin `: (x,y) position where the scale bars will start (default: (0.0, 0.0) )
308+ - `length_x `: Length of the x -scale bar in data units (default: nothing, meaning no x -scale bar)
309+ - `length_y`: Length of the y-scale bar in data units (default: nothing, meaning no y-scale bar )
308310- `color`: Color of the scale bars (default: :black)
309311- `linewidth`: Width of the scale bars (default: 2.0)
310312- `text_color`: Color of the scale bar labels (default: :black)
311313- `fontsize`: Size of the scale bar labels (default: 12)
312314- `x_label`: Label for the x-scale bar (default: nothing, will use length_x value)
313315- `y_label`: Label for the y-scale bar (default: nothing, will use length_y value)
314- - `offset_ratio`: Ratio of axis range to use for text offset (default: 0.02)
316+ - `offset_x_ratio`: Ratio of x-axis range to use for text offset (default: 0.02)
317+ - `offset_y_ratio`: Ratio of y-axis range to use for text offset (default: 0.02)
315318
316319# Example
317320```julia
@@ -320,84 +323,63 @@ ax = Axis(fig[1,1])
320323# First plot your data
321324lines!(ax, x, y)
322325# Then add scale bars
323- scale_bar !(ax, start_x= 0.1, length_x=10.0, start_y=0.1 , length_y=5.0)
326+ scalebar !(ax, origin=( 0.1, 0.1), length_x=10.0 , length_y=5.0)
324327```
325328"""
326- @recipe (ScaleBar) do scene
327- Attributes (
328- start_x = nothing ,
329- length_x = 1.0 ,
330- start_y = nothing ,
331- length_y = 1.0 ,
332- color = :black ,
333- linewidth = 2.0 ,
334- text_color = :black ,
335- fontsize = 12 ,
336- x_label = nothing ,
337- y_label = nothing ,
338- offset_ratio = 0.02
339- )
340- end
341-
342- """
343- Makie.plot!(sb::ScaleBar)
344-
345- Internal plotting function for ScaleBar recipe. Adds scale bars to the current plot
346- based on the specified positions and lengths.
347- """
348- function Makie. plot! (sb:: ScaleBar )
349- # Get the parent axis
350- ax = sb. parent
351-
352- # Get current axis limits
353- xlims = ax. finallimits[]. origin[1 ], ax. finallimits[]. origin[1 ] + ax. finallimits[]. widths[1 ]
354- ylims = ax. finallimits[]. origin[2 ], ax. finallimits[]. origin[2 ] + ax. finallimits[]. widths[2 ]
355-
356- # Calculate offsets based on axis ranges
357- x_range = xlims[2 ] - xlims[1 ]
358- y_range = ylims[2 ] - ylims[1 ]
359- offset = sb. offset_ratio[] * min (x_range, y_range)
329+ function scalebar! (ax;
330+ origin = (0.0 , 0.0 ),
331+ length_x = nothing ,
332+ length_y = nothing ,
333+ color = :black ,
334+ linewidth = 2.0 ,
335+ text_color = :black ,
336+ fontsize = 12 ,
337+ x_label = nothing ,
338+ y_label = nothing ,
339+ offset_x_ratio = 10.0 ,
340+ offset_y_ratio = 0.2
341+ )
342+ x_start, y_start = origin
360343
361- # Plot x-scale bar if start_x is specified
362- if ! isnothing (sb. start_x[])
363- x_start = sb. start_x[]
364- x_end = x_start + sb. length_x[]
365- y_pos = sb. start_y[] === nothing ? ylims[1 ] + offset : sb. start_y[]
344+ # Plot x-scale bar if length_x is specified
345+ if ! isnothing (length_x)
346+ x_end = x_start + length_x
366347
367- lines! (sb , [x_start, x_end], [y_pos, y_pos ],
368- color = sb . color[] ,
369- linewidth = sb . linewidth[]
348+ lines! (ax , [x_start, x_end], [y_start, y_start ],
349+ color = color,
350+ linewidth = linewidth
370351 )
371352
372- x_label_text = isnothing (sb . x_label[] ) ? " $(sb . length_x[] ) " : sb . x_label[]
373- text! (sb , x_label_text,
374- position = (x_start + sb . length_x[] / 2 , y_pos - offset ),
375- align = (:center , :top ),
376- color = sb . text_color[] ,
377- fontsize = sb . fontsize[]
353+ x_label_text = isnothing (x_label) ? " $(length_x) " : x_label
354+ text! (ax , x_label_text,
355+ position = (x_start + length_x/ 2 , y_start - y_start / offset_x_ratio ),
356+ align = (:center , :center ),
357+ color = text_color,
358+ fontsize = fontsize
378359 )
379360 end
380361
381- # Plot y-scale bar if start_y is specified
382- if ! isnothing (sb. start_y[])
383- y_start = sb. start_y[]
384- y_end = y_start + sb. length_y[]
385- x_pos = sb. start_x[] === nothing ? xlims[1 ] + offset : sb. start_x[]
362+ # Plot y-scale bar if length_y is specified
363+ if ! isnothing (length_y)
364+ y_end = y_start + length_y
386365
387- lines! (sb , [x_pos, x_pos ], [y_start, y_end],
388- color = sb . color[] ,
389- linewidth = sb . linewidth[]
366+ lines! (ax , [x_start, x_start ], [y_start, y_end],
367+ color = color,
368+ linewidth = linewidth
390369 )
391370
392- y_label_text = isnothing (sb . y_label[] ) ? " $(sb . length_y[] ) " : sb . y_label[]
393- text! (sb , y_label_text,
394- position = (x_pos - offset , y_start + sb . length_y[] / 2 ),
395- align = (:right , :center ),
396- color = sb . text_color[] ,
397- fontsize = sb . fontsize[] ,
398- rotation = - pi / 2 # Makie uses radians for rotation
371+ y_label_text = isnothing (y_label) ? " $(length_y) " : y_label
372+ text! (ax , y_label_text,
373+ position = (x_start - x_start / offset_y_ratio , y_start + length_y/ 2 ),
374+ align = (:center , :center ),
375+ color = text_color,
376+ fontsize = fontsize,
377+ rotation = pi / 2 # Makie uses radians for rotation
399378 )
400379 end
401380
402- return sb
403- end
381+ return ax
382+ end
383+
384+ # Export the function
385+ export scalebar!
0 commit comments