Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 57 ยท Getting Started

Your First Plot

Draw your first Matplotlib line plot from Python lists and NumPy arrays, add a title and axis labels, and learn the two ways to call plt.plot.

The fastest way to learn plotting is to draw something. In this lesson you will make a line plot from a handful of numbers, label it so a stranger can read it, then repeat the trick with NumPy arrays. You will also learn the one detail that confuses beginners: plot can take one list or two, and the meaning changes.

If you want the big picture first, read What Is Matplotlib?. Otherwise, just press Run.

Plotting a list

Import the plotting module as plt (everyone does) and pass a list of numbers to plt.plot. Then call plt.show() to display the result.

Python The smallest plot

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You gave Matplotlib only y values. It guessed the x values for you: 0, 1, 2, ..., one per item. That is why the horizontal axis runs from 0 to 6. This is the y-only form of plot.

Giving x and y

Usually you know the x values: days, years, distances. Pass them first, then the y values. The two sequences must have the same length.

Python x and y together

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Now the x axis shows days 1 to 7. The rule to remember:

  • plt.plot(y) plots y against 0, 1, 2, ....
  • plt.plot(x, y) plots y against your own x.

If the lengths differ, Matplotlib raises a ValueError that mentions both shapes, such as x and y must have same first dimension, but have shapes (3,) and (4,). The fix is almost always to check which list lost or gained an item.

Making the chart readable

A chart without labels is a riddle. At minimum, add a title and a name (with units) for each axis.

Python Title and axis labels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

plt.title, plt.xlabel and plt.ylabel each take a string. They apply to the plot you are currently drawing. Put units in parentheses on the axis label, and make the title say what the data is, not just "Plot".

A plot in four moves

Import import matplotlib.pyplot as plt
Draw plt.plot(x, y)
Label Title and axis names.
Show plt.show()

Most charts are just these four moves, repeated.

1 / 2

Plotting NumPy arrays

Matplotlib accepts NumPy arrays anywhere it accepts lists, and arrays are how you will usually create data. np.linspace(start, stop, count) gives evenly spaced x values, and a NumPy function applied to the whole array gives y values in one step (see Vectorization).

Python A smooth curve from arrays

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A line plot just connects points with straight segments. The curve looks smooth only because there are many points. With 5 points it would look like a polygon, which is a good experiment to try in the editor above: change 100 to 5.

Several lines in one plot

Call plot more than once before show, and every line goes on the same chart. Matplotlib picks a different color for each automatically. Add label= to each and call plt.legend() so readers know which is which.

Python Two lines and a legend

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Legends get their own lesson (Legends and Grids). For now, the pattern is enough: label= on the line, plt.legend() once.

You can put a format string as a third argument for a quick style: plt.plot(x, y, "ro--") means red circles joined by a dashed line. Colors, Line Styles and Markers explains the full syntax.

What plt.show() does

plt.show() displays every figure that is open and, in a normal script, blocks until you close the window. Here on the site it simply displays the image. If you forget plt.show() in a script, the figure is built but never displayed; in a notebook, the figure usually appears anyway at the end of the cell, which hides the mistake until you run the code elsewhere. Make plt.show() the last line of every plotting example.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does plt.plot([5, 7, 6]) use for the x values?

    With a single sequence, Matplotlib treats it as y values and uses the positions 0, 1, 2, ... as x.
  2. 2. Which call adds a name to the horizontal axis?

    plt.xlabel sets the x axis label, plt.ylabel the y axis label, and plt.title the title of the plot.
  3. 3. You call plt.plot twice and then plt.show once. What do you get?

    Successive plot calls draw onto the current axes, so both lines share one chart until you start a new figure.
  4. 4. Why does np.linspace(0, 1, 100) help when drawing a curve?

    A line plot connects points with straight segments. Many closely spaced points make the result look like a smooth curve.

Summary

  • import matplotlib.pyplot as plt, then plt.plot(...) and plt.show().
  • plt.plot(y) uses 0, 1, 2, ... for x; plt.plot(x, y) uses your own x. Both must be the same length.
  • Add plt.title, plt.xlabel and plt.ylabel so the chart explains itself, with units on the axes.
  • NumPy arrays work just like lists; np.linspace plus a vectorized formula makes smooth curves.
  • Several plot calls before show share one chart; use label= and plt.legend() to tell them apart.

Next, learn the names of the parts you just drew in Anatomy of a Figure.