EGR 103/Fall 2021/Lab 6

From PrattWiki
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 column 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. Be sure to include the sign change in the last equation! The values for $$L$$, $$E$$, $$I$$, and $$w_0$$ are given in the problem. There is a value for spacing between points in the problem but you will be ignoring it - instead using 101 linearly spaced points between 0 and 600 cm.

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. Note that your butterfly() function should take an array as a required argument and return two arrays - the function should not do the plotting! Your code should look like:

# %% Imports
# your imports here

# %% Function definition
def butterfly(t_array):
# your function code here
    return xvals, yvals

# %% main script
if __name__ == "__main__":
    # code to generate t and calculate x and y using butterfly()
    # code to generate and save figure

6.4.3 Chapra 3.26

Once you have used the steps in the assignment to generate your 10000 points, make array versions for yourself with code like:

ma = np.array(m)
na = np.array(n)

since you will be doing math with these values later.

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 or some additional table. 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 four 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. You will want to use points for these last four plots.
  • The marker size can be given with the s=N kwarg. Make the markers size 1 for the remaining four plots.
  • 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. The only one of the five plots where you want everything to be the same color was already done above with the plot() command.
    • If you want the points to take on different colors based on some function, use the c= possibly in conjunction with the cmap=kwarg.
      • You can give the 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 gave c and will assign the "left" colors in the map to data points corresponding to the lowest values in the c array and the "right" colors to the highest values. Here are several examples of that, using different functions for the c 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 to c
      • You can give the c an array with the same number of rows as the number of entries in $$x$$ and $$y$$ where each row either has three entries: the red, green, and blue value for that marker. Building maps like this can be a little complicated because you need to make sure the values are normalized to be between 0 and 1 and you need to build an Nx3 array of values. Here are some examples of creating random colors, making a map where the red level is dependent on the $$x$$ coordinate and the other components are off, making a map where the blue level is dependent on the $$y$$ coordinate and the other components are off, and finally making a map where the red and blue components above come into play. The np.block() command allows you to build a 2-D matrix out of other 2-D matrices.
      • Here is another set of examples creating custom colormaps, but with fewer points and with the various normalized arrays and color tables printed out.

You should make five plots and include all of them in your lab report:

  • All the dots slate blue
  • The dots are colored based on the value of $$n*m$$ using a built-in color map of your choosing
  • Each dot is a totally random color
  • Dots range from bright green at the bottom to black at the top
  • Your own mapping - you will need to describe the math and the color scheme in the lab report
Class Document Protection