Latexlabels and Legend Location in Stackedplot
23 views (last 30 days)
Show older comments
I want my y-labels to be interpreted by latex and I want to make my Legend horizontal above the plot. Any Advice? (Matlabversion R2018b)
MWE:
%Settings
xlab = '$x$';
ylab = {'$h_0(x)$','$g(x)$'};
colors = {[0 0 0;0 0 0;0 0 0],[1 0 0]};
lt = {{'-',':','--'},{'-'}};
fn = 'times'; fs = 20;%font setting
lw = 3; %Linewidth setting
legendlabels= {{'1','2','3'},{''}};
%Create Date for first subplot
dat=createData();
%Create data for second plot
x=-25:0.2:25; x=x';
g = sin(2*pi*x/50);
dat=addvars(dat,g); %Add to data table
%Plot
s=plotStack(dat,lw,fs,fn,colors,lt,xlab,ylab,legendlabels);
%%
function dat=createData()
x=-25:0.2:25; x=x';
dat = table();
dat = addvars(dat,x);
datarow = zeros(length(x),3);
for i=1:3
datarow(:,i) = cos(2*pi*i*x/50);
end
dat =addvars(dat, datarow,'NewVariableNames',{'data'});
end
function [s]=plotStack(dat,lw,fs,fn,colors,linetypes,xlabel,ylabels,legendlabels)
%plots a stackedplot from dat and sets Linewidth lw, Fontsize fs, Fontname
%fn, Markersize ms, Markercolor mc, colors and linetypes for all the lines.
%Returns the plot for further adaptions
s=stackedplot(dat,'XVariable','x','Linewidth',lw,'Fontname',fn,'Fontsize',fs);
%Labels
s.XLabel = xlabel;
s.DisplayLabels=ylabels;
%Change Individual Lines
lp = s.LineProperties;
for i=1:length(lp)
lp(i).Color=colors{i};
lp(i).LineStyle=linetypes{i};
end
%Change Axes Properties such as LegendProperties and y limits
ax = s.AxesProperties;
for i=1:length(ax)
ax(i).LegendLabels=legendlabels{i};
end
end
0 Comments
Answers (1)
Adam Danz
on 19 Nov 2020
> I want my y-labels to be interpreted by latex
> I want to make my Legend horizontal above the plot.
Well.... it's a little messy. You can access some of the legend properties from the output handle to stackedplot but not all of them. Here's an undocumented approach which will produce a warning. It gets the actual legend handes. You'll have to play around with the legend position (last line).
T = array2table(rand(40,4));
h = stackedplot(T,{{'Var1','Var2'},'Var3','Var4'});
set(h.AxesProperties(1), 'LegendVisible', 'on');
legs = getfield(struct(h),'LegendHandle');
legs(1).Orientation = 'Horizontal';
set(h.AxesProperties(1), 'LegendLocation', 'North'); % see note [1]
% legs(1).Location = 'northoutside'; % see note [2]
legs(1).Units = 'Normalize';
legs(1).Position = [.40, sum(h.Position([2,4])), .2, .05];
Note [1] : 'NorthOutside' is not an option with stackedplot
Note[2]: 'NorthOutside', just moved the legend to "north" in stackedplot
0 Comments
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!