2

I've created a histogram with multiple sets of data. The data sets are color imgages converted to grayscale and taken over a given time period (e.g. pic 1 @ time=0, pic 2 @ time=5min, etc.) which is why I need the legend entries to show up in a specific order. When I put the legend in, the entries are scattered around in no specific order and I can't figure out how to get the entries switched up the way I need them.

user2650768
  • 71
  • 2
  • 2
  • 3

6 Answers6

6

In 2017a and higher (using part of the answer by Peter M above)

  1. Select "Edit Plot" with the standard arrow button in the toolbar. This will allow you to select plot lines.
  2. Select the plot line you want to appear first in the legend.
  3. Use Ctrl-X (or whatever on OS X,Linux) to "Cut" the Plot line.
  4. Create a new figure (File -> New -> Figure).
  5. Use Ctrl-V (or whatever on OS X, Linux) to "Paste" the Plot line in the new figure.
  6. "Cut" the Plot line in the new figure.
  7. "Paste" the Plot line in the original figure.
  8. Repeat steps 2-6 for every plot line, in the order that you want them to appear in the legend. Right click on the legend and "Refresh"
Rami
  • 61
  • 1
  • 1
3

If you are regenerating the plot often of course it is probably a good idea to make sure that the script puts them in the correct order. Luis Mendo provided an answer here, but his answer to a slightly different wording was a little more detailed: how to change the sequence of the legend.

Pre 2017a Only! It seems that 2017a breaks this behavior, so the following trick does not work in the latest versions.

To answer your specific question, here is a neat trick if you are just doing this once in awhile on a plot that doesn't have many lines, and only want to use the Figure Editor...

  1. Select "Edit Plot" with the standard arrow button in the toolbar. This will allow you to select plot lines.
  2. Select the plot line you want to appear first in the legend.
  3. Use Ctrl-X (or whatever on OS X, Linux) to "Cut" the Plot line.
  4. Use Ctrl-V (or whatever on OS X, Linux) to "Paste" the Plot line.
  5. Repeat steps 2-4 for every plot line, in the order that you want them to appear in the legend.
  6. Right click on the legend and "Refresh"
Peter M
  • 1,918
  • 16
  • 24
  • This seems to have stopped working somewhere around R2017a. Now if I cut and paste the lines are pasted in their original order, so nothing changes. Is there a workaround? – Emil Jul 12 '17 at 17:45
  • Interesting, I don't know, but this kinda *was* the workaround. I don't do this often, and I had some trouble with my scripts due to some other annoying changes in 2017a, so I uninstalled it and went back to 2016b. – Peter M Jul 13 '17 at 18:31
0

You can get a handle to each plotted object, and in the legend use the handles to control which string applies to which plotted object.

Example:

h1 = plot(1:5, 1:5, 'r');
hold on
h2 = plot(1:5, 2:6, 'b');
legend([h1 h2],'First red','Second blue')
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
0

The answer above also works for R2017a and higher...here is a little bit more general example:

x = 1:10;
y1 = x;
y2 = 2*x;
y3 = 3*x;
y4 = x.^2;
figure
plot(x, y1, x, y2, x, y3, x, y4);
lh = legend('y = x', 'y = 2*x', 'y = 3*x', 'y = x.^2');

legendEntries = get(gca,'legend')
plotHandles = get(gca,'children')
legendEntries = legendEntries.String;
newOrder = [3,4,1,2];
legend([plotHandles(newOrder)],legendEntries{newOrder})
0

flipud(h) or fliplr(h) to change complete order of the array up-down oder left-right

seb
  • 1
0

Through try and error I found this easy fix:

1) select the plot line that should be at position 1 (at the top) 2) Strg + X 3) Strg + V in the same plot figure

Repeat step 2 and 3 about 3 or 4 time. This will result in the plot beeing at the bottom of the legend. Now select the plot that should be at the 2nd position, again Step 2 and 3 a couple times. The previous plot moves one place up, the 2nd picked plot is now at the bottom.... continue with the rest of the plots the same way