how to change the sequence of the legend - matlab

I want to change the sequence of the legend.
See the figure. I want the sequence to be: green and data2, blue and data3, black and data4, red and data1.
Could anyone give a demo?

Change the order in which the plots are added to the figure, and then call legend normally. That should do it.
You can also do it as follows. First get handles to the individual plots:
h1 = plot(1:5);
hold on
h2 = plot(11:15, 'r');
Then call legend specifying the order:
legend([h1 h2],'plot1','plot2')
or
legend([h2 h1],'plot2','plot1')

In case, you have already made the plots or if you added some plot at the end, which want to reorder to somewhere in middle, you can try this way:
1) Go to show Plot Tools and Dock Figure.
2) Delete the data (which you want to move to bottom). Then undo delete.
3) Refresh legend.

Related

Plot hidden under another plot in Matlab

I am creating a candlestick chart representing stock prices. Once created, I want to add green circle showing where/when I am buying the stock.
hold on;
candle(myData.High, myData.Low, myData.Close, myData.Open, '', myData.Date, 'dd/mm/yy');
m = plot(myExecutionTable.BuyDate,myExecutionTable.BuyPrice,'og')
uistack(m)
hold off;
The problem is that if myExecutionTable.BuyPrice has a value between the Open and Close, the circle is not showing up. I guess it is hidden under the candlestick. Hence I tried to use uistack but without success. When I change to
plot(myExecutionTable.BuyDate,myExecutionTable.BuyPrice+100,'og')
the green circle then appears (above the candlestick)
Thanks,
Serge
The easiest way to make sure one graphics object is on top of another (and not below), is to plot it later.
If for some reason you can't do it this way, you can also manipulate the order of the child objects of the axes:
h = get(gca, 'Children');
returns a vector of graphics handles. Exchange handles between positions in this vector (higher index means higher on top), and then write it back using
set(gca, 'Children', h)

Remove Objects from Legend When You Have Also Used Fit, Matlab

I've plotted data points and fitted an exponential curve between them using 'fit' in Matlab. The problem is that with the fit-function I got the fitted line I wanted to plot, but also extra markers on top of my regular markers. I solved this problem by plotting the wanted markers on top of the unwanted so that they couldn't be seen. Now to the problem. When I want to show the legends those dots are also in there. How can I remove the markers from the legend without removing the fitted line since both of them are hidden inside the fit-function? Can I stop 'fit' from plotting the unwanted markers? So, I want to remove the blue dot called 'hoff' in the picture below.
You can leave out legend-entries by manually leaving out the handles of the lines, that you dont want to be in the legend. Try this:
%if you plot something, that you want showing up in the legend, save its handle:
h_line = plot(x,y)
%you dont want to show up this line? dont do anything, just plot it:
plot(myMarker)
%then set the legend-> you can add the text for your legend-entries with
%a cell-array containing the strings you want to show up:
legend([h_line another_line],{'Text1' 'Text2'})
with the example (see comments) I came to this solution:
close all
X=[1:10];
Y=X*0.5+0.1;
ft = fittype('poly2');
f = fit(X', Y',ft);
ha=plot(f)
hold on
hc=plot(X,Y)
hb=errorbar(X, Y, X*0.1, 'squarek','MarkerFaceColor','k','markersize',5)
hleg1 = legend([ha hc],{'hnh','ghg'});
-> this is just about splitting the plot-command. Hope that helps...
the result should look like this:

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

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.
In 2017a and higher (using part of the answer by Peter M above)
Select "Edit Plot" with the standard arrow button in the toolbar. This will allow you to select plot lines.
Select the plot line you want to appear first in the legend.
Use Ctrl-X (or whatever on OS X,Linux) to "Cut" the Plot line.
Create a new figure (File -> New -> Figure).
Use Ctrl-V (or whatever on OS X, Linux) to "Paste" the Plot line in the new figure.
"Cut" the Plot line in the new figure.
"Paste" the Plot line in the original figure.
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"
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...
Select "Edit Plot" with the standard arrow button in the toolbar. This will allow you to select plot lines.
Select the plot line you want to appear first in the legend.
Use Ctrl-X (or whatever on OS X, Linux) to "Cut" the Plot line.
Use Ctrl-V (or whatever on OS X, Linux) to "Paste" the Plot line.
Repeat steps 2-4 for every plot line, in the order that you want them to appear in the legend.
Right click on the legend and "Refresh"
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')
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})
flipud(h) or fliplr(h) to change complete order of the array up-down oder left-right
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

matlab remove only top and right ticks with leaving box on

In Matlab figure, I would like to remove ticks only from the top and right axes with keeping the plot box on.
I know if I make the plot box off, the ticks on the top and right go away. But, this is not what I want. In other words, I want to keep ticks only at the bottom and left and, at the same time, want to keep the plot box on.
My workaround similar to #j_kubik proposition:
plot(1:10)
% get handle to current axes
a = gca;
% set box property to off and remove background color
set(a,'box','off','color','none')
% create new, empty axes with box but without ticks
b = axes('Position',get(a,'Position'),'box','on','xtick',[],'ytick',[]);
% set original axes as active
axes(a)
% link axes in case of zooming
linkaxes([a b])
You can use box off to remove the ticks and then draw the box back using plot. For example:
figure
hold on
box off
plot(1:10)
plot([1,10],[10, 10],'k')
plot([10,10],[1,10],'k')
Now in 2022, if anyone is still interested in a quick resolution other than the box option, here is my answer:
figure
plot(1:10) ;
ax = gca ;
ax.Box = 'off' ;
xline(ax.XLim(2),'-k', 'linewidth',ax.LineWidth);
yline(ax.YLim(1),'-k', 'linewidth',ax.LineWidth);

Matlab image display in specific old figures

Take an example, I have 2 a sequences of left and right images: left01, right01, left02, right02, etc. How can I display those on only two figures: left and right. Each loop, these 2 figures will be updated with leftxx and rightxx.
"Hold on" will only hold the current figure. Creating figures with figure('Name', 'Left') will not do the trick, as multiple "Left" figures will be created. Yet imshow() does not let me specify the figure to display based on its name.
What I want is something similar to OpenCV, which let you choose which figure (already opened) to display
cvNamedWindow("Left");
cvShowImage("Left", myLeftImg);
"Left" figure will be updated with new img without creating new figure.
Thanks a lot.
Ken.
You need to save a handle on the axes-object within the figure and you need to tell the image-function to precisely which axes you want it to draw.
Try something along the lines of:
figure, h_r = axes;
figure, h_l = axes;
for n=1:whatever
image(right_bitmap, 'Parent', h_r, ...);
image(left_bitmap, 'Parent', h_l, ...);
drawnow;
end;
Update: image expects the handle as property 'Parent' rather than as first parameter.