Search code examples
matlabmatlab-figure

How to avoid overlapping plot labels in MATLAB?


I am trying to label my horizontal (constant y-axis) plot lines in MATLAB. However, some y-lines overlap and therefore their labels also overlap as shown:

'Min' and 'Max' lines overlap so their labels overlap

How may I separate these labels? I am simply using the yline function in MATLAB for the horizontal lines.

ymax = 0;
ymin = 0;
yline([ymax,ymin],'-',{'Max','Min'})
set(gca,'ylim',[-10,100])

Solution

  • One approach could be to set the LabelVerticalAlignment property for one of the two lines to 'bottom', as follows:

    ymax = 0;
    ymin = 0;
    yline(ymax,'-','Max')
    yline(ymin,'-','Min','LabelVerticalAlignment','bottom')
    set(gca,'ylim',[-10,100])
    

    enter image description here

    This property can be set to 'top' (default), 'middle' or 'bottom'.

    There is also a LabelHorizontalAlignment, which can be set to 'right' (default), 'left' or 'center'.