Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show legend for only a specific subset of curves in the plotting?

t = 0 : 0.01 : 2 * pi; s = sin(t); c = cos(t); m = -sin(t);  hold on; plot(t, s, 'r'); plot(t, c, 'b'); plot(t, m, 'g'); hold off;  legend('', 'cosine', ''); 

The Plotting

There are several curves in my plotting. I want to display legend for only some of them. How do I do it?

For example, how do I make only the legend for the cosine curve visible in the plotting above? When I call the legend() functions as legend('', 'cosine'); instead of adding the empty third parameter, indeed the third green line is removed from the legend. But that doesn't solve my problem, because the undesired red line stays visible.

like image 782
hkBattousai Avatar asked Dec 03 '12 15:12

hkBattousai


People also ask

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.

What is Legend () when drawing multiple curves in a figure?

Adding legends to the multiple curvesLegends can be added to describe the different curves in the plot. For this, we call the legend() function after plotting the curves. This funciton adds a legend box with appropriate legends at a desired location inside the plot.

How do you show the legend on a plot in Matlab?

Alternatively, you can specify the legend labels using the DisplayName property. 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.

Which function is used to show legends in a plot?

legend() function in R Programming Language is used to add legends to an existing Plot. A legend is defined as an area of the graph plot describing each of the parts of the plot.


2 Answers

I do not like storing the handle values, it becomes a mess when I have a lot of graphs in my figures. Therefore i found another solution.

t = 0 : 0.01 : 2 * pi; s = sin(t); c = cos(t); m = -sin(t); hold on; plot(t, s, 'r', 'HandleVisibility','off'); % Plotting and telling to hide legend handle h2 = plot(t, c, 'b', 'DisplayName', 'cosine');  % Plotting and giving legend name plot(t, m, 'g', 'HandleVisibility','off'); % Plotting and telling to hide legend handle  legend show  % Generating legend based on already submitted values 

This give me the same graph as shown in Eitan T's answer.

It should be noted that this will affect other matlab functions also, for example will cla only remove the plots mentioned on the legend. Search for HandleVisibility in the Matlab documentation for more about that.

like image 106
Mixopteryx Avatar answered Oct 09 '22 06:10

Mixopteryx


Just store the desired legend handles in a variable and pass the array to legend. In your case, it would only be one value, like so:

hold on; plot(t, s, 'r'); h2 = plot(t, c, 'b');  % # Storing only the desired handle plot(t, m, 'g'); hold off;  legend(h2, 'cosine');  % # Passing only the desired handle 

You should get this plot:

enter image description here

like image 21
Eitan T Avatar answered Oct 09 '22 04:10

Eitan T