1

I have a figure where I had some plots trough different functions using hold on.

When I want to create my Legend, I don't have access to all the handle of my figures.

Is there a way to create an independent Legend by defining the tip of the plot and the string for the description?

For example I would like to be able to do:

figure;
plot(0:0.1:pi, sin(0:0.1:pi), 'b-');
customLegend('r.', 'red dots');

In the previous version it was possible to create a virtual plot using:

h1 = plot([], [], 'r.');
legend(h1, 'red dots');

For example I want to change from the image of the left to the image of the right:

enter image description here

R.Falque
  • 904
  • 8
  • 29
  • I am not sure what you are asking even with your solution provided. You don't need handles as long as you intend to use all lines. They simply are displayed in the order they were entered. – Eypros Jan 22 '15 at 07:35
  • I want to overwrite the legend. The handles give you control what you want to display. My problem is that I plot several points with several colors. In the legend I just want one single label for each colors. – R.Falque Jan 22 '15 at 09:56
  • You want to overwrite it (i.e. replace it) with what? The legends you provide does not give any different legend than the default one – Eypros Jan 22 '15 at 09:58
  • I edit my first answer ;). I just want to be able to define the legend in the way I want. The example provided is just a simple representation of what I actually want to do (but it does a difference of the original one). – R.Falque Jan 22 '15 at 10:03
  • No you are not using a different legend (at least I don't get one). Typical legend would be `legend('red dots', 'blue lines')` which creates a 2-lines legend with the string you provided corresponding to the lines in the order they were plotted. What do you do differently in your example? – Eypros Jan 22 '15 at 10:17
  • You don't get it, the problem is not about changing the string but about the representation of the plot. Tell me, if it is not clear I will add a picture ;) – R.Falque Jan 22 '15 at 10:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69382/discussion-between-eypros-and-r-bergamote). – Eypros Jan 22 '15 at 10:31
  • [This answer](http://stackoverflow.com/a/25885777/3372061) may be related to your problem. – Dev-iL Jan 23 '15 at 11:06
  • How to plot this fig? The link is (http://tex.stackexchange.com/questions/225128/how-to-plot-this-beautiful-figs)? – xiaojidan Jan 27 '15 at 11:59

2 Answers2

3

Just use NaN instead of []:

figure;
plot([1:20], [-9:10], 'b-');
hold on
h1 = plot(NaN, NaN, 'r.');
legend(h1, 'red dots');

enter image description here

My interpretation of why this works: using NaN generates a line object h1 (size 1x1). This line is not visible in the figure because NaN values are not shown in graphs, but you can use it for the legend. On the contrary, using [] produces an empty h1 (size 0x1), which doesn't have the desired effect when used as the first input of legend.

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
0

So I got this solution which is not very elegant (plotting outside of the window, save the handle and resizing the window to the original axis).

figure;
plot(0:0.1:pi, sin(0:0.1:pi), 'b-');

hold on;
a = axis; 
h1 = plot(min(a) - 10, min(a),  'r.'); % plots outside of the current figure
axis(a);
legend(h1, 'red dots');
hold off;

If someone has a more elegant solution, I would be happy to take it :)

Edit: it is actually possible to use a nan instead of [] like:

h1 = plot(nan, nan, 'r.');
h2 = plot(nan, nan, 'b+');
legend([h1, h2], 'red dots', 'blue cross');

However this method does not work with the rectangles command.

R.Falque
  • 904
  • 8
  • 29