Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Programming Workshop 6 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d"— Presentation transcript:

1 Introduction to Programming Workshop 6 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d n.a.dipper@durham.ac.uk

2 Contents Plotting Graphs with MatPlotLib Saving and loading data to/from file Exercises

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

4 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:

5 Plot of y=2x What is missing from the graph?

6 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:

7 Plot of y=2x with labels

8 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/

9 Examples http://matplotlib.org/gallery.html

10 Examples http://matplotlib.org/gallery.html

11 Examples http://matplotlib.org/gallery.html

12 Examples http://matplotlib.org/gallery.html

13 Examples http://matplotlib.org/gallery.html

14 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.

15 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

16 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=2x 2 + 3x + 4 over the range of x from 0.0 to 9.0 with axis labels and a title.

17 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 –  to +  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()


Download ppt "Introduction to Programming Workshop 6 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d"

Similar presentations


Ads by Google