Specify x-axis tick label format (2024)

Specify x-axis tick label format

collapse all in page

Syntax

xtickformat(fmt)

xtickformat(datefmt)

xtickformat(durationfmt)

xtickformat(ax,___)

xfmt = xtickformat

xfmt = xtickformat(ax)

Description

example

xtickformat(fmt) setsthe format for numeric x-axis tick labels. Forexample, specify fmt as 'usd' todisplay the labels in U.S. dollars.

example

xtickformat(datefmt) setsthe format for labels that show dates or times. For example, specify datefmt as 'MM-dd-yy' todisplay dates such as 04-19-16. This option appliesonly to an x-axis with datetime values.

example

xtickformat(durationfmt) setsthe format for labels that show durations. For example, specify durationfmt as 'm' todisplay the durations in minutes. This option applies only to an x-axiswith duration values.

example

xtickformat(ax,___) usesthe axes specified by ax instead of the currentaxes. Specify ax as the first input argument.

example

xfmt = xtickformat returnsthe format style used for x-axis tick labelsof the current axes. Depending on the type of labels along the x-axis, xfmt isa character vector of a numeric format, date format, or duration format.

xfmt = xtickformat(ax) returnsthe format style used for the axes specified by ax insteadof the current axes.

Examples

collapse all

Display Tick Labels in Dollars

Open Live Script

Display x-axis tick labels in dollars with and without decimal values.

Create a bar chart. Display the tick labels along the x-axis in dollars.

x = 0:20:100;y = [88 67 98 43 45 65];bar(x,y)xtickformat('usd')

Specify x-axis tick label format (1)

Query the tick label format. MATLAB® returns the format as a character vector containing the formatting operators.

fmt = xtickformat

Display the tick labels with no decimal values by tweaking the numeric format to use a precision value of 0 instead of 2.

xtickformat('$%,.0f')

Specify x-axis tick label format (2)

Display Text After Each Tick Label

Open Live Script

After creating a line chart, display the tick labels along the x-axis with the text "GHz" after each value.

x = 1:10;y = [.17 .25 .27 .28 .3 .32 .33 .34 .345 .35];plot(x,y,'-V')xtickformat('%g GHz')

Specify x-axis tick label format (3)

Control Number of Decimals Displayed

Open Live Script

After creating a scatter plot, display the x-axis tick labels with two decimal places. Control the decimal places by passing xtickformat a character vector of a numeric format that uses fixed-point notation for the conversion character and a precision value of 2.

x = linspace(0,1,100);y = randn(1,100).*cos(x);scatter(x,y)xtickformat('%.2f')

Specify x-axis tick label format (4)

Format for Date Tick Labels

Open Live Script

Create a line plot with datetime values along the x-axis. Then, change the format of the dates.

t = datetime(2014,6,28) + caldays(1:10);y = rand(1,10);plot(t,y);xtickformat('MM-dd')

Specify x-axis tick label format (5)

Format for Duration Tick Labels

Open Live Script

Create a line plot with duration values along the x-axis. Then, change the format of the tick labels.

t = 0:seconds(30):minutes(3);y = rand(1,7);plot(t,y)xtickformat('mm:ss')

Specify x-axis tick label format (6)

Query Current Tick Label Format

Open Live Script

Create a scatter plot and display the x-axis tick labels in Euro. Then, query the tick label format. MATLAB® returns the format as a character vector containing the formatting operators.

x = rand(100,1);y = rand(100,1);scatter(x,y)xtickformat('eur')

Specify x-axis tick label format (7)

xfmt = xtickformat
xfmt = '\x20AC%,.2f'

Tick Label Format for Specific Axes

Open Live Script

Starting in R2019b, you can display a tiling of plots using the tiledlayout and nexttile functions. 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. Plot into each of the axes. Specify the tick label format for the x-axis of the lower plot by specifying ax2 as the first input argument to xtickformat. Display the tick labels in U.S. dollars.

tiledlayout(2,1)ax1 = nexttile;plot(ax1,rand(6))ax2 = nexttile;plot(ax2,rand(6))xtickformat(ax2,'usd')

Specify x-axis tick label format (8)

Input Arguments

collapse all

fmtFormat for numeric tick labels
'%g' (default) | character vector | string scalar

Format for numeric tick labels, specified as a character vector or string scalar. You can specify one of the formats listed in this table. Alternatively, you can specify a custom format.

Predefined FormatDescription
'usd'

U.S. dollars. This option is equivalent using '$%,.2f'. If the labels use scientific notation, this option sets the exponent value to 0.

'eur'

Euro. This option is equivalent to using '\x20AC%,.2f' with an exponent value of 0.

'gbp'

British pound. This option is equivalent to using '\x00A3%,.2f' with an exponent value of 0.

'jpy'

Japanese yen. This option is equivalent to using '\x00A5%,d' with an exponent value of 0.

'degrees'

Display degree symbol after values. This option is equivalent to using '%g\x00B0' with the default exponent value.

'percentage'

Display percent sign after values. This option is equivalent to using '%g%%' with the default exponent value.

'auto'

Default format of '%g' with the default exponent value.

Example: xtickformat('usd')

Custom Numeric Format

You can specify a custom numeric format by creating a character vector or string containing identifiers.

Specify x-axis tick label format (9)

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

  • One or more flags — Options such as adding a plus sign before positive values. For a full list of options, see the table of Optional Flags.

  • Field width — Minimum number of characters to print in the tick label. Specify the field width as an integer value. If the number of significant digits in the tick value is smaller than the field width, then the label is padded with spaces.

  • Precision — Number of digits to the right of the decimal point or the number of significant digits, depending on the conversion character. Specify the precision as an integer value.

  • Conversion character — Value type. For a full list of options, see the table of Conversion Characters. If you specify a conversion that does not fit the data, then MATLAB® overrides the specified conversion, and uses %e.

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 %%.

Example: xtickformat('%.2f') displays the values using fixed-point notation with two decimal places.

Example: xtickformat('$%.2f') displays a dollar sign before each value.

Example: xtickformat('%.2f million') displays million after each value.

Optional Flags

IdentifierDescriptionExample 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'
0Pad 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

IdentifierDescriptionExample
d or iSigned integer with base 10. The precision value indicates the number of significant digits. '%.4d' displays π as 0003.
fFixed-point notation. The precision value indicates the number of decimal places.'%.4f' displays π as 3.1416.
eExponential notation. The precision value indicates the number of decimal places.'%.4e' displays π as 3.1416x100.
gThe more compact version of e or f, with no trailing zeros. The precision value indicates the maximum number of decimal places.'%.4g' displays π as 3.1416.

datefmtFormat for dates and times
'auto' | character vector | string scalar

Format for dates and times, specified as 'auto', a character vector, or a string scalar. The default format is based on the data.

Example: xtickformat('yyyy-MM-dd') displaysa date and time such as 2016-04-19.

Example: xtickformat('eeee, MMMM d, yyyy HH:mm:ss') displaysa date and time such as Saturday, April 19, 2016 21:41:06.

Example: xtickformat('MMMM d, yyyy HH:mm:ss Z') displaysa date and time such as April 19, 2016 21:41:06 -0400.

The display format tables show the letter identifiers that you can use to construct the format. To separate the fields, you can include nonletter characters such as a hyphen, space, colon, or any non-ASCII characters. The identifiers correspond to the Unicode® Locale Data Markup Language (LDML) standard for dates.

Date and Time Formats

Use these identifiers to specify the display formats of thedate and time fields.

Letter IdentifierDescriptionDisplay
GEraCE
yYear, with no leading zeros. See the Note that follows thistable.2014
yyYear, using last two digits. See the Note that follows thistable.14
yyy, yyyy ...Year, using at least the number of digits specified by thenumber of instances of 'y'For the year 2014, 'yyy' displays 2014,while 'yyyyy' displays 02014.
u, uu, ...ISO year. A single number designating the year. An ISO yearvalue assigns positive values to CE years and negative values to BCEyears, with 1 BCE being year 0.2014
QQuarter, using one digit2
QQQuarter, using two digits02
QQQQuarter, abbreviatedQ2
QQQQQuarter, full name2nd quarter
MMonth, numerical using one or two digits4
MMMonth, numerical using two digits04
MMMMonth, abbreviated nameApr
MMMMMonth, full nameApril
MMMMMMonth, capitalized first letterA
WWeek of the month1
dDay of the month, using one or two digits5
ddDay of the month using two digits05
DDay of the year, using one, two or three digits95
DDDay of the year using two digits95
DDDDay of the year using three digits095
eDay of the week, numerical using one or two digits.7, where Sunday is the first day of theweek.
eeDay of the week, numerical using two digits07
eeeDay, abbreviated nameSat
eeeeDay, full nameSaturday
eeeeeDay, capitalized first letterS
aDay period (AM or PM)PM
hHour, 12-hour clock notation using one or two digits9
hhHour, 12-hour clock notation using two digits09
HHour, 24-hour clock notation using one or two digits21
HHHour, 24-hour clock notation using two digits21
mMinute, using one or two digits41
mmMinute, using two digits41
sSecond, using one or two digits6
ssSecond, using two digits06
S, SS, ..., SSSSSSSSSFractional second, using the number of digits specified bythe number of instances of 'S' (up to 9 digits).'SSS' truncates 6.12345 seconds to 123.

Some tips and considerations:

  • Use one or more u characters insteadof y characters to represent the year when workingwith year numbers near zero.

  • Datetime values later than 144683 years CE or before140743 BCE display only the year numbers, regardless of the specifiedformat value.

Time Zone Offset Formats

Use these identifiers to specify the display format of the timezone offset. A time zone offset is the amount of time that a specificdate and time is offset from UTC. A time zone offset is differentfrom a time zone in that it comprises rules that determine the offsetsfor specific times of the year. Include a time zone offset identifierwhen you want to ensure that the time components are displayed unambiguously.

Letter IdentifierDescriptionDisplay
zAbbreviated name of the time zone offset. If this value isnot available, then the time zone offset uses the short UTC format,such as UTC-4.EDT
ZISO 8601 basic format with hours, minutes, and optional secondsfields.-0400
ZZZZLong UTC format.UTC-04:00
ZZZZZISO 8601 extended format with hours, minutes, and optionalseconds fields. A time offset of zero is displayed as the ISO 8601UTC indicator “Z”.-04:00
x or XISO 8601 basic format with hours field and optional minutesfield. If you specify X, a time offset of zerois displayed as the ISO 8601 UTC indicator “Z”.-04
xx or XXISO 8601 basic format with hours and minutes fields. If youspecify XX, a time offset of zero is displayedas the ISO 8601 UTC indicator “Z”.-0400
xxx or XXXISO 8601 extended format with hours and minutes fields. Ifyou specify XXX, a time offset of zero is displayedas the ISO 8601 UTC indicator “Z”.-04:00
xxxx or XXXXISO 8601 basic format with hours, minutes, and optional secondsfields. If you specify XXXX, a time offset of zerois displayed as the ISO 8601 UTC indicator “Z”.-0400
xxxxx or XXXXXISO 8601 extended format with hours, minutes, and optionalseconds fields. If you specify XXXXX, a time offsetof zero is displayed as the ISO 8601 UTC indicator “Z”.-04:00

durationfmtFormat for duration values
character vector | string scalar

Format for duration values, specified as a character vector or string scalar.

To display a duration as a single number that includes a fractionalpart (for example, 1.234 hours), specify one of the following formats.

Format Description
'y'Number of exact, fixed-length years. A fixed-length year isequal to 365.2425 days.
'd'Number of exact, fixed-length days. A fixed-length day is equalto 24 hours.
'h'Number of hours
'm'Number of minutes
's'Number of seconds

To display a duration in the form of a digital timer, specifyone of these formats:

  • 'dd:hh:mm:ss'

  • 'hh:mm:ss'

  • 'mm:ss'

  • 'hh:mm'

In addition, you can display up to nine fractionalsecond digits by appending up to nine S characters.For example, 'hh:mm:ss.SSS' displays the millisecondsof a duration value to three digits.

axTarget axes
Axes object | array of Axes objects

Target axes, specified as an Axes object or an array of Axes objects.

If you do not specify this argument, then xtickformat modifies the current axes.

Algorithms

The xtickformat function sets and queriesthe TickLabelFormat property of the ruler objectassociated with the x-axis.

Version History

Introduced in R2016b

See Also

Functions

  • xticks | xticklabels | xlim | xsecondarylabel | xtickangle

Properties

  • NumericRuler Properties | DatetimeRuler Properties | DurationRuler 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.

Specify x-axis tick label format (10)

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

Specify x-axis tick label format (2024)
Top Articles
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 6006

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.