logo
down
shadow

How can I order items in a Matlab legend via the figure editor?


How can I order items in a Matlab legend via the figure editor?

Content Index :

How can I order items in a Matlab legend via the figure editor?
Tag : matlab , By : arbeitandy
Date : November 24 2020, 03:41 PM


Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

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


Tag : matlab , By : Mike
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 individually--but 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 File|Save 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


Tag : matlab , By : Stuka
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 3-D 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


Tag : matlab , By : beng
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');
Related Posts Related QUESTIONS :
  • How to get the total sum of several function calls in a double loop
  • MATLAB: Problem calling builtin('addlistener', ...)
  • NN training with Matlab: train() function output is not consistent with MSE error
  • Efficiently find Unique triplets of three char vectors in MATLAB
  • Make vector of elements less than each element of another vector
  • How to overlap a point cloud file with another plot in Matlab?
  • How to compare 2 matrices and keep greatest values in Matlab
  • Colour line plot by categorical data in MATLAB
  • how to do 3D number-density scatter plot using given data?
  • Obtain a table of proper size from a csv in Matlab
  • How to prevent the legend from updating in R2017a and newer?
  • How to replace groups of characters between flags in MATLAB
  • Is there an alternative of and() in Matlab that does not check syntax and returns a false as soon as the first false is
  • while loop continuously executing
  • How to empty a cell
  • How to use nested for loop with if else in the code to make it smaller
  • Histogram of an image
  • How to compute confidence intervals and plot them on a bar plot
  • How can I use dialog box in the mask editor to make my constant change?
  • Send data to serial port in little endian
  • I have a problem with this code. some idea?
  • Calculating Normalized Root Mean Square Error
  • matlab: get variable creation time
  • Breaking 64 figures into a set of subplots
  • How to fill 3d array with contents from two other 3d arrays with unequal first and second dimensions
  • Average of subgroup of 2nd column, grouped by 1st column
  • Polymorphic Return Types in MATLAB
  • (In Matlab) How would I switch to another working directory in the middle of a for loop?
  • How to calculate distance from cluster centroids from the outputs of Matlab kmean() function
  • How can I force a variable in matlab to be a column vector
  • Looping Sortino Ratio in Matlab
  • Plot Command Does Not Show Anything
  • Creating an axis of months
  • how to covert 1/4 elements of a matrix to zero
  • How to check if an axes handle is cleared or not
  • Generating smaller video file size from frames
  • how to edit simulink plutosdr qpsk example
  • Get rectangle coordinates from a point and a line
  • after user had entered value update number to include commas
  • How to replace characters between flags in MATLAB
  • How to select from two identical bus signals in Simulink?
  • Difference between fspecial('sobel') and edge(i,'sobel') matlab
  • Insert a new line in listbox on Matlab GUIDE
  • creating url with sprintf creates wrong url
  • How to keep new symbolic value for subs remain through Matlab Code
  • I would like to beable to stack rows of one matrix in a special way
  • Two minimum values in matlab
  • MATLAB reconstruction of FFT 1D with an image
  • How to plot multiple figures in a for loop in matlab
  • Matlab allow access to folder for mkdir
  • Close Stateflow editor programmatically
  • mex error during vl_compilenn
  • How to obtain a surface plot of f(x, y, z) = c
  • Give certain values specific color in matrix with imagesc
  • MATLAB / Octave - how to parse CSV file with numbers and strings that contain commas
  • Looping through documents in matlab
  • converting a string array into a struct
  • Colormap three colors
  • plot3 for display three dimension data in matlab
  • Matlab: can not simplify a symbolic scalar function use subs() in a simple case
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com