EGR 103/Spring 2021/Lab 6
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
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
There are two different commands for plotting a bunch of points: ax.plot()
and ax.scatter()
. The first is better when everything is going to be a single color and the second is better if you want to have the colors determined by some function. For the first figure you make, you will be using the ax.plot
version because you will be using the default color (though not the default marker size) for the dots.
For the next three figures, you will be using a scatter plot and looking at how to use the marker, s, color or c, and cmap kwargs.
- The scatter command does not have a format string the way the plot command does. If you want to specify a marker, you have to include a
marker="X"
argument where X is the marker you want to use. The default case is to use circles. - The marker size can be given with the
s=N
kwarg. - The color can be given in several different ways:
- If you want all the points to be the same color, use the color= kwarg. There are nine different ways to specify a color - see details at Specifying Colors at matplotlib.org. For now, it is probably easiest to stick with the named colors.
- If you want the points to take on different colors based on some function, use the
c=
possibly in conjunction with thecmap=
kwarg.- You can give
c
an array the same size and shape of the data set you are plotting and then specify a colormap (see the list at Choosing Colormaps). The computer will figure out the minimum and maximum values in the array you gavec
and will assign the "left" colors in the map to data points corresponding to the lowest values in thec
array and the "right" colors to the highest values. Here are several examples of that, using different functions for thec
array. The last two examples also show how to get a colorbar attached to the axis. Note that in both cases the colorbar stretches over its full range but that the numerical values associated with each color changes because of the different ranges of the array assigned toc
- You can give
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