1 of 24

Unit 1 - Training�Lab 1B : Arrays and Plots in Python

UCLA Physics Department

1

University of California, Los Angeles

Department of Physics and Astronomy

​

Physics 4AL

​

2 of 24

Data Storage

For this class, you will collect data in groups, that data needs to be saved to a private google drive.

This google drive folder needs to be shared with all members of your table and your section TA. It is part of your postlab this week to create and share that folder.

​

2

3 of 24

Assignments

  • There are two kinds of assignments (15% + 15%) if submission is required.
    • Pre-labs that are due 2 hours before the first lab session weekly.
    • Post-labs that are due on Sundays after the second lab weekly at 11:59 pm.
  • A team report is due at the end Unit 2 (10%).
  • A team report is due at the end Unit 3 (15%).
  • The Final Project includes a Presentation (15%) and Written Report (15%).
  • All Assignment submissions: PDF to Gradescope.
  • Attendance is worth 10% and pre-lab survey is worth 2%.
  • Attendance: Max 4 missed unexcused absence before dropped.
  • Cleaning up lab/table is worth 3%.
  • All of the above sum to 100%.

Grading info: https://www.uclaphysics4labs.org/4al-grading-policy.html

​

​

3

4 of 24

Online Lab Resources and Format

​

​

  • Gradescope Linked on Bruinlearn

​

    • Assignments: PDF submission for grading
    • Join Gradescope for your lab section with the entry code.
    • Pre-lab and post-lab assignment templates are on the lab website. Make a duplicate of the slides (or of the Python notebook), meet the requirements, download as a PDF and submit on Gradescope.

​

​

4

5 of 24

Outline of Lab 1B

  • 1B In-Lab
    • Arrays in python.
    • Plotting in python.
    • Polyfit in python.

​

In this lab, we will be using the following Jupyter Notebook found in your Hub:��Physics_4AL/Unit 1/Unit 1 - Lab 1B.ipynb

​

UCLA Physics Department

5

6 of 24

Arrays

UCLA Physics Department

6

7 of 24

Arrays

  • Arrays are a list or matrix of numbers.

​

​

  • Convenient - If you run a mathematical operation on an array, it will carry out the operation on all the elements of the array.
    • For example, converting many values in millimeters to centimeters.

​

  • Can find mean, standard deviation etc. of the values in an array.
  • Define using the numpy module : np.array([])

7

8 of 24

Arrays

  • Easy to generate arrays with known spacing between values.

​

​

​

  • Convenient to index or extract specific values in an array

​

​

​

Physics_4AL/Practice Notebooks/Practice - Beginners_arrays.ipynb

8

9 of 24

TA Checkpoint 1 - Using Arrays and Slices

  • For a matrix, we can extract values for each row and column:
    • element = array_name[row_number, col_number]

​

​

​

​

​

  • We can extract everything in a given row or column of a matrix by taking a slice with ‘:’
    • ‘:’ means, ‘all indices in row/column’

​

​

​

  • Open the lab 1B notebook and complete problem 1. Show your TA when you complete the problem.

9

10 of 24

Plotting

UCLA Physics Department

10

11 of 24

TA Checkpoint 2 - Reading data

  • Download this dataset and upload to your JupyterHub (the same location as your notebook).
  • np.loadtxt() to read the data.
  • You can right click the file name to “Copy Path” of the file.
  • Video lecture - Reading data files

11

12 of 24

Plotting

  • Module for plotting: matplotlib.pyplot

​

  • We can create plots and add axes labels, titles, legends etc.

​

​

​

​

​

​

​

12

13 of 24

Axes, Axis Labels, and Titles

  • Get x-axis and y-axis from the original data - By separation using slicing (using ‘:’)

​

​

​

  • Plot using plt.scatter or plt.plot (plt.scatter only marks the points, plt.plot connects the points with lines)

​

​

​

​

​

Video lecture on Plotting

13

14 of 24

Labelling Plots

  • You can use the ‘label’ keyword to label your scatterplots:

​

  • Then, use plt.legend() to generate a plot with the appropriate labels

​

​

​

​

​

​

​

​

  • Each label you provide will be included in the legend generated by plt.legend()

​

​

​

14

15 of 24

TA Checkpoint 3 - Plotting

  • We can also use the function plt.plot() to generate a plot with lines between the points

​

​

​

​

​

​

​

​

  • Complete the scatter plot section of problem 2 and show it to your TA

15

16 of 24

Plotting with Error Bars

UCLA Physics Department

16

17 of 24

Errors in distance/data collection

  • What is the error in our distance measurement?
    • It is the minimum quantity that we can possibly measure with our apparatus.
    • Example:
      • A ruler can measure a minimum of 0.5 cm
      • error = 0.5 cm
  • What is the error in our data collected for each distance?
    • Example:
      • Sample 100 points from a data set
      • Error = standard deviation of the points error = np.std()
  • Represent errors in a plot: error bars

17

18 of 24

Error bars

  • Sources of error
    • from uncertainties of our measurement tools (like ruler)
    • from variation in the measurements (like standard deviations)
    • etc.
  • Plotting error bars:
    • Save the errors for each point as arrays.
    • An x-axis error array and an y-axis error array

18

  • Use plt.errorbar to plot the points and the associated errors

19 of 24

Error Bars

  • Plt.errorbar:
    • a scatterplot with the associated error bars for each data point
    • By default, plt.errorbar also connects these points with lines. We can specify a format (given by fmt).
    • More information on formats can be found in the documentation

19

  • To use this, we need :
    • the x and y data
    • any associated errors

20 of 24

Polyfit

UCLA Physics Department

20

21 of 24

Polyfit

  • Polyfit finds the least square polynomial fit of a dataset.
    • least square polynomial fit finds the best fitting curve to a given set of points by minimizing the sum of squares (the distances between the data points and the best-fit curve).
  • It takes 3 different inputs : x-axis data, y-axis data, and the polynomial degree.
  • We can store the results of polyfit in an array.
    • The zeroth element of the array is the coefficient of the highest degree of the polynomial.

�

Video lecture on Polyfit

​

​

​

​

​

Video lecture on Polyfit

21

22 of 24

TA Checkpoint 4 - Polyfit

  • We can use the array to plot the best-fit curve and look how it compares to the data.

​

​

​

​

​

​

Physics_4AL/Practice Notebooks/Practice - Plotting tutorial.ipynb

  • Complete problem 2 in the Python notebook you started at TA Checkpoint 1 and show your results to your TA. The completed notebook (including problem 3) will be part of the post-lab for week 1.

22

23 of 24

Post-Lab Requirements for lab 1B

  • Your post lab for Lab 1B will include exercises from Checkpoints 1-4. You can transfer work down from other notebooks and paste them into your Post-Lab notebook.
  • Please complete the post lab assignment found in your JupyterHub Repo.
    • Physics_4AL/Assignments/Post-Labs/Post-Lab-Week-1.ipynb

​

​

​

​

23

24 of 24

Pre-Lab Requirements for lab 1C

  • There will be a pre lab assignment due before the start of Lab 1C.
  • Please complete the post lab assignment found in your JupyterHub Repo.
    • Physics_4AL/Assignments/Pre-Labs/Pre-Lab-Week-2.ipynb

​

​

​

​

24