Vertical line with constant x-value (2024)

Vertical line with constant x-value

collapse all in page

Syntax

xline(x)

xline(x,LineSpec)

xline(x,LineSpec,labels)

xline(___,Name,Value)

xline(ax,___)

xl = xline(___)

Description

example

xline(x) creates a vertical line at one or more x-coordinates in the current axes. For example, xline(2) creates a line at x=2.

example

xline(x,LineSpec) specifies the line style, the line color, or both. For example, xline([12 20 33],'--b') creates three dashed blue lines.

example

xline(x,LineSpec,labels) adds labels to the lines.

example

xline(___,Name,Value) specifies constant line properties using one or more name-value pair arguments. For a list of properties, see ConstantLine Properties. Specify name-value pairs after all other input arguments.

example

xline(ax,___) creates the lines in the specified axes instead of the current axes.

example

xl = xline(___) returns a ConstantLine object or an array of ConstantLine objects. Use xl to modify or query properties of the line after it is created.

Examples

collapse all

Create Vertical Line

Open Live Script

Create a vertical line at x = 3.

xline(3);

Vertical line with constant x-value (1)

Add a Label

To create a line with a label, you must also specify the line style. The default line style is a solid line, '-'.

x = linspace(0,6,100);y = exp(x);plot(x,y)xline(4.5,'-',{'Acceptable','Limit'});

Vertical line with constant x-value (2)

Create Multiple Lines with Labels

Open Live Script

Create a vector of normally distributed random numbers and display them in a histogram. Then create three vertical lines with labels at the average value, one standard deviation below the average, and one standard deviation above the average.

data = 5 * randn(1,500) + 20;histogram(data,'FaceAlpha',0.1,'EdgeAlpha',0.1);m = mean(data);s = std(data);xline([m-s m m+s],'-',{'-1 Standard Dev.','Average','+1 Standard Dev.'})

Vertical line with constant x-value (3)

Specify Line Style and Color

Open Live Script

Create a red dashed line at x = 5.

xline(5,'--r');

Vertical line with constant x-value (4)

Add Legend and Specify Label Alignment

Open Live Script

Create a vertical dash-dot line with both a label and a display name for a legend.

y = [3187 2693 1771 1826 1958 3222 1645];barh(y,'DisplayName','Daily Sales')xl = xline(2328,'-.','Average','DisplayName','Average Sales');

Vertical line with constant x-value (5)

Adjust the vertical and horizontal alignment of the label, then display the legend.

xl.LabelVerticalAlignment = 'middle';xl.LabelHorizontalAlignment = 'center';legend('show');

Vertical line with constant x-value (6)

Add Lines to Different Axes

Open Live Script

Call the tiledlayout function to create a 2-by-1 tiled chart layout. Call the nexttile function to create the axes objects ax1 and ax2. Then plot into each of the axes. Add a dotted vertical line and label to each plot by passing the axes to the xline function.

tiledlayout(2,1)ax1 =nexttile;x = linspace(0,10,200);y1 = cos(x); plot(ax1,x,y1)ax2 = nexttile;y2 = sin(x); plot(ax2,x,y2)xline(ax1,pi/2,':','cos(\pi/2)=0');xline(ax2,pi,':','sin(\pi)=0');

Vertical line with constant x-value (7)

Input Arguments

collapse all

xx-coordinates
scalar | vector

x-coordinates, specified as a scalar or a vector containing numeric, categorical, datetime, or duration values. The data type of x must match the data type of the x-axis.

Example: xline(10) displays a line at x=10.

Example: xline([13 20 33]) displays lines at x=13, x=20, and x=33.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | categorical | datetime | duration

LineSpecLine style and color
character vector | string scalar

Line style and color, specified as a character vector or string scalar containing characters and symbols. The characters and symbols can appear in any order. You can specify the line style, line color, or both. Marker symbols such as 'o' are ignored.

Example: '--g' is a green dashed line.

Line StyleDescriptionResulting Line
"-"Solid line

Vertical line with constant x-value (8)

"--"Dashed line

Vertical line with constant x-value (9)

":"Dotted line

Vertical line with constant x-value (10)

"-."Dash-dotted line

Vertical line with constant x-value (11)

Color NameShort NameAppearance
'red''r'

Vertical line with constant x-value (12)

'green''g'

Vertical line with constant x-value (13)

'blue''b'

Vertical line with constant x-value (14)

'cyan' 'c'

Vertical line with constant x-value (15)

'magenta''m'

Vertical line with constant x-value (16)

'yellow''y'

Vertical line with constant x-value (17)

'black''k'

Vertical line with constant x-value (18)

'white''w'

Vertical line with constant x-value (19)

axTarget axes
Axes object

Target axes, specified as an Axes object. Use this argument if you want xline to plot in axes other than the current axes.

labelsLine labels
'' (default) | character vector | string scalar | cell array of character vectors | string array

Line labels, specified as a character vector, string scalar, cell array of character vectors, or string array.

Create a Label for One Line

Specify a character vector or a string scalar to display one line of text. To display multiple lines of text, specify a cell array of character vectors or a string array.

PresentationHow to Specify labelsExample
One line of textSpecify a character vector or a string scalar.
xline(12,'-','Sample')

Vertical line with constant x-value (20)

Multiline textSpecify a cell array of character vectors or a string array. Each element in the array is a different line of text.
xline(12,'-',{'Sample','x=12'})

Vertical line with constant x-value (21)

Create Labels for Multiple Lines

Specify a character vector or a string scalar to display the same label next to all the lines. Specify a cell array of character vectors or a string array to display different labels for each line.

PresentationHow to Specify labelsExample
One shared text labelSpecify a character vector or a string scalar.
xline([13 20 33],'-','Sample')

Vertical line with constant x-value (22)

Different labels for each lineSpecify a cell array of character vectors or a string array. Each element in the array is a label for a different line. The number of elements in the labels array must match the length of x.
label = {'Sample 1','Sample 2','Sample 3'};xline([13 20 33],'-',label)

Vertical line with constant x-value (23)

Different labels with some multiline labelsCreate a cell array of character vectors or a string array. Use the sprintf function to insert newline characters ('\n') in the text.
label = {'Sample 1',sprintf('Sample 2\nx=20'),'Sample 3'};xline([13 20 33],'-',label)

Vertical line with constant x-value (24)

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: xline(5,'LabelVerticalAlignment','middle') specifies the vertical alignment of the label as 'middle'.

Note

The properties listed here are only a subset. For a complete list, see ConstantLine Properties.

ColorLine color
[0.15 0.15 0.15] (default) | RGB triplet | hexadecimal color code | 'r' | 'g' | 'b' | ...

Line color, specified as an RGB triplet, a hexadecimal color code, a color name, or a short name.

For a custom color, specify an RGB triplet or a hexadecimal color code.

  • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1], for example, [0.4 0.6 0.7].

  • A hexadecimal color code is a string scalar or character vector that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Therefore, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
"red""r"[1 0 0]"#FF0000"

Vertical line with constant x-value (25)

"green""g"[0 1 0]"#00FF00"

Vertical line with constant x-value (26)

"blue""b"[0 0 1]"#0000FF"

Vertical line with constant x-value (27)

"cyan" "c"[0 1 1]"#00FFFF"

Vertical line with constant x-value (28)

"magenta""m"[1 0 1]"#FF00FF"

Vertical line with constant x-value (29)

"yellow""y"[1 1 0]"#FFFF00"

Vertical line with constant x-value (30)

"black""k"[0 0 0]"#000000"

Vertical line with constant x-value (31)

"white""w"[1 1 1]"#FFFFFF"

Vertical line with constant x-value (32)

"none"Not applicableNot applicableNot applicableNo color

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB® uses in many types of plots.

RGB TripletHexadecimal Color CodeAppearance
[0 0.4470 0.7410]"#0072BD"

Vertical line with constant x-value (33)

[0.8500 0.3250 0.0980]"#D95319"

Vertical line with constant x-value (34)

[0.9290 0.6940 0.1250]"#EDB120"

Vertical line with constant x-value (35)

[0.4940 0.1840 0.5560]"#7E2F8E"

Vertical line with constant x-value (36)

[0.4660 0.6740 0.1880]"#77AC30"

Vertical line with constant x-value (37)

[0.3010 0.7450 0.9330]"#4DBEEE"

Vertical line with constant x-value (38)

[0.6350 0.0780 0.1840]"#A2142F"

Vertical line with constant x-value (39)

Example: 'g'

Example: [0.6 0.2 0.5]

Example: '#D2F9A7'

LineWidthLine width
0.5 (default) | positive value

Line width, specified as a positive value in points.

LabelHorizontalAlignmentHorizontal alignment of label
'right' (default) | 'left' | 'center'

Horizontal alignment of the label with respect to the line, specified as one of the options in the table.

OptionDescriptionExample
'right'Right side of the line.

Vertical line with constant x-value (40)

'left'Left side of the line.

Vertical line with constant x-value (41)

'center'Center of the line. The label segments the line.

Vertical line with constant x-value (42)

LabelVerticalAlignmentVertical alignment of label
'top' (default) | 'middle' | 'bottom'

Vertical alignment of the label with respect to the line, specified as one of the options in the table.

OptionDescriptionExample
'top'Top of the line

Vertical line with constant x-value (43)

'middle'Middle of the line

Vertical line with constant x-value (44)

'bottom'Bottom of the line

Vertical line with constant x-value (45)

LabelOrientationLabel orientation
'aligned' (default) | 'horizontal'

Label orientation, specified as 'aligned' or 'horizontal'. Examples are shown in the table.

OrientationDescriptionExample
'aligned'Label has the same orientation as the line.

Vertical line with constant x-value (46)

'horizontal'Label is horizontal, regardless of the line orientation.

Vertical line with constant x-value (47)

Algorithms

In a 3-D view of the axes, the constant line appears in the x-y plane at the midpoint of the z-axis limits. If the axes are rotated, then the constant line rotates accordingly.

Version History

Introduced in R2018b

See Also

Functions

  • yline | xregion | yregion

Properties

  • ConstantLine Properties

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Vertical line with constant x-value (48)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Vertical line with constant x-value (2024)

References

Top Articles
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 6621

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.