Exercise Notebook 6: Numpy

Exercise Notebook 6: Numpy#


You can download this notebook and the additional files by clicking the download button() in the top right and selecting “Exercise notebook”.

Exercise 6.1

Plot the functions \( f(x) = \sin(x) \) and \( g(x) = \cos(x) \) over the interval \( x \in [0, \ 2\pi] \). Ensure that your plots include appropriately labeled axes, a descriptive title, and a legend to distinguish between \( f(x) \) and \( g(x) \).

### use this cell block for the exercise
Exercise 6.2 Bar graph revisited

In the last notebook you made a bar graph of monthly rainfall amounts. Perhaps you wondered if it’s possible to place the bars side by side so they don’t overlap.

You can do this by shifting the x-coordinates of your bars, and setting the bar width using the width= argument to the bar graph function.

Let’s see how it works…

  • first copy your plotting code from the last exercise notebook.

  • use the width= argument of the plt.bar function to change the width of the bars.

  • now make a numpy array of the month list.

  • now you can easily add or subtract a single number from this array, and in this way you can shift the x-values for the different countries a little to the left or right when plotting the bars for that country.

Try out: make a plot where the bars for the different regions don’t overlap.

### use this cell block for the exercise
Exercise 6.3

In linear algebra you will learn about matrices, which are basically 2D arrays (a vector being the 1D equivalent): an \(m\times n\) matrix has \(m\) rows and \(n\) columns.

  • Create a 4x2 matrix M filled with different values you may choose yourself

You can use np.array(), and pass it nested lists (lists within a list), or you can use M = np.zeros((rows, columns)) and fill in the values afterwards by assigning values to the individual elements. Here rows and columns are the number of rows and columns you want. Note the double parentheses. The reason is that (rows, columns) is a tuple, and the np.zeros() function takes this tuple as it’s argument to create a matrix filled with zeros of the required dimensions.

  • Check the shape of your matrix, using M.shape

  • Try the transpose() function on your array (hint: M.transpose()). What did it do?

### use this cell block for the exercise
Exercise 6.4 Slicing matrices

As you have seen for lists and strings, numpy arrays can also be sliced using [start:end:step].

  • Use slicing to cut out the part

11, 12
15, 16

from matrix A defined below.

### use this cell block for the exercise
A = np.array([[ 1,  2,  3,  4],
              [ 5,  6,  7,  8],
              [ 9, 10, 11, 12],
              [13, 14, 15, 16],
              [17, 18, 19, 20]])
Exercise 6.5 Plot the RICO input data

  • load the data

  • create a separate array (with their own name) for each column

  • use subplots to plot several quantities side by side:

    fig, axes = plt.subplots(nrows=1, ncols=5, sharey=True, figsize=(13,4))
    
  • figsize=(13,4) to set the figure size in inches (width, height) at some assumed dots-per-inch value. It’s included here to make the plot a bit wider, so that the labels don’t overlap. Change the values a bit to see what happens.

  • to plot in the first subplot, use axes[0].plot(...)

  • to set x and y labels, use axes[0].set_ylabel(...)

  • use the y-axis of the plot for the height above ground, in this way it is the same for all subplots

  • … and that’s why we used sharey=True we tell matplotlib that the y-axis is to be shared

### use this cell block for the exercise