In MATLAB, a legend can be added to a plot to describe different data series, enhancing the plot's readability.
Here's a simple code snippet to create a plot with a legend:
x = 0:0.1:10; % Define x values
y1 = sin(x); % Compute y1 values
y2 = cos(x); % Compute y2 values
plot(x, y1, 'r', x, y2, 'b'); % Plot y1 in red and y2 in blue
legend('Sine', 'Cosine'); % Add legend
xlabel('X-axis'); % Add x-axis label
ylabel('Y-axis'); % Add y-axis label
title('Sine and Cosine Functions'); % Add title
Understanding Legends in MATLAB
What is a Legend?
In the context of a MATLAB legend plot, a legend serves as a key to the various data series presented in a graph. It helps identify different datasets by providing a clear label for each one, allowing viewers to understand what each line or symbol in the plot represents. For instance, when plotting temperature data alongside humidity levels, a well-defined legend would specify which line corresponds to each variable. Legends enhance clarity, especially when multiple datasets are displayed on a single graph.
Benefits of Using Legends
Utilizing legends in your MATLAB plots offers numerous advantages:
- Clear information presentation: By labeling multiple datasets, legends help viewers quickly differentiate between them.
- Improved readability: Legends can significantly increase the comprehensibility of plots, making data interpretation seamless.

Creating Basic Plots in MATLAB
Setting Up Your Environment
To get started with MATLAB, you must first open the application and prepare your workspace. You can create a script file where you'll write your code. Here’s how to create a simple plot before adding legends.
Example Code Snippet
x = 0:0.1:10;
y1 = sin(x);
y2 = cos(x);
plot(x, y1, 'r', x, y2, 'b'); % Basic plot without a legend
This code generates a basic plot with sine and cosine functions, but it doesn’t yet include a legend.

Adding Legends to Your Plots
How to Add a Legend
The `legend()` function in MATLAB makes it straightforward to add legends to your plots. It is versatile and allows you to include custom labels easily.
Using the legend() Function
The syntax for the `legend()` function is quite simple. You need to call this function after your plot command, passing it labels for the datasets.
Example Code Snippet
plot(x, y1, 'r', x, y2, 'b');
legend('Sine', 'Cosine'); % Adding a legend
In this example, the legend identifies the red line as the Sine function and the blue line as the Cosine function.
Customizing Legend Text
You can use cell arrays to provide organized and descriptive labels for your legend.
Example Code Snippet
legend({'Sine Function', 'Cosine Function'}); % Customizing legend text
Here, the legend now shows clearer descriptions for both lines, enhancing viewer understanding.

Advanced Legend Customizations
Positioning the Legend
One of the key features of the `legend()` function is its ability to customize the position of the legend on your plot. The `Location` property allows you to specify where you want your legend to appear.
Example Code Snippet
legend({'Sine Function', 'Cosine Function'}, 'Location', 'northeast');
In this example, the legend is positioned in the upper right corner of the plot, minimizing distraction while maximizing visibility.
Styling the Legend
Besides positioning, you can also enhance the legend’s appearance by changing its font size and color. This helps to make your visuals more appealing and easier to read.
Changing Font Size and Color
To modify the appearance of your legend, use the `set()` function.
Example Code Snippet
hLegend = legend({'Sine Function', 'Cosine Function'});
set(hLegend, 'FontSize', 12, 'TextColor', 'blue');
In this example, the legend's font size is increased to 12, and the text color is set to blue, giving it a more polished look.
Adding a Box Around the Legend
Sometimes it may be beneficial to have a box around your legend to distinguish it from the plot's data visually. You can control this with the `Box` property.
Example Code Snippet
legend({'Sine','Cosine'}, 'Box', 'on');
This command ensures a box surrounds the labels, adding structure to your visualization.

Using Legends with Multiple Subplots
Basics of Subplots in MATLAB
When visualizing multiple datasets, you might want to create subplots to display different plots in the same figure window. This makes comparisons between datasets simpler and more intuitive.
Example Code Snippet
subplot(2,1,1);
plot(x, y1);
legend('Sine');
subplot(2,1,2);
plot(x, y2);
legend('Cosine');
In this code, two subplots are created—one for the sine plot and another for the cosine plot, each with its own legend, allowing for a clear distinction between the two datasets.

Troubleshooting Common Issues
Legend Not Displaying
It can be frustrating when your legend doesn't appear as expected. Common reasons for this issue include having no plot commands above the `legend()` function or incorrectly specifying the legends themselves. Always ensure that the plots are correctly rendered before calling the legend function.
Overlapping Legends
Sometimes, legends may overlap with plot data. To fix this, you can adjust the `Position` property or change the sizing of the plot area.

Conclusion
Incorporating legends into your MATLAB plots is essential for clear data representation and improved readability. With the tools and techniques discussed, you are now equipped to create visually informative and appealing graphics. Don't hesitate to explore different styling options and the many features the `legend()` function offers.

Call to Action
Share your experiences and queries in the comments section. If you want more hands-on experience, consider joining our MATLAB classes for an enriching learning journey!