Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom legend without any relation to the graph

I wish to insert a legend that is not related to the graph whatsoever:

figure; hold on; plot(0,0,'or'); plot(0,0,'ob'); plot(0,0,'ok'); leg = legend('red','blue','black'); 

Now I wish to add it to another figure:

figure; t=linspace(0,10,100); plot(t,sin(t)); %% ADD THE LEGEND OF PLOT ABOVE  
like image 515
jarhead Avatar asked Nov 02 '15 09:11

jarhead


People also ask

How do I manually add a legend in MATLAB?

Set the DisplayName property as a name-value pair when calling the plotting functions. Then, call the legend command to create the legend. Legends automatically update when you add or delete a data series. If you add more data to the axes, use the DisplayName property to specify the labels.

How do you exclude a plot in legend?

1- Select the curve you don't want have legend. 2- Go to the "more properties" (while the curve is still selected). 3- Turn "HandleVisibility" off.

How do you edit a legend in MATLAB?

If you double-click on a text label in a legend, MATLAB opens a text editing box around all the text labels in the legend. You can edit any of the text labels in the legend. To access the properties of these text objects, right-click on a text label and select Properties from the context-sensitive pop-up menu.

How do I create a legend in MATLAB?

Add Custom Legends Using the text() Function in MATLAB Simply plot the variable and then select the coordinates from the plot and then use the text() function to place the text on the selected coordinates. If you give the coordinates which don't lie on the plot, you won't be able to see the text.


Video Answer


2 Answers

This is how I have solved this problem in the past:

figure t=linspace(0,10,100); plot(t,sin(t)); hold on;  h = zeros(3, 1); h(1) = plot(NaN,NaN,'or'); h(2) = plot(NaN,NaN,'ob'); h(3) = plot(NaN,NaN,'ok'); legend(h, 'red','blue','black'); 

This will plot the additional points, but because the coordinates are at NaN they will not be visible on the plot itself:

enter image description here

EDIT 26/10/2016: My original answer results in greyed out legend entries in 2016b. The updated code above works, but the answer below is only relevant pre-2016b:

figure t=linspace(0,10,100); plot(t,sin(t)); hold on;  h = zeros(3, 1); h(1) = plot(0,0,'or', 'visible', 'off'); h(2) = plot(0,0,'ob', 'visible', 'off'); h(3) = plot(0,0,'ok', 'visible', 'off'); legend(h, 'red','blue','black'); 

This will plot the additional points, but they will not be visible on the plot itself.

You can also use copyobj to copy graphics elements from one figure to another if you have a lot of elements, then use set(x, 'visible', 'off') to hide them before showing the legend, but it depends on what your final application is.

like image 174
zelanix Avatar answered Oct 06 '22 18:10

zelanix


Your question is a little unclear. However, the first thing I thought of when reading it was the text function in Matlab.

You can use the text function to add text to a Matlab figure. It's use is

>> text(x, y, str); 

where x and y are the coordinates in the figure where you want to add the text str. You can use the Color option of text for colours and TeX to draw lines or even _. I've gotten very creative with plots using text.

Here's a quick and dirty example of emulating a legend with text

x = 0:pi/20:2*pi; y = sin(x); plot(x,y) axis tight  legend('sin(x)');  text(5.7, 0.75, 'sin(x)'); text(5.1, 0.78, '_____', 'Color', 'blue'); 

which produces

             <code>text</code> example

For this specific case you could use the specific command (noted by @Hoki in the comments).

ht = text(5, 0.5, {'{\color{red} o } Red', '{\color{blue} o } Blue', '{\color{black} o } Black'}, 'EdgeColor', 'k'); 

to produce

             <code>text</code> example

by retrieving the handle to the text object it becomes trivial to copy it to a new figure, copyobj(ht, newfig). [1]

like image 29
IKavanagh Avatar answered Oct 06 '22 18:10

IKavanagh