Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Day 09 Pre-Class Assignment

University of Missouri

✅  Put your name here

Python Modules: Matplotlib and NumPy

Official logo of the Matplotlib module in Python.

Credits: Wikipedia

Learning goals for today’s assignment

  • Import modules into Python and understand how to use them.

  • Use Python’s numpy module to do simple calculations

  • Use matplotlib and the pyplot submodule to make plots.

Assignment instructions

Watch the videos below, do the readings linked to below the videos, and complete the assigned programming problems. Please get started early, and come to office hours if you have any questions! Make use of Slack as well!

This assignment is due by 11:59 p.m. the day before class, and should be uploaded into appropriate the “Pre-class assignments” submission folder. Submission instructions can be found at the end of the notebook.


Before we dive into the pre-class assignment, here are some links that might be useful if you’re looking to learn some extra iPython (which is what the Jupyter Notebooks are built on) and Jupyter Notebook skills:

  • IPython tutorial -- this contains some very useful suggestions about IPython commands that allow you to get help on Python, figure out what specific variables or objects do, and many other things.

  • Jupyter notebook tips and tricks -- some clever things you can do with Jupyter notebooks

These are not things you have to learn, but for those looking to push their limits, these pages might be useful!


Python Modules: math and matplotlib.pyplot

The following video introduces the concept of Python “modules” which are extra software libraries that you can import into your Python environment in order to add new functionality to Python that it doesn’t have by default. Python modules are often created because they include commonly used and generally useful code, like mathematical functions or plotting routines.

You may have noticed that we’ve actually already been using one module quite regularly, the IPython.display module. From it, we import the YouTubeVideo function to embed and play YouTube videos.

Note: The words “module” and “library” can be used interchangeably

# Imports the functionality that we need to display YouTube videos in a Jupyter Notebook.
# You need to run this cell before you run ANY of the YouTube videos.
from IPython.display import YouTubeVideo

# Video on Modules in Python
# Make sure to watch it in full-screen mode!
YouTubeVideo("chBLLNBGoEE",width=640,height=360)  # modules and pyplot
Loading...

The numpy Module

In the video above, we reference the math module. While it is a useful and powerful module, we are going to put it aside in favor of an even more powerful module, “NumPy” or numpy (the Numerical Python module). The numpy module has many purposes beyond doing calculations that we will explore in later classes, but for now, we will only use it for numbers.

Watch this video for a brief introduction to using numpy for calculations (you might notice it is very similar to the math module for this purpose!).

YouTubeVideo("e0WUrM-P9QI",width=640,height=360)
Loading...

Useful references:

In addition to the video, the following references may be useful for your pre-class and in-class assignments:


Working with matplotlib

✅  Task 1:

The cell below contains four lists of data, which correspond to two sets of X and Y values. Use matplotlib to make a plot of these datasets. Make your plot with the following guidelines:

  • The first pair of lists (x1, y1) should be drawn with a thick, blue, dashed line.

  • The second pair of lists (x2, y2) should be drawn with red diamonds.

  • Make the width of the plot extend from 0 to 20 and height of the plot extend from 6 to 16. This will help make the image bit easier to see.

  • Add axis labels to the x- and y-axes, with whatever text you like.

Use the Pyplot Tutorial for inspiration, and in particular you can find instructions on how to create different types of characters and line types in the pyplot plot command documentation.

Note: It is important to remember that any time you need a module, you need to import it. A good coding practice is to put all of your import commands at the top of your code block. Try to get into the habit of always importing the modules you are going to need first, before writing the rest of your code. If you realize you need another module, add it to your list at the top, don’t just insert it into the body of the code randomly.

Another Note: Your plots might look different than the ones in the video because matplotlib has been updated to a new version since that video was made.

# imports the pyplot module from matplotlib
import matplotlib.pyplot as plt

x1 = [2,4,6,8,10,12,14,16,18]
y1 = [10,8.25,7.5,7,6.75,7,7.5,8.25,10]

x2 = [5, 15]
y2 = [15, 15]

# put your plotting commands below this comment!

Using the numpy module to compute new values and matplotlib to plot them

Now we want you to try to use various components of everything you just learned in the two videos above, and things you’ve learned in class up to this point, to make a new plot. This will require that you use the numpy module, the pyplot module, and probably loop (or two).

Just like with matplotlib, when you need/want to use numpy, you need to import that as well, we normally use import numpy as np -- make sure to put this in your code cell below!

✅  Task 2

Using the values from the x1 list above, make a plot of f(x)=sin(x)f(x) = \sin(x) using a green line and f(x)=cos(x)f(x) = \cos(x) using an orange line. Label your plot so that the x-axis reads “x”, and the y-axis reads “f(x)”.

Hint: The first step will be to figure out a way to calculate the sine and cosine values of x1 and store them. For calculating sine and cosine values, you can use np.sin() and np.cos(). For storing these new values in your list, you might want to review how the append() function works for lists.

Extra challenge: See if you can figure out how to use the label parameter in the plt.plot() function and the plt.legend() command to add a legend your plot that identifies each line appropriately.

# put your python commands here, make sure to include comments in your code using the "#" symbol
# make sure to import the numpy module



# Your plot will look very jagged. That's fine, you'll smoothen it for the next task

✅  Task 3

Now that you’ve been able to plot some functions that look like sine and cosine, we want to make them look more like sine and cosine by calculating more data points for our curves. One way of doing this is to create a more dense list of values that we will pass to the sine or cosine function. We created a list called x1 for you that you used for the plots you made above, but it didn’t have enough values to make the curves look very smooth.

Your goal is to generate a list called x_list of xx-values from 0 to 2π2\pi with a spacing of 0.1 between each value (you might want to use the numpy module for obtaining a value for π\pi, which can be accessed with np.pi). Then plot cosine and sine over this interval with the same axis labels and colors as the plot you made above.

Hint: To create your list, consider using a while-loop, that stops once the increment has surpassed 2π2\pi. The while-loop should append new values to x_list, starting at 0 and adding 0.1, appending that to the list, then adding 0.1 to the previous value, appending that value to the list, and so on. We will see other methods of creating lists like this in the future by some other very helpful modules.

(Please do not hard code the values of your list by hand. If you get stuck, just ask one of the instructors for help via Slack.)

# put your python commands here, make sure to include comments in your code using the "#" symbol
# this plot should look like the one from a math textbook

Checking in on prior knowledge

As we work on learning new things, it can be useful to check-in now and again on some of our previous skills and make sure they’re still fresh. Work through the following prompts to revisit your knowledge of using lists and loops.

Assigning animals

Imagine that you are studying broad trends across animals. You think it would be good idea to study animals separately based on their family rathen than lumping all of them together. You are give a list below (animal_list), where each entry is another list with two values: the animal’s name and its family.

✅  Task 4

You are only interested in studying the felines (members of the Felidae family). Write a piece of code that selects the animals to be studied further and appends their names to the list catlike.

Some optional starter code has been provided for you.

animal_list = [
    ['Gray fox', 'Canidae'],
    ['Jaguar', 'Felidae'],
    ['Canadian Lynx', 'Felidae'],
    ['Cloud lepard', 'Felidae'],
    ['Bobcat', 'Felidae'],
    ['Fennec Fox', 'Canidae'],
    ['Ocelot', 'Felidae'],
    ['Domestic cat', 'Felidae'],
    ['Red Fox', 'Canidae'],
    ['Cheetah', 'Felidae'],
    ['Lion', 'Felidae'],
    ['Swift Fox', 'Canidae'],
    ['Eurasian lynx', 'Felidae']
]
catlike = []

#Write your code here

#Optional starter code (uncomment the line below and use, if you wish)
# for jj in range(len(animal_list)):

Talking about code

Consider the following snippet of code:

animal_list[3]

✅  Task: In your own words, describe what the square brackets ([]) are doing in this code snippet.

Write your answer here

✅  Task: In your own words, describe what the number three (3) is doing in this code snippet.

Write your answer here

✅  Question: How would you describe what this code snippet is doing to someone that has never coded before?

Write your answer here


Follow-up Questions

Copy and paste the following questions into the appropriate box in the assignment survey include below and answer them there. (Note: You’ll have to fill out the section number and the assignment number and go to the “NEXT” section of the survey to paste in these questions.)

  1. What is the matplotlib Python package used for? Have you every used this package prior to this assignment? If so, what was the context in which you used it?

  2. What command do you need to use in your notebook to make sure extra packages like matplotlib or numpy are available for you to use?


Congratulations, you’re done!

Submit this assignment by uploading it to the course Canvas web page. Go to the “Pre-class assignments” folder, find the appropriate submission folder link, and upload it there.

See you in class!

Material drawn with permission from:
© Copyright 2023. Department of Computational Mathematics, Science and Engineering at Michigan State University

Adapted for:
© Copyright 2026, Division of Plant Science & Technology—University of Missouri