EGR 103/Fall 2020/Lab 6

From PrattWiki
Revision as of 16:10, 22 September 2020 by DukeEgr93 (talk | contribs) (6.4.1 Chapra 2.26)
Jump to navigation Jump to search

Errors / Additions

None yet!

6.1 Introduction

There are several different types of problem to work on this week. The ones in lab each have plots associated with them.

6.2 Getting Started

Beginning of Lab

6.3 apt

The third APT assignment is active. The APT page has some hints for how to solve problems. One quick note: a set is a collection of elements where each element is unique. If you cast some other collection (for example, a list or a string) as a set, the recast version will have as many elements as the number of unique entries in the original.

In [1]: set([1, 2, 3, 4, 5, 2, 4, 2])
Out[1]: {1, 2, 3, 4, 5}

In [2]: set("hello")
Out[2]: {'e', 'h', 'l', 'o'}

6.4 Individual Lab Assignment

6.4.1 Chapra 2.26

See the Python:Plotting/Subplots page for how to get a single solumn of plots with a shared $$x$$ axis. Also, use:

fig.set_size_inches(6, 8, forward=True)

just after you create the figure to make it 6" wide and 8" tall.

6.4.2 Chapra 2.27

See the Python:Plotting/Subplots page for how to get two rows of two columns of plots. If you use the subplots method to make a 2x2 array, see Python:Plotting#Example for how to remove unused axes.

6.4.3 Chapra 3.26

When you plot $n$ vs. $m$, use the format specifier '.' to plot dots. You will get 10000 slate blue dots since that is the default color for the first series plotted. You can also make each dot its own color by plotting it individually and using the color kwarg. This keyword argument expects either a nickname or a tuple of three numbers. The numbers need to be between 0 and 1 and will indicate how much red, green, and blue to use. As an example, agents could either use color='m' or color=(1, 0, 1). Here is a sample code that generates 1000 normally distributed random 2D locations, plots them all as blue dots first, then plots them going from black on the top to bright green on the bottom:

import numpy as np
import matplotlib.pyplot as plt

def scaled(val, minval, maxval):
    return (val-minval)/(maxval-minval)

x = np.random.normal(0, 1, 1000)
y = np.random.normal(0, 1, 1000)


fig, ax = plt.subplots(num=1, clear=True)
ax.plot(x, y, '.')

fig, ax = plt.subplots(num=2, clear=True)
for k in range(1000):
    ax.plot(x[k], y[k], '.', 
            color=(0, scaled(y[k], y.min(), y.max()), 0))

You should make four plots:

  • All the dots slate blue
  • Dots range from black at the bottom to pure blue at the top
  • Each dot is a total random color
  • Your own mapping - you will need to describe the math and the color scheme in the lab report
Class Document Protection