Exercises for Lesson 12

Back to Lesson 12

Exercise 1: Graphing data

The library Matplotlib makes it incredibly easy to visualize data. If you don’t have it already, you can get it from the command prompt (search “cmd” in Windows or “Terminal” on a Mac), with the following command:

pip3 install matplotlib

Let’s make a simple plot:

from matplotlib import pyplot as plt # new!

def main():
    x_vals = [1,2,3,4]
    y_vals = [1,4,9,16]

    plt.plot(x_vals, y_vals)
    plt.show()

if __name__ == "__main__": # weird but good to have
    main()

Exercise 2: Customizing a graph

You can also look at Matplotlib samples to find out how to customize the plot with a title, legend, etc.

Spend a few minutes exploring until you have a legend, title, and axis labels.

Here is an example graph:

<image: simple line graph>

Exercise 3: A different graph

How would you draw a bar graph instead of the line graph? Explore the Matplotlib samples to find a relevant code sample.

Back to Lesson 12