Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the order of lines in a Matlab figure?

Given a plot of three curves in a .fig file I'd like to add another plot (with hold all and plot), but put it behind one of the already existing curves (i.e. make sure the last original curve stays the foreground one). Can this be achieved without having to extract the plot data and re-plotting?

like image 258
Tobias Kienzler Avatar asked Oct 06 '11 13:10

Tobias Kienzler


People also ask

How do you bring a figure to the front in MATLAB?

Running MATLAB with the –nojvm option, the figures are created as X11 windows. So you can bring them to the front, as you had experienced previously, by double clicking the X11 icon in the Dock. In this case, it brings the current figure to the front, but you can use any available figure handle in place of “gcf”.

How do you organize a legend in MATLAB?

By default the order of the lines in the legend is the same as they are plotted. The easiest way is to just plot them in the order you want the legend to appear. If that is not feasible, then you can change the order of the lines as they are stored in the 'Children' property of the axes.


3 Answers

If you know the handle of line you want on top (e.g. because you called h = plot(...), you can use uistack

uistack(h,'top') 

Alternatively, you can manipulate the order of children of your current axes directly. The following puts the last-most curve on top.

chH = get(gca,'Children') set(gca,'Children',[chH(end);chH(1:end-1)]) 
like image 52
Jonas Avatar answered Sep 21 '22 22:09

Jonas


The resolution given by @Jonas using 'Children' property does not work in its given format. It should be modified as follows:

chH = get(gca,'Children') set(gca,'Children',flipud(chH)) 
like image 44
seyyed ali pourmousavi Avatar answered Sep 19 '22 22:09

seyyed ali pourmousavi


When the image has a legend, the get(gca,...) and set(gca,...) pair result in an error: "Error using set. Children may only be set to a permutation of itself" In that case, I used the GUI select tool of the figure to select the axes objects, then get and set work only with the plots as required and not the legend as well. After calling set, you have to refresh the legend by calling legend(...). I had 5 plots that I needed to reorder. When unsure about the order, permute plots two at a time, refresh the legend and see if that is the order you wanted

like image 21
Hazem Avatar answered Sep 20 '22 22:09

Hazem