MATLAB: Adding a legend to the bottom of a figure without changing figure width or resizing plots
Tag : matlab , By : user157138
Date : March 29 2020, 07:55 AM
With these it helps I am using Octave, rather than MATLAB, but does the following work (or at least get you closer to what you want)? % Create the figure and plot
f = figure('Position',[0 0 800 600]);
x = pi:0.01:pi;
plot(x,sin(x),x,cos(x),x,tan(x));
% Set axes and figure units to pixels, get current positions
set(f,'Units','pixels')
set(gca,'Units','pixels')
fig_pos = get(f,'position');
old_ax_pos = get(gca,'position');
% Add a legend et get its position too
h = legend('L1','L2','L3','location','southoutside');
set(h,'Units','pixels')
leg_pos = get(h,'position');
% Get the new axes position, look at how much it shifted
new_ax_pos = get(gca,'position');
pixel_shift = new_ax_pos  old_ax_pos; % y position shift is positive (axes moved up), y height shift is negative (axes got smaller)
% Make figure taller and restore axes height to their initial value
set(f,'position',fig_pos  [0 0 0 pixel_shift(4)]);
set(h,'position',leg_pos)
set(gca,'position',old_ax_pos + [0 pixel_shift(2) 0 0])
% Create a new figure without legend for comparing
f2 = figure('Position',[0 0 800 600]);
x = pi:0.01:pi;
plot(x,sin(x),x,cos(x),x,tan(x));

Figure legend is being cut off MATLAB
Date : March 29 2020, 07:55 AM
With these it helps In my experience, the legend is not manageable once the number of plots becomes so large. You could try and hack this out by hand by trying to make a compact legend my manually moving the legend lines and text boxes around individuallybut I fear this way will ultimately make you unhappy. If you want, you could start playing with the legend pieces by writing something like: hl = legend(uniqueHandles, myNames);
hlc = get(hl, 'children');
%% Setup the problem:
nd = 81;
nx = 256;
data = zeros(nx,nd);
t = linspace(0, 2*pi, nx);
%% We divide the data into 3 groups and set a color for each. This calculation then notes what group each line belongs to:
myCategory = ceil((1:nd)/27);
myColor = myColors(myCategory)';
myColors = ['r', 'b', 'g'];
myNames = {'1 to 27', '28 to 54', '55 to 81'};
%% Make a simple set of data:
randn('seed', 1982);
dt = 0;
for ind = 1:nd
dt = dt+randn/5;
data(:, ind) = sin(t+dt) + dt*cos(t);
end
%% Plot all at once:
figure(33);
clf;
h = plot(data);
%% find the unique entries and choose one representative line for the legend.
%% We then use the handles of these lines to create the legend:
lastColor = 1;
uniqueHandles = [];
for ind = 1:nd
set(h(ind), 'color', myColor(ind));
if lastColor ~= myColor(ind)
lastColor = myColor(ind);
uniqueHandles(end+1) = h(ind);
end
end
% make the legend:
legend(uniqueHandles, myNames);

I have copied one figure file from the Matlab figure editor. I have then pasted it directly into word. what is the exten
Tag : matlab , By : negonicrac
Date : March 29 2020, 07:55 AM
seems to work fine Why can't you just use the FileSave As in the MATLAB figure viewer to save as pdf, eps, or jpeg? If you have a lot of figures, then a loop like this might work. ix = 1;
for hx = findall(0, 'type', 'figure')'
figname = get(hx, 'Name');
if ~isequal(handles.appName, figname)
figure(hx);
figname = ['figure_' num2str(ix) '.jpg'];
saveas(hx, figname , 'jpg');
end
end

Matlab: flip legend order and legend overlapping plots after saving
Date : March 29 2020, 07:55 AM
To fix the issue you can do The reason for which the code provided in the answer to reverse ordering of legend colors in matlab bar plot does not work in your case is because in that case (plot of a bar chart) the object in the legend are patches while in your plot they are lines. The FaceColor only applies to patches and not to lines. p_h=get(gca,'children')
'NorthOutside' outside plot box near top
'SouthOutside' outside bottom
'EastOutside' outside right
'WestOutside' outside left
'NorthEastOutside' outside top right (default for 3D plots)
'NorthWestOutside' outside top left
'SouthEastOutside' outside bottom right
'SouthWestOutside' outside bottom left
figure
% Initial plot
h_p=plot(0:.1:2*pi,bsxfun(@plus,sin([0:.1:2*pi]),[3:3:30]'),'linewidth',3)
% Initial legend
[h_leg,b]=legend(cellstr(strcat(repmat('sin(x)+',10,1),num2str([3:3:30]'))))
%
% YOUR CODE TO GENERATE NTHE NEW COLORS
%
map = colormap; % current colormap
n = size(b,1);
z = linspace(size(map,1),1,n/3); % there is 1 text and 2 line elements for every data series, so I divide by 3
z = round(z); %otherwise matlab gets angry that indices must be real integers or logicals
MAP = map(z(:),:); % gets elements specified by linspace from colormap
%
% Reverse the legend strings
%
rev_str=flipud(get(b(1:10),'string'))
%
% Get the handles of the lines in the legend
%
b1=b(11:2:30)
%
% Revere the string in the legend
% and update the color of the lne in the plot using the colors defined in
% MAP
%
p_h=get(gca,'children')
for i=1:10
set(b(i),'string',rev_str{i})
set(p_h(i),'color',MAP(i,:),'linewidth',3)
end
%
% Reverse the color of the lines in the legend
for i=1:10
set(b1(i),'color',MAP(i,:),'linewidth',3)
end
%
% Move the legend outside the axes
%
set(h_leg,'location','NorthEastOutside')

Matlab: change order of entries in Figure legend
Date : March 29 2020, 07:55 AM
will help you If your figure was generated in R2014b or newer you can utilize the undocumented 'PlotChildren' property to manipulate the order of the legend entries without requiring a new legend call. For example: x = 1:10;
y1 = x;
y2 = 2*x;
y3 = 3*x;
y4 = x.^2;
plot(x, y1, x, y2, x, y3, x, y4);
lh = legend('y = x', 'y = 2*x', 'y = 3*x', 'y = x.^2');
neworder = [3, 1, 4, 2];
lh.PlotChildren = lh.PlotChildren(neworder);
% Handle to figure object known
lg = findobj(figureobj, 'Type', 'legend');
% Handle to figure object unknown
lh = findobj(gcf, 'Type', 'legend');

