Introduction to Matplotlib
Master a commonly used Python graphing module, Matplotlib. Soon, you will be producing high-quality plots to visualize your data.
StartKey Concepts
Review core concepts you need to learn to master this subject
Pyplot functions
Pyplot-axis
Setting Linestyle, Color in Matplotlib
Adjusting Subplot Margins in Matplotlib
X-ticks and Y-ticks in Matplotlib
Subplots in Matplotlib
Figures in Matplotlib
Pyplot functions
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 |
- 1Matplotlib 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…
- 2Line 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…
- 3We 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…
- 4We 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…
- 5Sometimes, 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…
- 6Eventually, 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…
- 8Sometimes, 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…
- 10In 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…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory