NumericRuler - Control axis with numeric values - MATLAB (original) (raw)

NumericRuler Properties

Control axis with numeric values

NumericRuler properties control the appearance and behavior of an _x_-axis, _y_-axis, or _z_-axis that shows numeric values. Each individual axis has its own ruler object. By changing property values of the ruler, you can modify certain aspects of a specific axis.

Use dot notation to refer to a particular ruler and property. Access the ruler objects through the XAxis, YAxis, andZAxis properties of the Axes object.

ax = gca; co = ax.XAxis.Color; ax.XAxis.Color = 'blue';

Appearance

expand all

Axis label horizontal alignment, specified as one of the values from the table.

LabelHorizontalAlignment Value Description Appearance
'center' For a horizontal axis, the label is centered between the left and right edges of the plot box.For a vertical axis, the label is centered between the top and bottom edges of the plot box. Horizontal and a vertical axis labels that are centered.
'left' For a horizontal axis, the label is aligned with the left edge of the plot box.For a vertical axis, the label is aligned with the bottom edge of the plot box. Horizontal and a vertical axis labels that left-aligned.
'right' For a horizontal axis, the label is aligned with the right edge of the plot box.For a vertical axis, the label is aligned with the top edge of the plot box. Horizontal and a vertical axis labels that right-aligned.

Scale and Direction

Tick Values and Labels

expand all

Exponential notation common to all tick values, specified as an integer value. The axis displays the secondary label 105.

A secondary axis label for scaling tick values by ten raised to the fifth power

The base value is always 10. However, you can change the exponent value by setting the Exponent property. For example, change the exponent to 2.

ax = gca; ax.YAxis.Exponent = 2;

If the exponent value is 0, then the exponent label does not display.

If you assign a value to this property, then MATLAB sets the ExponentMode property to'manual'. If the axis has a log scale, then theExponent property has no effect.

Selection mode for the Exponent property, specified as one of these values:

Data Types: char | string | categorical

Tick label 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.

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

Color Name Short Name RGB Triplet Hexadecimal Color Code Appearance
"red" "r" [1 0 0] "#FF0000" Sample of the color red
"green" "g" [0 1 0] "#00FF00" Sample of the color green
"blue" "b" [0 0 1] "#0000FF" Sample of the color blue
"cyan" "c" [0 1 1] "#00FFFF" Sample of the color cyan
"magenta" "m" [1 0 1] "#FF00FF" Sample of the color magenta
"yellow" "y" [1 1 0] "#FFFF00" Sample of the color yellow
"black" "k" [0 0 0] "#000000" Sample of the color black
"white" "w" [1 1 1] "#FFFFFF" Sample of the color white
"none" Not applicable Not applicable Not applicable No color

This table lists the default color palettes for plots in the light and dark themes.

Palette Palette Colors
"gem" — Light theme default_Before R2025a: Most plots use these colors by default._ Sample of the "gem" color palette
"glow" — Dark theme default Sample of the "glow" color palette

You can get the RGB triplets and hexadecimal color codes for these palettes using the orderedcolors and rgb2hex functions. For example, get the RGB triplets for the "gem" palette and convert them to hexadecimal color codes.

RGB = orderedcolors("gem"); H = rgb2hex(RGB);

Before R2023b: Get the RGB triplets using RGB = get(groot,"FactoryAxesColorOrder").

Before R2024a: Get the hexadecimal color codes using H = compose("#%02X%02X%02X",round(RGB*255)).

Note

Setting the ruler’s Color property also sets theTickLabelColor property to the same value. However, setting the TickLabelColor property does not change theColor property. To prevent the Color property value from overriding the TickLabelColor property value, set the Color property first, and then set theTickLabelColor property.

Tick label format and decimal precision, specified as a character vector or string. For example, you can display the tick labels in a currency format, control the number of decimals that appear in each label, or add text after all the labels.

Example: ax.XAxis.TickLabelFormat = '%g%%'; displays a percent sign after all the tick labels.

This table lists some common formats.

Common Format Character Vector to Use Examples of Resulting Format
Currency — Display dollar sign before values, use two decimal places, and use commas. '$%,.2f' 0.010.01 0.011.00 $1,000.00
Temperatures — Display degree symbol after values. '%g\\circ' 0.01o 1o 1000o
Percentages — Display percent sign after values. '%g%%' 0.01% 1% 1000%
Commas — Display commas in the thousandth place. '%,g' 0.01 1 1,000

Alternatively, specify the tick label format using the xtickformat, ytickformat, and ztickformat functions.

Custom Format

If none of the formats mentioned in the table gives your desired format, then create a custom character vector or string with identifiers.

Example of a custom format

Identifiers are optional, except the percent sign and conversion character. Construct the format in this order:

Also, you can specify literal text at the beginning or end of the format. To print a single quotation mark, use ''. To print a percent character, use %%.

Optional Flags

Identifier Description Example of Numeric Format
, Display commas every three digits, such as'1,000'. '%,4.4g'
+ Print the sign character (+) for positive values, such as '+100'. '%+4.4g'
0 Pad the field width with leading zeros instead of spaces, such as '0100'. '%04.4g'
Left-justify, which pads the end of the value with spaces instead of the beginning. For example, if the field width is 4, then this flag formats the label as '100 ' instead of ' 100'. '%-4.4g'
# For the %f,%e, and %g conversion characters, print the decimal point even when the precision is 0, such as'100.'. For%g, do not remove trailing zeros. '%#4.4g'

Conversion Characters

Identifier Description Example
d or i Signed integer with base 10. The precision value indicates the number of significant digits. '%.4d' displays π as0003.
f Fixed-point notation. The precision value indicates the number of decimal places. '%.4f' displays π as3.1416.
e Exponential notation. The precision value indicates the number of decimal places. '%.4e' displays π as3.1416x100.
g The more compact version of e orf, with no trailing zeros. The precision value indicates the maximum number of decimal places. '%.4g' displays π as3.1416.

Font

Callbacks

expand all

Axis limits changed callback, specified as one of these values:

This callback executes after the axis limits have changed, either programmatically or using an interaction such as panning within the axes. It can also execute if MATLAB changes the axis limits to encompass the range of your data when you call a plotting function.

This callback function can access specific information about the axis limits. MATLAB passes this information in a LimitsChanged object as the second argument to your callback function. If you are developing an app in App Designer, the argument is calledevent. You can query the object properties using dot notation. For example, event.NewLimits returns the new axis limits. TheLimitsChanged object is not available to callback functions specified as character vectors.

The following table lists the properties of the LimitsChanged object.

Property Description
Source Ruler object that executes the callback
EventName 'LimitsChanged'
OldLimits Two-element vector containing the previous axis limits
NewLimits Two-element vector containing the new axis limits

For more information about writing callbacks in apps, see Callbacks in App Designer.

Parent/Child

expand all

The ruler has no children. You cannot set this property.

Version History

Introduced in R2015b

expand all

The FontSmoothing property has no effect and will be removed in a future release. You can set or get the value of this property without warning, but all text is smooth regardless of the property value. This property removal was announced in R2022a.