You will be able to
- Create line graphs and bar charts with matplotlib's pyplot interface
- Add titles, axis labels, legends, and custom tick marks to a plot
- Style lines and markers using keyword arguments and the shorthand format string
- Generate smooth x-value arrays with numpy to plot functions such as x squared
- Set figure size and DPI, then save a plot to a PNG file
- Use the matplotlib documentation to look up plotting options on your own
Good to know first
- Basic Python syntax such as variables, lists, and calling functions
- Python with pip installed so matplotlib and numpy can be installed
- Basic idea of x and y coordinates
Syllabus
7 lessons and a final project
- 1
Setting up imports for matplotlib, numpy, and pandas Free
This lesson shows how to import the three libraries used for data visualization in Python: matplotlib.pyplot, numpy, and pandas. You will learn the standard aliases (plt, np, pd) and how to install the libraries if an import fails.
1 code5 build steps3 exercises3 questions - 2
Your first line graph, and how to read the matplotlib docs Beyond Pass
You learn the habit of starting from the matplotlib documentation (search with Ctrl+F) to find the function you need. You then draw your first line graph with plt.plot(x, y), tidy the code into x and y variables, and display it with plt.show().
2 code4 build steps2 exercises3 questions - 3
Adding a Title, Axis Labels, and Custom Fonts Beyond Pass
You will learn how to add a title and axis labels to a matplotlib graph using plt.title, plt.xlabel and plt.ylabel. You will also learn how to change the font name and font size using a font dictionary.
2 code3 build steps3 exercises3 questions - 4
Controlling Tick Marks on Each Axis Beyond Pass
You will learn how to choose exactly which tick marks appear on the x and y axes using plt.xticks() and plt.yticks(). You will also see how matplotlib places ticks at the right spot even when the values are unevenly spaced.
1 code3 build steps2 exercises3 questions - 5
Adding legends, colors, markers, and line styles Beyond Pass
You will learn how to label a line so a legend can show it, and how to change its color, thickness, markers and line style with keyword arguments. You will also learn the shorthand format string such as 'r.--' that sets color, marker and line style in one go.
2 code5 build steps3 exercises4 questions - 6
Plotting Functions with NumPy, Dashed Projections, Figure Size, and Saving Beyond Pass
You will learn how to plot a smooth function using NumPy arrays of decimal x-values, split one line into a solid part and a dashed projection, and control figure size and resolution before saving the graph as a PNG file. The lesson follows the Jupyter notebook from the video step by step.
2 code5 build steps3 exercises4 questions - 7
Bar charts, hatch patterns, and wrapping up Beyond Pass
You will draw a bar chart with plt.bar, resize it with plt.figure, and give each bar its own hatch pattern. You will also see how a loop with pop() can assign patterns to many bars without repeating code.
1 code5 build steps2 exercises3 questions
Final project
One-Page Data Story: Sales Line Graph and Product Bar Chart
Invent a small dataset, such as monthly sales and sales per product. Turn it into two polished matplotlib figures and save them as PNG files. The goal is to use every skill from the course in one short script that someone else could read and trust.
Its 6 tasks open with Beyond Pass.
Free preview · Lesson 1 in full
Setting up imports for matplotlib, numpy, and pandas
This lesson shows how to import the three libraries used for data visualization in Python: matplotlib.pyplot, numpy, and pandas. You will learn the standard aliases (plt, np, pd) and how to install the libraries if an import fails.
Type the lesson's commands, break things, and see why.
The idea
Every matplotlib project starts the same way: you load the libraries you need before you draw anything. This lesson sets up three of them. Matplotlib's plotting module, pyplot, draws the graphs. NumPy handles arrays of numbers. Pandas loads and organizes tables of data, such as a CSV file.
You load a library with the import keyword. The line import matplotlib.pyplot as plt means: load the pyplot part of matplotlib and call it plt for short. In the same way, write import numpy as np and import pandas as pd. These short names are a convention used by almost everyone. So when you see plt.plot, np.array or pd.read_csv in other people's code, you know which library is being used.
Put the three import lines in one notebook cell and run it. Python runs the lines once, from top to bottom. If the cell finishes with no output and no error, the libraries loaded correctly and you can move on.
If you see an error such as ModuleNotFoundError, the library is not installed on your computer. Fix this by installing it from a terminal with pip install matplotlib, pip install numpy, or pip install pandas. Another route is to install a Python distribution such as Anaconda, which already includes these packages.
Once these three lines have run, every later cell in the notebook can use plt, np and pd. Keep this cell at the top of your notebook and run it first.
Key points
- The line import matplotlib.pyplot as plt loads the plotting module under the short name plt. 2:00
- The standard aliases are plt for matplotlib.pyplot, np for numpy, and pd for pandas. 2:00
- If an import fails with an error, the library is missing: install it with pip install matplotlib, numpy or pandas. 1:00
- A distribution such as Anaconda already includes matplotlib, numpy and pandas, so you can skip separate installs. 1:00
- Run the import cell first; the later cells depend on plt, np and pd being loaded. 2:14
Code from the screen
import matplotlib.pyplot as plt
import numpy as np
import pandas as pdRun this in a Jupyter notebook cell (or a Python file after installing with pip install matplotlib numpy pandas), then press Run; no output means the imports worked.
Build along in the practice terminal
- Open a Jupyter notebook and create a new cell at the top, under a heading such as Load Necessary Libraries. 1:51
- Type the first import line exactly as shown. 1:51You should see: The cell shows the line; nothing runs yet.
$ import matplotlib.pyplot as plt - Add the numpy import on the next line. 2:00You should see: Two lines in the cell.
$ import numpy as np - Add the pandas import on the third line. 2:00You should see: Three import lines in the cell.
$ import pandas as pd - Run the cell (Shift+Enter or the Run button). If you see no error, the libraries are loaded. If you see ModuleNotFoundError, install the missing library in a terminal with pip install and run the cell again. 2:14You should see: The cell finishes with no error message, and the prompt number increases.
Break it on purpose
$ import matplotlib.pyplot as plt (no output, no error)Why: This is correct, but beginners often think nothing happened. A silent cell means the import worked; errors are the only sign of trouble.
$ import matplotlib as plt (no output, no error)Why: This runs but does not give you plt.plot, because plt is the whole matplotlib package, not pyplot. Keep the .pyplot part in the import.
$ pip install matplotlib Successfully installed matplotlib-3.xWhy: Install a library once in your environment, then import it in code. Installing does not put it into your notebook by itself; you still need the import line.
Watch out
- Do not write import matplotlib as plt. That gives you the package, not pyplot, so plt.plot will fail. 2:00
- Installing a library with pip is a one-time step on your computer; the import line is needed in every notebook or script that uses it. 1:05
- If you change the alias (for example writing import numpy as n), the rest of the course code will not match, so keep the standard names np, pd and plt. 2:00
Practice
Create a new Jupyter cell that imports matplotlib.pyplot as plt, numpy as np, and pandas as pd, then run it with no error.
Hint
Each line starts with import and ends with as and the short name.
Solution
- Click into an empty code cell.
- Type the three import lines exactly with the aliases plt, np and pd.
- Run the cell with Shift+Enter.
- Check that there is no error message.
The cell runs with no error and plt, np and pd are available to later cells.
In your own words, explain what import numpy as np does and why we write np instead of numpy.
Hint
Think about what the as part does.
Solution
- It loads the numpy library into your program.
- The as np part gives it a short name, so you write np.array instead of numpy.array.
- Short names are a convention, so other people's code reads the same way.
import numpy as np loads the numpy library and gives it the short name np, so later code can write np.something instead of typing the full name each time. This is a shared convention that makes code easier to read.
A friend wrote import matplotlib as plt and then plt.plot failed. What is wrong, and what is the fixed line?
Hint
Which part of matplotlib draws the graphs?
Solution
- With import matplotlib as plt, the name plt stands for the whole matplotlib package.
- The plotting functions live in the pyplot module, so the import must name it.
- Write import matplotlib.pyplot as plt.
The import skips pyplot. The fixed line is import matplotlib.pyplot as plt.
Quiz
Which line correctly loads pyplot under the short name plt?
- import matplotlib.pyplot as plt
- import matplotlib as plt
- import pyplot as matplotlib
- import plt from matplotlib
Show the answer
A. import matplotlib.pyplot as plt · The video writes import matplotlib.pyplot as plt, which loads the pyplot module and names it plt.
2:00 watch the momentWhat is the standard short name for pandas used in this chapter?
- pd
- pa
- pdas
- p
Show the answer
A. pd · The video imports pandas as pd, the convention used for pandas.
2:00 watch the momentWhat should you do if running the import cell gives a ModuleNotFoundError?
- Install the missing library, for example with pip install, then run the cell again
- Rename the cell and ignore the error
- Delete the numpy line because numpy is optional
- Restart the computer and keep the same code
Show the answer
A. Install the missing library, for example with pip install, then run the cell again · The video says that if an import fails, the library is not installed and you should install it with pip or use a distribution like Anaconda.
1:00 watch the moment
Lessons 2–7 and the final project
They open with Beyond Pass: every lesson of every course, in full. One payment: $14.99 for 90 days or $29 for a year. No subscription.
Made by Beyond from the video “Intro to Data Visualization in Python with Matplotlib! (line graph, bar chart, title, labels, size)” by Keith Galli. The video plays in YouTube's own player. Watch it on YouTube · All lessons
