MATLAB Graphics and App Building

MATLAB graphics, data visualization, and app building

Exploring Secondary Axis Labels in MATLAB

One of our missions for this blog is to boost our readers' familiarity with the latest features and to keep you in the forefront of MATLAB graphics and app building advancements. Last week I mentioned that MATLAB R2023b has more than two dozen new features in the graphics and app building domains. Help us achieve our goal by letting us know which new features you would like to see covered in the blog!
One of my favorite features from R2023b is a trio of convenience functions that control secondary labels on Cartesian axes.
You might be wondering, what are secondary labels?
Secondary labels are designed to display additional information about an axis such as tick label units. By default, MATLAB adds a secondary label next to an axis when plotting datetime values, duration values, or numeric values in exponential form. The use of secondary labels often simplifies tick labels by eliminating repetitive information such as including the same year in all datetime tick labels. Here are some automatically generated secondary labels for numeric, duration, and datetime rulers.
To generate a datetime example similar to the purple axes above,
date1 = datetime(2023,09,13,0,3,0) + seconds(-45:5:45);
date2 = datetime(2023,10,28,0,0,0) + days(-45:5:45);
plot(date1, date2, "o");
axis tight

Controlling secondary labels in MATLAB R2023b

Here are some common situations reported by MATLAB users where these functions will come in handy.
Remove a secondary label. Time-of-day is the focus of your data and you don't want to show the date in the purple axes above. Remove the secondary label using,
xsecondarylabel(Visible = "off")
Change a secondary label. In your manuscript, "hr" is used to indicate "heart rate" and you want to change "hr" in the orange axes above to "hours". Change the secondary label using,
Add your own secondary label. You're working with scaled data such that the values 0.5 and 1.0 should be interpreted as five million and ten million. Add the exponent notation shown in the blue axes above using,
Label axis units. This is my favorite reason to use these new functions. Label the axes' units without relying on the xlabel or ylabel. Let's look at a full demo below.

Demo: using secondary labels to indicate axis units

In 1662 Robert Boyle discovered that the volume of an ideal gas is inversely proportional to the pressure exerted upon the gas assuming constant temperature (Boyle's Law). Graphical visualizations were not yet common in 1662 so Boyle published his results as a table of numbers. Let's plot the results to show the relationship between pressure and volume for ideal gases.
Read in Boyle's data from 1662 using Boyle's original generic variable names. We'll use columns A2 (gas volume) and D (pressure). For a complete explanation of the table variables, see J. B. West (1999, J Appl Physiol).
T = readtable("")
T = 25×6 table
A A2 B C D E
1 48 12 0 29.125 29.125 29.125
2 46 11.5 1.4375 29.125 30.562 30.375
3 44 11 2.8125 29.125 31.938 31.75
4 42 10.5 4.375 29.125 33.5 33.143
5 40 10 6.1875 29.125 35.312 35
6 38 9.5 7.875 29.125 37 36.789
7 36 9 10.125 29.125 39.312 38.875
8 34 8.5 12.5 29.125 41.625 41.118
9 32 8 15.062 29.125 44.188 43.688
10 30 7.5 17.938 29.125 47.062 46.6
11 28 7 21.188 29.125 50.312 50
12 26 6.5 25.188 29.125 54.312 53.769
13 24 6 29.688 29.125 58.812 58.25
14 23 5.75 32.188 29.125 61.312 60.783
Boyle trapped gas between the end of a j-shaped tube and a column of mercury. He measured pressure upon the gas in units of inHG or inches of mercury by gradually adding mercury to the tube which increases the pressure on the gas. Volume of the gas was measured by the height of the gas pocket at the end of the tube, in inches. I dug deep searching for the diameter of his tube so we could use actual units of volume but couldn't find it. Inches will suffice assuming the tube had a uniform diameter. A shout-out is waiting for anyone who can find Boyle's tube diameter from a credible source!
Boyle treated volume as the independent variable (table variable A2) so we'll plot volume along the x-axis and pressure (table variable D) on the y-axis.
plot(T.A2, T.D, "-s")
grid on
title("Boyle's Data")
Add secondary labels to indicate axis units (starting in MATLAB R2023b).
Boyle's data shows that the volume of the gas is inversely proportional to the pressure exerted upon it. As pressure falls, volume increases like a balloon expanding in a vacuum. This relationship is beautifully shown by plotting the reciprocal of pressure against volume.
plot(T.A2, 1./T.D, "-o");
grid on
xlim([0, 15])
ylim([0, 0.04])
title("Boyle's Law")
ylabel("Inverse of pressure")

Join the conversation!

We've learned how to change, create, or remove a secondary label. Let's continue the discussion in the comments section below!
  • What other customizations would be helpful with secondary labels?
  • What other recent new features in graphics and app building are you interested in learning more about?
  • Do you think Robert Boyle's interest in pressure and volume was affected by being the 14th child in his family?


  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.