1 of 25

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 25

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.

Any data from the old google drive will be made inaccessible by Thursday night.

2

3 of 25

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 25

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 25

Github Cloning Links

Log in link:

https://physhub.oarc.ucla.edu/hub/login?next=%2Fhub%2F

Physics 4AL:

https://physhub.oarc.ucla.edu/hub/user-redirect/git-pull?repo=https%3A%2F%2Fgithub.com%2Fuclaphysics4labs%2FPhysics_4AL_2023-24&urlpath=lab%2Ftree%2FPhysics_4AL_2023-24%2F

Please note that material pulled from this link will be periodically updated as the course is changing throughout the quarter. Your TA will tell you when and if changes were made so that you can update you repos.

6 of 25

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_2023-24/Unit 1/Unit 1 - Lab 1B.ipynb

UCLA Physics Department

6

7 of 25

Arrays

UCLA Physics Department

7

8 of 25

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([])

8

9 of 25

Arrays

  • Easy to generate arrays with known spacing between values.

  • Convenient to index or extract specific values in an array

Physics_4AL_2023-24/Practice Notebooks/Practice - Beginners_arrays.ipynb

9

10 of 25

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.

10

11 of 25

Plotting

UCLA Physics Department

11

12 of 25

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

12

13 of 25

Plotting

  • Module for plotting: matplotlib.pyplot

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

13

14 of 25

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

14

15 of 25

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()

15

16 of 25

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

16

17 of 25

Plotting with Error Bars

UCLA Physics Department

17

18 of 25

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

18

19 of 25

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

19

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

20 of 25

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

20

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

21 of 25

Polyfit

UCLA Physics Department

21

22 of 25

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

22

23 of 25

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_2023-24/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.

23

24 of 25

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_2023-24/Assignments/Post-Labs/Post-Lab-Week-1.ipynb

24

25 of 25

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_2023-24/Assignments/Pre-Labs/Pre-Lab-Week-2.ipynb

25