Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook HW4.ipynb

149 views
Kernel: Python 2 (SageMath)

Tian Qiu

**Exercise:**

Write a function that takes as an argument a positive integer, and generates a list of integers between 0 and that number, separated by 1. That is, if you entered 5, the function should generate

0,1,2,3,4,5 ```
def print_list(n): list = [] i = n for x in range (n + 1): list.append(n - i) i = i - 1 print(list) print_list(5)
[0, 1, 2, 3, 4, 5]

**Exercise:**

Create a 1-d numpy array (call it ```ts``` for timestamps), with increasing integers from 3 to 300 (including 3 but not 300) with increment of 3, and print the array

import numpy as np from __future__ import print_function #clean up the print functions ????????? ts = np.arange(start = 3, stop = 300, step = 3) print("ts: ",ts)
ts: [ 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147 150 153 156 159 162 165 168 171 174 177 180 183 186 189 192 195 198 201 204 207 210 213 216 219 222 225 228 231 234 237 240 243 246 249 252 255 258 261 264 267 270 273 276 279 282 285 288 291 294 297]

**Exercise **

Modify the above plot in the following ways:

  1. Add a title to each subplot, and the figure as a whole
  2. Label all axes
  3. Modify the x-labels on the lower-left plot such that there is a tick every 0.5 points (0, 0.5, 1, 1.5, etc).
import matplotlib.pyplot as plt import numpy as np from __future__ import print_function import matplotlib.gridspec as gridspec t = np.arange(0., 5., 0.1) fig=plt.figure() # Create grispec object and define each subplot gs = gridspec.GridSpec(3, 3) ax0 = plt.subplot(gs[0, 0]) # Top left corner ax1 = plt.subplot(gs[0, 2]) # Top right corner ax2 = plt.subplot(gs[2, :]) # Bottom, span entire width ax0.plot(t, np.cos(5 * t), c='b') ax1.plot(t, np.exp(-1 * t), c='g') ax2.plot(t, np.cos(5 * t) * np.exp(-1 * t), c='k') ax2.set_xticks(np.arange(min(t), max(t)+1, 0.5)) # modify the x-labels on the lower plot # Label axises for all the subplots ax0.set_xlabel('x axis',fontsize=14) ax1.set_xlabel('x axis',fontsize=14) ax2.set_xlabel('x axis',fontsize=14) ax0.set_ylabel('y axis',fontsize=14) ax1.set_ylabel('y axis',fontsize=14) ax2.set_ylabel('y axis',fontsize=14) # Set titles for all the subplots and the whole plot fig.suptitle('This is the figure title',fontsize=14); ax0.set_title("Title for first plot") ax1.set_title("Title for second plot") ax2.set_title("Title for third plot")
<matplotlib.text.Text at 0x7f8af5aff250>
Image in a Jupyter notebook