1
votes

I would like to ask something about Matlab. I could not find this on this forum. I would like to create a new text box right at the top of a legend box in a plot. I am trying to do this with a function called annotation, but I am not doing it right because sometimes the new box is over the legend box. Is there any way to do this in order to keep my plot with a good look?

I am using this code: annotation('textbox',[0.7 0.192 0.1 0.1],'String',{'OOI =',num2str(OOI)}); I've playing on those numbers, but I could not find the right ones yet to do what I want.

I have another question. For some plots, my axes don't start at the point (0,0). It is starting at (0,-something). I would like to know if there is a way to make it start at (0,0). I tried

set(gca,'XTick', [0:20:300]); set(gca,'YTick',[0:0.1:1]);

but it is not working. Could you guys, please, help me? Any help I would be really glad.

1
can you post a minimal working example with the code you use to generate the legend as well? You can use dummy data just for the demo it's going to be easier to understand what is the problem and how to help. Thanks!Benoit_11

1 Answers

0
votes

For the question with axes at first: you have to set XLim and YLim, i.e set(gca,'XLim',[0 1],'YLim',[0 1]). You should also set XLimMode, YLimMode to manual.

For the question with textbox, you should set Parent property of annotation to be your axes, and set Units property of both to normalized (or another, but must be the same). From my own experience if you are running version earlier than R2014b, then positioning annotation does not work very good, but from R2014b upwards, once you properly set the Parent property, positioning is not problem.

Update: small example for you, however you can use Matlab's documentation and google!!!

f = figure('Position', [values_in_pixels]);
a = axes('Parent', f);
t = annotation('textbox', [0 0 1 1], 'EdgeColor', 'r');

At this point, t is an annotation which is child of a hidden object AnnotationPane:

>> tp = get(t, 'Parent')
tp = 
    AnnotationPane

and tp is child of your figure f:

>> isequal(f, get(tp, 'Parent'))
ans =
     1

However, t is currently positioned according to f, since you can see the red border fits to the figure.

Changing t's parent property to be your axes a and you see the red border fits to your axes:

set(t, 'Parent', a);

Change t's position, and you can see t is now positioned according to your axes:

set(t, 'Position', [.2 .5 .3 .1])

Note: since both axes and annotation have Units = normalized by default, after you set t to be child of a, you dont need to care about it while moving or re-positioning your annotation.