Difference between revisions of "EGR 103/Spring 2021/Lab 6"

From PrattWiki
Jump to navigation Jump to search
(6.4.3 Chapra 3.26)
 
(2 intermediate revisions by the same user not shown)
Line 33: Line 33:
  
 
=== 6.4.3 Chapra 3.26 ===
 
=== 6.4.3 Chapra 3.26 ===
'''Important''': If your computer is struggling to get 10000 dots to work, you can reduce this to 1000!
+
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.
  
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 <code>color</code> 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 useAs an example, agents could either use <code>color='m'</code> or <code>color=(1, 0, 1)</code>.  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:
+
There are two different commands for plotting a bunch of points: <code>ax.plot()</code> and <code>ax.scatter()</code>.  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 tableFor the first figure you make, you will be using the <code>ax.plot</code> version because you will be using the default color (though not the default marker size) for the dots.
<syntaxhighlight lang=python>
 
import numpy as np
 
import matplotlib.pyplot as plt
 
  
def scaled(val, minval, maxval):
+
For the next four figures, you will be using a [https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.scatter.html scatter plot] and looking at how to use the ''marker'', ''s'', ''color'' or ''c'', and ''cmap'' kwargs.
    return (val-minval)/(maxval-minval)
 
  
x = np.random.normal(0, 1, 1000)
+
* 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 <code>marker="X"</code> 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.
y = np.random.normal(0, 1, 1000)
+
* The marker size can be given with the <code>s=N</code> 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 </code>color=</code> kwarg.  There are '''nine''' different ways to specify a color - see details at [https://matplotlib.org/stable/tutorials/colors/colors.html Specifying Colors] at matplotlib.org.  For now, it is probably easiest to stick with the [https://matplotlib.org/stable/gallery/color/named_colors.html named colors].  The only one of the five plots where you want everything to be the same color was already done above with the <code>plot()</code> command.
 +
** If you want the points to take on different colors based on some function, use the <code>c=</code> possibly in conjunction with the <code>cmap=</code>kwarg. 
 +
*** You can give the <code>c</code> an array the same size and shape of the data set you are plotting and then specify a colormap (see the list at [https://matplotlib.org/stable/tutorials/colors/colormaps.html Choosing Colormaps]).  The computer will figure out the minimum and maximum values in the array you gave <code>c</code> and will assign the "left" colors in the map to data points corresponding to the lowest values in the <code>c</code> array and the "right" colors to the highest values.  Here are several examples of that, using different functions for the <code>c</code> 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 <code>c</code><br><html><iframe src="https://trinket.io/embed/python3/c4e53b3594?start=result" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 +
</html>
 +
*** You can give the <code>c</code> 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 <code>np.block()</code> command allows you to build a 2-D matrix out of other 2-D matrices. <br><HTML><iframe src="https://trinket.io/embed/python3/8f493a60ec?start=result" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe></html>
  
 
+
You should make five plots:
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))
 
</syntaxhighlight>
 
 
 
You should make four plots:
 
 
* All the dots slate blue
 
* All the dots slate blue
* Dots range from black at the bottom to pure blue at the top
+
* The dots are colored based on the value of $$n*m$$ using a built-in color map of your choosing
* Each dot is a total random color
+
* 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
 
* Your own mapping - you will need to describe the math and the color scheme in the lab report
  
 
{{Protected Class Document}}
 
{{Protected Class Document}}

Latest revision as of 05:08, 3 March 2021

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

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.

You should make five plots:

  • 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