Module 5 · Matplotlib · Lesson 25 of 66
Your first charts
Line charts with plt.plot, labels, and several charts side by side.
In this lesson · about 12 minutes
- Import matplotlib.pyplot as plt
- Draw and label a line chart
- Put charts side by side with plt.subplot
Matplotlib is Python's original charting library. Almost every other plotting tool, including pandas' built-in charts and Seaborn, draws with it underneath. It can make nearly any chart and gives you control over every detail.
Import its plotting module as plt. In a Jupyter notebook you'd also run %matplotlib inline once so charts appear under the cell; in a plain script you'd call plt.show() to open a window. On this page charts show up automatically when a cell finishes.
Several charts on one canvas
plt.subplot(nrows, ncols, plot_number) splits the canvas into a grid and makes the next commands draw in one cell of it. Cells are numbered from 1, left to right.
Exercise
Plot a parabola
Plot x against y, label the x axis 'x' and the y axis 'y', and title the chart 'Squares'.
Code that runs before yours
import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 5, 11) y = x ** 2
Exercises solved: 0 of 1
Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.