# Renaming y-axis of stackedplot in MATLAB

3 views (last 30 days)
Vicky Ngo on 29 Sep 2021
Commented: Vicky Ngo on 30 Sep 2021
Hi, here I just import some stock prices for Apple (which is inside the Apple.mat file in the first line) and do a few calculations in MATLAB. I plot stock return vs time using stackedplot, however I can't figure out how I can rename the y-axis to something else rather than Column1. Can anyone help me with that? (sorry this sounds like an amateur question).
I attached the pic of the graph as well as the Apple.mat table here. Thank you so much!
% Name of loaded table is AppleDailyPriceHistoryTable
% 'Ascending' sorts dates from oldest to newest (2017-2021)
% 'Descending' sorts dates from newest to oldest (2021-2017)
% Either sort the table in ascending order using the code below or manually in
% Apple_descending=sortrows(Apple_ascending,1,'descend');
t = AppleDailyPriceHistoryTable{:,1}; % extract first column = dates
st = AppleDailyPriceHistoryTable{:,2}; % extract second column = prices
STplot = stackedplot(t,st);
STplot.XLabel = 'Exchange Date';
% Calculate rt. (Extract from the table will return a cell array, which can't
% be used for numerical calculations).
% rt is in decimal form, not percentage form.
for i=1:1:(length(st)-1)
rt(i,1)=st(i)/st(i+1)-1;
end
dt = 1/12;
mean = sum(rt)/(length(st)-1);
mu = mean/dt;
sd = std(rt);
sigma = sqrt(1/dt)*sd;
% mean = 0.001565 = 0.1565%
% sd = 0.020558
% sigma = [0.0712144108966816]
% mu = [0.0187856732853678]

Dave B on 29 Sep 2021
(I'm not entirely sure why you're using stackedplot if you only have one plot, you might consider just using 'plot' but...)
You can use the DisplayLabels property on a stackedplot to set the labels:
s=stackedplot(rand(30,3));
s.DisplayLabels={'Apple' 'Banana' 'Microsoft'}; Vicky Ngo on 30 Sep 2021
Thank you so much, this works flawlessly!

### Categories

Find more on Line Plots in Help Center and File Exchange

R2021a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!