lab_matplotlib

Mighty MatPlotLib

Due: Feb 05, 23:59 PM

Learning Objectives

  • Introduce Python visualization with matplotlib
  • Practice common plot shapes (dotplot, line plot, bar chart)
  • Review list manipulations and paired list indexing
  • Explore fundamentals of math evaluation and numpy

Submission Instructions

Using the Prairielearn workspace, test and save your solution to the following exercises. (You are welcome to download and work on the files locally but they must be re-uploaded or copied over to the workspace for submission).

Assignment Description

Matplotlib is a library designed to add matlab plotting functionalities in Python. Over time it has grown to a huge library that supports all kinds of visualizations and integration with many data science libraries such as numpy and pandas and other packages such as scikit-learn are built on top of it. In this assignment, we will cover a ‘core’ set of useful visualization types and observe how other packages such as numpy are integrated into the workflow.

Warning: Debugging this assignment will be tricky – the autograder cannot display images. A number of test images have been provided for you to compare against but if you are unsure where your code is going wrong you are strongly encouraged to get help from course staff.

makeDotPlot()

# INPUT:
# fname, the filename for a PNG to be written
# Each of the following four lists is identically sized:
# xList, a list of numeric values representing the x-coordinates for a list of points
# yList, a list of numeric values representing the y-coordinates for a list of points
# shape, a list of strings representing the shape the point should be
# The shape should be a 'c', 's', or 't' (the only allowable values)
# Each shape is a patch that uses the provided coordinates and color with the following fixed parameters:
# 'c': Create a Circle patch with radius 0.05
# 's': Create a Rectangle patch with both width and height 0.1 
# 't': Create a RegularPolygon patch with 3 vertices and a radius of 0.07
# color, a list of colors representing the color of the point
# The color will always be a matplotlib interpretable color string as a single character
# OUTPUT:
# None, instead a PNG is saved to file at fname containing the contents of the dot plot
def makeDotPlot(fname, xList, yList, shape, color):

Representing data as a set of 2D points is a common way of visualizing raw data, clustered data, and principal component analyses (among many other use cases). Here we will practice our ability to plot specific paired points while reviewing the fundamentals of list indexing. Your task is to plot each individual point from four paired lists each giving a fraction of the points description.

Note that while the colors will be given matplotlib single-letter codes, the shapes must be hardcoded according to the above descriptions, each using a different patch function.

evaluateExpression()

# INPUT:
# fname, the filename for a PNG to be written
# xList, a list of numeric values representing the x-coordinates for a list of points
# yList, a list of numeric values representing the y-coordinates for a list of points
# linetype, an integer value representing one of four possible line styles
# '0' -- a solid black line
# '1' -- a dashed blue line
# '2' -- a solid red line with x's at data points
# '3' -- a dashed green line with circles at data points
# OUTPUT:
# An equal sized list yList consisting of the values of the input equation for each input value to x.
def makeLinePlot(fname, xList, yList, linetype):

Line plots are a staple of publications and being able to plot different styles of lines will make future experiments much easier – including our first mini-project! Once again, you have to write some hardcoded variants according to the description above.

randomHistogram()

# INPUT:
# fname, a string containing the filename for a PNG to be written
# maxInt, an integer containing the maximum value of the integer being generated
# trials, an integer containing the total number of random numbers being generated
# seed, an optional value for setting the random seed (Default value is None)
# OUTPUT:
# None, instead create a histogram that plots the count for each number from 0 to maxInt
# Every histogram should be colored blue and width rwidth=0.8 (a hist kwark)
def randomHistogram(fname, maxInt, trials, seed=None):

Bar charts and histograms are another very useful tool in your visualization toolkit. Here we will visualize the distribution of randint() directly by using a histogram to track the total counts of different values! What is the probability distribution of random? What happens as the number of trials grows?

NOTE: This question is deleted for SP 24.

evaluateExpression()

# INPUT:
# equation, a string input built using numbers, the variable (as letter) x, and standard math operations.
# xList, a list of numeric values representing the x-coordinates for a list of points
# OUTPUT:
# An equal sized list yList consisting of the values of the input equation for each input value to x.
def evaluateExpression(equation, xList):

Python has a built-in capacity to evaluate algebra expressions using variables, which frankly isn’t particularly useful when used to generate a single value. But when you want to simultaneously generate a whole list of output values given a list of input, there are many good ways to do this. Pick one that works for you!

animateExpression()

# INPUT:
# fname, the filename for a PNG to be written
# equation, a string input built using numbers, the variable (as letter) x, and standard math operations.
# xList, a list of numeric values representing the x-coordinates for a list of points
# OUTPUT:
# None, instead a PNG is saved that has a frame for each value in xList
def animateExpression(fname, equation, xList):

This last function is a challenging one but hopefully also a fun one! We will use animations using matplotlib for our second mini-project but for now its worth getting a fundamental understanding on how multiple matplotlib frames can be combined into a gif using FuncAnimate.

When creating the gif, you should set the axes of your image equal to the min and max value in the full list before building your animation. This means you must compute what the full list of points looks like but only plot i points for frame i. Note that frame 0, the empty frame, is already given in your starting code.