Matlab and Octave Programming for STEM Applications · LibreTexts
0%
Contents
Text
Size
From LibreTexts, under CC BY-NC-SA 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.
19.8: Labeling Axes
Opening the book…
The plots in this chapter have labels on the axes, and one of them has a legend, but I didn’t show you how to do that. Let’s do it now.
The functions to label the axes are xlabel and ylabel:
xlabel('Time (hours)')
ylabel('Population (billions of cells)')
The function to generate a legend is legend:
legend('euler', 'ode45')
The arguments are the labels for the lines, in the order they were drawn. Usually the legend is in the upper-right corner, but you can move it by providing an optional argument called Location:
legend('euler', 'ode45', 'Location', 'northwest')
Finally, save the figures using saveas:
saveas(gcf, 'runge.eps', 'epsc')
The first argument is the figure we want to save; gcf is a MATLAB command that stands for “get current figure,” which is the figure we just drew. The second argument is the filename. The extension specifies the format we want, which is Encapsulated PostScript (.eps). The third argument tells MATLAB what driver to use. The details aren’t important, but epsc generates figures in color.