This post describes how to handle expression and background color of legend box in MatLab figures.
We can print LaTex expressions in legend box, by instructing the legend to interpret the following string as latex expression. Create the handle to legend box and set the interpreter option to latex as follows
set(lgnd,'interpreter','latex'); |
By default the legend box background is white in MatLab figures. When the situation demands, the legend box background color can be set with appropriate color option.
In the following example, the background of the legend box is made transparent by instructing color option to be ‘none’
set(handle,’color’,'none’); |
In place of ‘none’ we can pass any other color argument.
Another important option that we use is legend location. It is set with location options as follows.
set(handle,'Location','Best'); |
Here MatLab finds the best location for the legend. The other possible legend location options are : North, South, East, West, NorthEast, NorthWest, SouthEast, SouthWest, NorthEastOutside, NorthWestOutside, SouthEastOutside, SouthWestOutside, Best, BestOutside
The following code snippet produces the plot shown below
1 2 3 4 5 6 7 8 | plot(rand(3)); lgnd = legend('$f_1(x)$','$f_2(x)$','$f_3(x)$'); set(lgnd,'interpreter','latex'); %Latex expression set(lgnd,'color','none',... %transparent background ... %other legend options of interest are 'Location','Best',... %legend location 'FontSize',14,... %legend font-size ); |