Lessons · Coding · beginner · Keith Galli

Intro to Data Visualization in Python with Matplotlib! (line graph, bar chart, title, labels, size)

The video “Intro to Data Visualization in Python with Matplotlib! (line graph, bar chart, title, labels, size)” by Keith Galli as 7 lessons, with the idea explained, the code from the screen, steps to try in the practice terminal, practice problems with solutions and a quiz that leads back to the moment.

7lessons
11pieces of code
18exercises
23questions
33min of video
Open the lessons →Lesson 1 is free · Beyond Pass opens all
Intro to Data Visualization in Python with Matplotlib! (line graph, bar chart, title, labels, size), video by Keith Galli▶ 32:32 · Keith Galli
1234567
0:007 lessons across the whole video32:32

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. 1

    Setting up imports for matplotlib, numpy, and pandas Free

    0:00–2:262 min of video

    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. 2

    Your first line graph, and how to read the matplotlib docs Beyond Pass

    2:26–4:502 min of video

    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. 3

    Adding a Title, Axis Labels, and Custom Fonts Beyond Pass

    4:50–8:534 min of video

    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. 4

    Controlling Tick Marks on Each Axis Beyond Pass

    8:53–11:122 min of video

    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. 5

    Adding legends, colors, markers, and line styles Beyond Pass

    11:12–17:416 min of video

    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. 6

    Plotting Functions with NumPy, Dashed Projections, Figure Size, and Saving Beyond Pass

    17:41–26:599 min of video

    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. 7

    Bar charts, hatch patterns, and wrapping up Beyond Pass

    26:59–32:326 min of video

    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.

0:00 to 2:26 in the video

Practice terminal

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

Codepython 1:51 on screen
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

Run 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

  1. Open a Jupyter notebook and create a new cell at the top, under a heading such as Load Necessary Libraries. 1:51
  2. Type the first import line exactly as shown. 1:51
    $ import matplotlib.pyplot as plt
    You should see: The cell shows the line; nothing runs yet.
  3. Add the numpy import on the next line. 2:00
    $ import numpy as np
    You should see: Two lines in the cell.
  4. Add the pandas import on the third line. 2:00
    $ import pandas as pd
    You should see: Three import lines in the cell.
  5. 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

Practice terminal
$ 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.
Practice terminal
$ 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.
Practice terminal
$ pip install matplotlib
Successfully installed matplotlib-3.x
Why: 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

Do it Solution checked

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
  1. Click into an empty code cell.
  2. Type the three import lines exactly with the aliases plt, np and pd.
  3. Run the cell with Shift+Enter.
  4. Check that there is no error message.

The cell runs with no error and plt, np and pd are available to later cells.

Explain Solution checked

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
  1. It loads the numpy library into your program.
  2. The as np part gives it a short name, so you write np.array instead of numpy.array.
  3. 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.

Explain Solution checked

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
  1. With import matplotlib as plt, the name plt stands for the whole matplotlib package.
  2. The plotting functions live in the pyplot module, so the import must name it.
  3. Write import matplotlib.pyplot as plt.

The import skips pyplot. The fixed line is import matplotlib.pyplot as plt.

Quiz

  1. Which line correctly loads pyplot under the short name plt?

    1. import matplotlib.pyplot as plt
    2. import matplotlib as plt
    3. import pyplot as matplotlib
    4. 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 moment
  2. What is the standard short name for pandas used in this chapter?

    1. pd
    2. pa
    3. pdas
    4. p
    Show the answer

    A. pd · The video imports pandas as pd, the convention used for pandas.

    2:00 watch the moment
  3. What should you do if running the import cell gives a ModuleNotFoundError?

    1. Install the missing library, for example with pip install, then run the cell again
    2. Rename the cell and ignore the error
    3. Delete the numpy line because numpy is optional
    4. 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

Open lesson 1, with the video →

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