In this section, we will explore how to customize plots in MATLAB to make them more informative and visually appealing. Customizing plots involves modifying various plot properties such as titles, labels, legends, colors, and line styles. This is crucial for creating professional and publication-quality figures.

Key Concepts

  1. Titles and Labels
  2. Legends
  3. Line Styles and Colors
  4. Markers
  5. Axes Properties
  6. Annotations

  1. Titles and Labels

Adding Titles and Labels

To add titles and labels to your plots, you can use the following functions:

  • title: Adds a title to the plot.
  • xlabel: Adds a label to the x-axis.
  • ylabel: Adds a label to the y-axis.

Example

x = linspace(0, 2*pi, 100);
y = sin(x);

plot(x, y);
title('Sine Wave');
xlabel('x-axis (radians)');
ylabel('y-axis (amplitude)');

Explanation

  • linspace(0, 2*pi, 100): Generates 100 points between 0 and \(2\pi\).
  • plot(x, y): Plots the sine wave.
  • title('Sine Wave'): Adds the title "Sine Wave".
  • xlabel('x-axis (radians)'): Labels the x-axis.
  • ylabel('y-axis (amplitude)'): Labels the y-axis.

  1. Legends

Adding Legends

Legends help identify different data series in a plot. Use the legend function to add legends.

Example

x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);

plot(x, y1, '-r', x, y2, '--b');
legend('Sine', 'Cosine');

Explanation

  • plot(x, y1, '-r', x, y2, '--b'): Plots sine and cosine waves with different line styles and colors.
  • legend('Sine', 'Cosine'): Adds a legend identifying the sine and cosine waves.

  1. Line Styles and Colors

Customizing Line Styles and Colors

You can customize the appearance of lines using various options in the plot function.

Example

x = linspace(0, 2*pi, 100);
y = sin(x);

plot(x, y, 'LineWidth', 2, 'Color', [0.5, 0.2, 0.8]);

Explanation

  • LineWidth: Sets the width of the line.
  • Color: Sets the color of the line using RGB values.

  1. Markers

Adding Markers

Markers can be added to data points to make them more visible.

Example

x = linspace(0, 2*pi, 10);
y = sin(x);

plot(x, y, '-o', 'MarkerSize', 8, 'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'g');

Explanation

  • '-o': Line style with circle markers.
  • MarkerSize: Size of the markers.
  • MarkerEdgeColor: Color of the marker edges.
  • MarkerFaceColor: Color of the marker faces.

  1. Axes Properties

Customizing Axes

You can customize the properties of axes using the axis function and other related functions.

Example

x = linspace(0, 2*pi, 100);
y = sin(x);

plot(x, y);
axis([0 2*pi -1.5 1.5]);
grid on;

Explanation

  • axis([0 2*pi -1.5 1.5]): Sets the limits for the x-axis and y-axis.
  • grid on: Turns on the grid.

  1. Annotations

Adding Annotations

Annotations such as text, arrows, and shapes can be added to plots to highlight specific points or areas.

Example

x = linspace(0, 2*pi, 100);
y = sin(x);

plot(x, y);
text(pi, 0, '\leftarrow \pi', 'FontSize', 12);

Explanation

  • text(pi, 0, '\leftarrow \pi', 'FontSize', 12): Adds a text annotation at the point \((\pi, 0)\).

Practical Exercise

Exercise

  1. Create a plot of the functions \(y = \sin(x)\) and \(y = \cos(x)\) on the same graph.
  2. Customize the plot with the following:
    • Title: "Sine and Cosine Waves"
    • X-axis label: "x (radians)"
    • Y-axis label: "Amplitude"
    • Legend identifying the sine and cosine waves
    • Different line styles and colors for each function
    • Grid enabled

Solution

x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);

plot(x, y1, '-r', 'LineWidth', 2);
hold on;
plot(x, y2, '--b', 'LineWidth', 2);
hold off;

title('Sine and Cosine Waves');
xlabel('x (radians)');
ylabel('Amplitude');
legend('Sine', 'Cosine');
grid on;

Explanation

  • hold on and hold off: Allows multiple plots on the same graph.
  • The rest of the code customizes the plot as specified in the exercise.

Conclusion

In this section, we learned how to customize plots in MATLAB by adding titles, labels, legends, and annotations, and by modifying line styles, colors, markers, and axes properties. These customizations help in creating clear and professional-quality visualizations. In the next section, we will explore advanced plotting techniques to further enhance our data visualization skills.

© Copyright 2024. All rights reserved