Start Coding

Topics

MATLAB 2D Plots

2D plots are essential tools for visualizing data and functions in MATLAB. They provide a clear and intuitive way to represent relationships between variables.

Basic Plotting

The fundamental function for creating 2D plots in MATLAB is plot(). It takes x and y coordinates as arguments and draws a line connecting the points.

x = 0:0.1:2*pi;
y = sin(x);
plot(x, y)
title('Sine Wave')
xlabel('x')
ylabel('sin(x)')

This example creates a sine wave plot with labeled axes and a title.

Plot Types

MATLAB offers various plot types for different data representations:

  • scatter(): Creates scatter plots
  • bar(): Generates bar graphs
  • histogram(): Displays data distribution
  • pie(): Shows data as a pie chart

Customization

Enhance your plots with customization options:

x = linspace(0, 10, 100);
y1 = sin(x);
y2 = cos(x);

plot(x, y1, 'r--', 'LineWidth', 2)
hold on
plot(x, y2, 'b-.', 'LineWidth', 2)
legend('sin(x)', 'cos(x)')
grid on
axis([0 10 -1.5 1.5])

This example demonstrates line color, style, and width adjustments, as well as adding a legend, grid, and setting axis limits.

Multiple Plots

Use subplot() to create multiple plots in a single figure:

x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);

subplot(2,1,1)
plot(x, y1)
title('Sine')

subplot(2,1,2)
plot(x, y2)
title('Cosine')

This code creates two vertically stacked plots within one figure.

Best Practices

  • Always label your axes and include a title for clarity
  • Use appropriate plot types for your data
  • Consider color choices for accessibility
  • Adjust font sizes for readability
  • Use hold on to overlay multiple plots

Related Concepts

To further enhance your MATLAB plotting skills, explore these related topics:

By mastering 2D plots in MATLAB, you'll be able to effectively visualize your data and communicate your findings. Practice with different plot types and customization options to create compelling and informative graphics.