Skip to content
AgileLogics
5 min read

Basic Plotting

Prerequisites: Pure functions and Map

Plots are ordinary results: evaluate a plotting function and the graphic appears in the notebook. (Graphics can't be shown as text, so this lesson describes what you'll see.)

Plotting functions

Plot[Sin[x], {x, 0, 2 Pi}]

One period of a sine wave from 0 to 2π. {x, min, max} gives the variable and its range.

Plot[{Sin[x], Cos[x]}, {x, 0, 2 Pi},
  PlotLegends -> "Expressions",
  AxesLabel -> {"x", "y"},
  PlotLabel -> "Sine and cosine"]

Two curves in different colors, with a legend, axis labels, and a title. Options are written Name -> value after the required arguments.

Plotting data

ListPlot[{1, 4, 9, 16, 25}]
ListLinePlot[Table[{t, t^2}, {t, 0, 5}]]
BarChart[{3, 7, 2}, ChartLabels -> {"A", "B", "C"}]
Histogram[{1, 2, 2, 3, 3, 3, 4, 4, 5}]
  • ListPlot draws points; a plain list uses positions 1, 2, 3, ... as x-values, and a list of {x, y} pairs gives both.
  • ListLinePlot joins the points with lines.
  • BarChart, PieChart, and Histogram cover everyday charts.

Interactive plots

Manipulate[Plot[Sin[a x], {x, 0, 2 Pi}], {a, 1, 5}]

A slider for a appears above the plot, and the curve redraws as you drag it. Manipulate works with any expression, not just plots.

Saving a plot

p = Plot[x^2, {x, -2, 2}];
Export["parabola.png", p]
parabola.png

Export returns the file name it wrote. The format comes from the extension — PNG, PDF, SVG, JPEG, and more.

For quick checks of a formula without code, the Graphing Calculator on this site plots functions in your browser.