Search code examples
matlabplotlabelmatlab-figure

Adding description outside the plot with coloured text


I am working on a visualisation of my data and would like to add a description of a plot. The description will be added outside the plot. For that I have written:

plot(1:10)
text(2,8,'my text here ','Color','green','FontSize',14,'location','EastOutside')

But it doesn't work, I get the error:

There is no location property on the Text class.

How can I fix this?

This is my desired output:

pic


Solution

  • The location input pair you're giving to text are for the legend, not text objects...

    The location is specified by the first two inputs (x/y), so if you don't use the location input you get this:

    text( 2, 8, 'my text here ', 'Color', 'green', 'FontSize', 14 )
    

    plot with text

    If you want the text location to be independent of the axes, you should use an annotation instead, which gets its location from the figure rather than the axes.

    annotation( 'textbox', 'String', 'my annotation', 'Color', 'green', ...
                'FontSize', 14, 'Units', 'normalized', 'EdgeColor', 'none', ...
                'Position', [0.8,0.5,0.2,0] )
    

    Because I used normalized position here, the Position argument is a percentage of the figure window. To get the behaviour I suspect you're after, you'll have to reposition the axes too...

    set( gca, 'Position', [0.1, 0.1, 0.6, 0.8] )
    

    plot with annotation