Plotting confidence intervals with only one serie in the legend

10 views (last 30 days)
Hello,
In a figure, I plotted a series of data. After having calculated the average, lower and upper limits of both 95% and 99.4% confidence intervals, I added them to the figure. Then, I inserted the legend. Here, all the 6 series' labels appear. What I would then like is to remove from the legend labels, one of the 2 labels corresponding to 95% confidence intervals and one of the 2 labels of the 99.4%. But, Matlab doesn't allow me to do it. Could you help e to solve this problem ? Thanks and best regards.

Accepted Answer

the cyclist
the cyclist on 8 Jun 2012
There are several different approaches to doing this, but you didn't share your code, so it is not possible to know which method fits best with how your code currently operates. However, the key is to only supply the legend() command with the handles for the objects you want to appear in the legend. Here's a simple example that illustrates the approach. I hope you can figure out how to work the same approach into your code:
figure
hold on
h1 = plot(1:100,rand(1,100)+1,'g-');
h2 = plot(1:100,rand(1,100), 'b-');
h3 = plot(1:100,rand(1,100)-1,'r-');
legend([h1 h2],{'top','middle'}); % I did not include the handle for the bottom line
  2 Comments
Philippe
Philippe on 12 Jun 2012
Here is my code:
hold on
plot(Processdata(:,1),Processdata(:,2));
plot(Stat(:,1),Stat(:,(2:end)));
Here, in this case, "Processdata" are the main data, "Stat" gathers the average, the upper and lower range values of 95% and 99.4% confidence interval of all the data.
So the graph contains the data and 5 lines: the average value, both upper and lower limits of 95% confidence interval and both upper and lower limits of 99.4% of confidence interval. At the end, when I insert the legend, all series appear in it: the data, the average value, the two limits of both confidence intervals. But, in this case, the legend becomes unusefully overcrowded. I thus would like to remove one of the two representation of each confidence interval in the legend.
In Excel, it is easy, you just have to select it in the legend and delete it without impacting the plots themselves. But how to do it in Matlab ?
Thanks and best regards.
Tom Lane
Tom Lane on 12 Jun 2012
Instead of inserting the legend from the figure toolbar, try getting handles from the plot commands as the cyclist suggests, and supplying labels for the handles that you want to include in the legend.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!