Skip to content

replace vline/hline with xline/yline - #178

Open
cwnaber wants to merge 1 commit into
masterfrom
yline
Open

replace vline/hline with xline/yline#178
cwnaber wants to merge 1 commit into
masterfrom
yline

Conversation

@cwnaber

@cwnaber cwnaber commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

In short: vline/hline versus xline/yline are very similar, but xline/yline is more full featured. I think we should make these changes to free-speech to "set a good example."

General info about the difference between vline/hline versus xline/yline is below. I intend to share this with lab members later.


ABOUT HLINE AND VLINE

hline and vline are free-speech functions for drawing horizontal and vertical lines. They're simple, and our code uses them a ton. A common call looks like this: hline(0, 'k', '--') , meaning, make a black horizontal line at zero with the '--' line style.

Two limitations with these functions are:

  1. They draw a line that extends to the current axis limits. If you readjust the axis limits after running the function, your line stays in place. See pic # 1 below for an example where I ran hline then shifted the axis limits. This isn't always the desired behavior.
  2. Color and LineStyle are the only properties that can be set in the definition call. Other properties have to be done in a separate code call, which means you also need to assign a handle when defining the line, which might be annoying if you just want to make this line once and then forget about it.

AN ALTERNATIVE FOR MOST SITUATIONS - YLINE AND XLINE

MATLAB has built-in functions yline and xline which behave similarly -- they draw a horizontal or vertical line at a specified value. E.g., yline(0, 'Color', 'k', 'LineStyle', '--') . However, yline and xline use a special object class called LineConstant which extends infinitely and has some different characteristics.

Benefits/differences when using yline and xline:

  • If you resize or rescale the axis or plot more data, your line will still span the whole axis
  • You can set the Label property if you want to add text beside the line -- see text "region A" in pic # 2 below. Label color and position can be configured with properties. Previously, you'd have to create a Text object and define its position relative to the line object, but they weren't intrinsically linked.
  • Allows any number of object properties to be set in the definition
  • Can draw multiple lines in one call -- Accepts a vector of values and draws a line at each one (e.g., xline([5 10 15], 'Color', 'k'))
  • Defaults to plotting "under" other objects ('Layer' property defaults to 'bottom'), which would normally be our intended behavior. No need to remember to set  uistack(h_line, 'bottom') !
  • One goofy use case - You can create a simple band of color by setting a low Alpha (0.2) and high LineWeight (20) -- see red line in pic # 2 below. (If you care about precisely what area is colored, you're still much better off creating patches with fill )
2026-07-22 14_32_53-old style line stops 2026-07-22 11_50_59-new style

@cwnaber
cwnaber requested a review from carrien July 22, 2026 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant