Key Concepts

Review core concepts you need to learn to master this subject

Pyplot functions

The Python library Matplotlib contains the pyplot module, which provides users with an interface for graphing data. Pyplot contains over 100 functions, from acorr to yticks. You must import the module, and plt is the standard variable name used.

from matplotlib import pyplot as plt

Here are some of the most common pyplot functions:

Function Description
Plot plots y versus x as lines and/or markers
Show displays a figure
Axis sets some axis properties
Xlabel sets the label for the x-axis
Ylabel sets the label for the y-axis
Title sets a title for the axes
Subplot adds a subplot to the current figure
Subplots_adjust tunes the subplot layout
Legend places a legend on the axes
Figure creates a new figure
Savefig saves the current figure
Line Graphs in Matplotlib
Lesson 1 of 3
  1. 1
    Matplotlib is a Python library used to create charts and graphs. In this first lesson, you will get an overview of the basic commands necessary to build and label a line graph. The concepts you wi…
  2. 2
    Line graphs are helpful for visualizing how a variable changes over time. Some possible data that would be displayed with a line graph: * average prices of gasoline over the past decade * weight o…
  3. 3
    We can also have multiple line plots displayed on the same set of axes. This can be very useful if we want to compare two datasets with the same scale and axis categories. Matplotlib will automati…
  4. 4
    We can specify a different color for a line by using the keyword color with either an HTML color name or a HEX code : plt.plot(days, money_spent, color=’green’) plt.plot(days, money_spent_2, col…
  5. 5
    Sometimes, it can be helpful to zoom in or out of the plot, especially if there is some detail we want to address. To zoom, we can use plt.axis(). We use plt.axis() by feeding it a list as input. T…
  6. 6
    Eventually, we will want to show these plots to other people to convince them of important trends in our data. When we do that, we’ll want to make our plots look as professional as possible. The…
  7. 7
    Sometimes, we want to display two lines side-by-side, rather than in the same set of x- and y-axes. When we have multiple axes in the same picture, we call each set of axes a subplot. The pictu…
  8. 8
    Sometimes, when we’re putting multiple subplots together, some elements can overlap and make the figure unreadable: ![overlapping](https://content.codecademy.com/courses/matplotlib/overlapping_sub…
  9. 9
    When we have multiple lines on a single graph we can label them by using the command plt.legend(). The legend method takes a list with the labels to display. So, for example, we can call: plt.pl…
  10. 10
    In all of our previous exercises, our commands have started with plt.. In order to modify tick marks, we’ll have to try something a little bit different. Because our plots can have multiple subpl…
  11. 11
    When we’re making lots of plots, it’s easy to end up with lines that have been plotted and not displayed. If we’re not careful, these “forgotten” lines will show up in your new plots. In order to b…
  12. 12
    Now you’ve played around with several two-dimensional line plots in Matplotlib. You’ve seen how you can create simple, readable plots with few commands. You’ve also learned some commands to style a…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo