1 of 20

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 20

Outline of Lab 1B

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

UCLA Physics Department

2

3 of 20

Arrays

UCLA Physics Department

3

4 of 20

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.

4

5 of 20

Arrays

  • Easy to generate arrays with known spacing between values.

  • Convenient to index or extract specific values in an array

Arrays practice notebook

5

6 of 20

TA Checkpoint 1 - Using Arrays and Slices

  • For a matrix, we can extract values for each row and column:

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

  • Download this Python notebook, place it in your drive (not the shared one), and complete problem 1. Show your TA when you complete the problem.

6

7 of 20

Plotting

UCLA Physics Department

7

8 of 20

TA Checkpoint 2 - Reading data

  • Download this dataset and upload to your Google drive (the same location as your notebook).
  • Allow Google colab to access your Google drive for files by mounting your drive.
  • We can then use np.loadtxt() to read the data.
  • Select the “Files” button on the left side on your notebook to locate the file.
  • Then you can right click the file name to “Copy Path” of the file.
  • Video lecture - Reading data files

8

9 of 20

Plotting

  • We need to import a new module for plotting (matplotlib.pyplot)

  • With this module we can create plots and add axes labels, titles, legends etc.

9

10 of 20

Axes, Axis Labels, and Titles

  • Separate the x-axis and y-axis values if they are not separated

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

Video lecture on Plotting

10

11 of 20

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

11

12 of 20

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

12

13 of 20

Plotting with Error Bars

UCLA Physics Department

13

14 of 20

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. If our ruler can measure a minimum of 0.5 cm, then that is our error.
  • What is the error in our data collected for each distance?
    • It is the standard deviation of the 100 points that we collected. (Use np.std() for this error)

We can use error bars to represent these errors in the plot.

14

15 of 20

Error bars

  • Some sources of error will result from uncertainties of our measurement tools (like ruler), and some from variation in the measurements (like standard deviations).

  • Save the errors for each point as arrays. One array for the x-axis errors and another for the y-axis errors.

15

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

16 of 20

Error Bars

  • plt.errorbar generates a scatterplot where each of the points shows the associated error bars
  • By default, plt.errorbar also connects these points with lines. To change this, we can specify a format (given by fmt). More information on formats can be found in the documentation

16

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

17 of 20

Polyfit

UCLA Physics Department

17

18 of 20

Polyfit

  • Polyfit helps us by finding the least square polynomial fit of a dataset. This means finding 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

18

19 of 20

TA Checkpoint 4 - Polyfit

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

Plotting and polyfit practice notebook

  • 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.
  • Python Notebook

19

20 of 20

Post-Lab Requirements for lab 1B

  • Screenshot of your completed Google Collab notebook for Checkpoint 1, 2 3 and 4
  • Add all the requirements in the Post-Lab template here

20