Skip to content

MATLAB - Plot Arrays

Data visualization is a cornerstone of scientific computing, and MATLAB excels at turning arrays of data into insightful graphics. Plotting arrays is the most fundamental visualization task, allowing you to see trends, relationships, and patterns in your data. This tutorial covers the modern, robust techniques for creating, customizing, and exporting plots in MATLAB.

While you can create a simple plot with plot(x, y), a more robust and scalable approach involves explicitly creating figure and axes objects. This gives you full control over every aspect of your visualization and is essential for complex plots or applications.

  • figure: Creates a new window to contain your plot(s).
  • axes: Creates a coordinate system (an axes object) inside a figure. All plots are drawn on an axes.
  • plot: The core function that draws lines and/or markers on the specified axes.

Let’s plot a sine wave. This example demonstrates the fundamental workflow: create data, create a figure and axes, plot the data, and then annotate the plot.

MATLAB provides extensive customization options using Name-Value pairs. This is the modern, readable way to control the appearance of your plot, far superior to older, cryptic short-hand strings.

  • 'Color': Set line/marker color (e.g., 'r', 'green', [R G B]).
  • 'LineStyle': '-' (solid), '--' (dashed), ':' (dotted), '-.' (dash-dot).
  • 'LineWidth': A positive scalar value for line thickness.
  • 'Marker': '+', 'o', '*', '.', 'x', 's' (square), 'd' (diamond).
  • 'MarkerSize': Size of the marker.
  • 'MarkerEdgeColor': Color of the marker’s outline.
  • 'MarkerFaceColor': Fill color of the marker.

Beyond Line Plots: Other Essential 2D Plot Types

Section titled “Beyond Line Plots: Other Essential 2D Plot Types”

Arrays can represent more than just connected lines. Here are other key functions for visualizing array data:

  • scatter(x, y): Creates a scatter plot. Ideal for showing the relationship between two variables without implying a connection between points.
  • bar(y) or bar(x, y): Creates a bar chart. Perfect for comparing values across a set of discrete categories.
  • stem(y): Creates a stem plot. Excellent for visualizing discrete-time signals or sequences.

Mini-Project: Visualizing Monthly Sales Data

Section titled “Mini-Project: Visualizing Monthly Sales Data”

Let’s apply these concepts to a practical scenario: plotting and analyzing fictional monthly sales data.

  • Always Label Your Axes: A plot without labels is meaningless. Use xlabel, ylabel, and title to provide context.
  • Use Legends for Multiple Datasets: If you have more than one line or data series on a plot, a legend is essential.
  • Choose Appropriate Colors: Use colors that are easily distinguishable. Consider colorblind-friendly palettes for publications.
  • Exporting Graphics: Use exportgraphics instead of the older saveas. It provides better control over resolution, background color, and file types (PNG, JPEG, PDF, EPS).
  • Vector vs. Raster: For publications, export to a vector format like PDF or EPS, which can be scaled to any size without losing quality. For web or presentations, a high-resolution PNG is usually sufficient.