Introduction to Programming Workshop 6 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room...

Preview:

Citation preview

Introduction to ProgrammingWorkshop 6

PHYS1101 Discovery Skills in Physics

Dr. Nigel DipperRoom 125dn.a.dipper@durham.ac.uk

Contents

• Plotting Graphs with MatPlotLib

• Saving and loading data to/from file

• Exercises

MatPlotLib

• There are many plotting packages around!• MatPlotLib is the most popular. Please use it.• There are excellent web pages at:

matplotlib.org

• Importing matplotlib:• Confusingly there are many ways to import it!• Let’s just stick to one of these (you will see others):

import matplotlib.pyplot as pyplot• Get a python prompt now in your terminal• import numpy and matplotlib (as above)

A first plot

• We generally want to plot a function y=f(x) against x• So let’s plot the function y=2x• Make an array called x of real numbers from 0.0 to 9.0• Make an array called y of the x array multiplied by 2.0• Now at the Python prompt type:

>>> pyplot.plot(x,y)• This tells python to create an object that is a plot of x against y.• It will write a description of the object on the screen but won’t plot it!• To make it show on the screen type:

>>> pyplot.show()• The show() method knows where to find the plot and plots it for you• You should see this:

Plot of y=2x

• What is missing from the graph?

Title and Axis Labels

• All graphs for ‘publication’ need a title and and axis labels – and units!

• Close the plotter. This will give you the python prompt back in your terminal.

• You still have the x and y arrays but must make a new plot and add the labels:

• These methods take a single string as a parameter.

>>> pyplot.plot(x,y)

>>> pyplot.xlabel(‘x value in radians’)

>>> pyplot.ylabel(‘f(x)’)

>>> pyplot.title(‘y=f(x)’)

>>> pyplot.show()

• The plot should now look like this:

Plot of y=2x with labels

Default plotting parameters

• There is a large number of plotting parameters that you can set.• These have all been set to defaults.• Leave the defaults for now. You can improve your graphs by

changing them to meet your requirements.

• Take a look for example at this on-line tutorial :

http://www.loria.fr/~rougier/teaching/matplotlib/

Examples

http://matplotlib.org/gallery.html

Examples

http://matplotlib.org/gallery.html

Examples

http://matplotlib.org/gallery.html

Examples

http://matplotlib.org/gallery.html

Examples

http://matplotlib.org/gallery.html

References

• The slides from the lectures are on DUO.

• We touched on graph plotting in lecture 4.

• Input and output of data from/to files was covered in lecture 5.

• The use of numpy.savetxt() and numpy.loadtxt() is covered in chapter 4 of the course notes.

Exercises

• These exercises are not (yet!) in the course notes.

• Both these slides and the worked solutions will be put up on DUO

• Please complete the exercises now or later at home.

• You will be asked to plot your data in the main project

Exercise 12.2

• Exercise 12.2• Write a function (as supplied for you in the mini-project) that plots any

y array against any x array• Add parameters to pass the x-axis, y-axis and title strings to the

function• Add some test code to your file to test the function by plotting the

polynomial: y=2x2 + 3x + 4 over the range of x from 0.0 to 9.0 with axis labels and a title.

Exercises 12.3 and 12.4

Exercise 12.3• Imagine you are saving data from an experiment in the lab• We will just save some dummy data to file• Write a function that generates sin and cos values for x in the range

–p to +p at a variable number of points. (Use numpy.linspace())• Save the x values, sin values and cos values to file using

numpy.savetxt()• Note that the data need to be sent to savetxt() as a tuple

Exercise 12.4• Write a function that reads back your data from file using

numpy.loadtxt() and displays it on a graph.• You will not be able to use your simple plotting function because you

are going to plot both sin and cos on the same graph.• You must add both sin and cos data sets to the plot before calling

pyplot.show()

Recommended