Narration 1 00:00:00 --> 00:00:05 Hello friends and welcome to the tutorial on creating simple plots using iPython. 2 00:00:06 --> 00:00:09 I hope you have iPython running on your computer. 3 00:00:10 --> 00:00:19 At the end of this tutorial, you will be able to:

Create simple plots of mathematical functions. Use the Figure window to study plots better.

4 00:00:20 --> 00:00:21 Let's start ipython. 5 00:00:22 --> 00:00:34 Open the terminal and type: ipython -pylab and hit Enter. 6 00:00:35 --> 00:00:38 Pylab is a python library which provides plotting functionality. 7 00:00:39 --> 00:00:42 It provides many other important mathematical and scientific functions. 8 00:00:43 --> 00:00:54 After running IPython -pylab in the shell, you will see some information about 'ipython' and pylab followed by the In prompt. 9 00:00:55 --> 00:01:01 But, if you get an error like 'ERROR: matplotlib could NOT be imported! Starting normal IPython.' 10 00:01:02 --> 00:01:07 then you will have to install the matplotlib and run this command again. 11 00:01:08 --> 00:01:18 Now type: 'linspace' followed by a '?' mark in your ipython shell. 12 00:01:19 --> 00:01:28 As the documentation says, it "returns 'num' evenly spaced samples, calculated over the interval 'start' and 'stop'". 13 00:01:29 --> 00:01:32 To illustrate this, let's try to generate 100 points. 14 00:01:33 --> 00:01:46 Type: linspace within brackets 1 comma 100 comma 100 and hit Enter. 15 00:01:47 --> 00:01:51 As you can see, a sequence of numbers from 1 to 100 appears. 16 00:01:52 --> 00:01:56 Now let's try to generate 200 points between 0 and 1. 17 00:01:57 --> 00:02:10 we do that by typing linspace within brackets 0 comma 1 comma 200. 18 00:02:11 --> 00:02:17 Here, 0 is the start, 1- the stop and 200- the number of points. 19 00:02:18 --> 00:02:23 In linspace, the start and stop points can be integers, decimals or constants. 20 00:02:24 --> 00:02:29 Let's try and get 100 points between minus 'pi' to 'pi'. 21 00:02:30 --> 00:02:33 Here 'pi' is a constant defined by pylab. 22 00:02:34 --> 00:02:51 Save this to the variable, say 'p'. 23 00:02:52 --> 00:03:04 If we now type 'len' within brackets 'p', we will get the no. of points. 24 00:03:05 --> 00:03:10 len function gives the no. of elements of a sequence. 25 00:03:11 --> 00:03:49 Let's try and plot a cosine curve between minus 'pi' and 'pi'. For this, we use the plot command. Here, cos(p) gets the 'cosine' value at every point corresponding to point 'p'. 26 00:03:50 --> 00:03:56 We can save cos(p) to variable 'cosine' and then plot it using the plot function. 27 00:03:57 --> 00:04:18 Now, to clear the plot, we use the clf closing brackets function. 28 00:04:19 --> 00:04:24 This is done because, if we wish to make another plot, it will overlap the previous plot. 29 00:04:25 --> 00:04:33 As we do not wish to clutter the area with overlaid plots, we just clear it with clf(). 30 00:04:34 --> 00:05:03 Now, let's try a 'sine plot'. 31 00:05:04 --> 00:05:10 We can study the plot better on the plot window by using the various options available on it. 32 00:05:11 --> 00:05:13 Let us have a look at these options. 33 00:05:14 --> 00:05:25 As we can observe, moving the mouse pointer along the plot gives us the location of each point on the plot. 34 00:05:26 --> 00:05:29 To the bottom left of the window, there are a few buttons. 35 00:05:30 --> 00:05:34 The right most among them is for saving the file. 36 00:05:35 --> 00:05:47 Just click on it and type the file name. 37 00:05:48 --> 00:05:59 We will save the file by the name 'sin_curve' in 'pdf' format. 38 00:06:00 --> 00:06:04 As you can see, we can specify the format of file from the drop-down. 39 00:06:05 --> 00:06:16 Formats like png, eps, pdf, ps are available. 40 00:06:17 --> 00:06:24 Left to the save button is the slider button by which we can specify the margins. 41 00:06:25 --> 00:06:29 Left to this is the zoom button by which we can zoom into the plot. 42 00:06:30 --> 00:06:39 Just specify the region to zoom into. 43 00:06:40 --> 00:06:49 The button to the left of it can be used to move the axes of the plot. 44 00:06:50 --> 00:06:59 The next two buttons with left and right arrow icons change the state of the plot and take it to the previous state it was in. 45 00:07:00 --> 00:07:05 It more or less acts like the back and forward button in a browser. 46 00:07:06 --> 00:07:12 The last one is home, referring to the initial plot. 47 00:07:13 --> 00:07:19 Pause the video here, try out the following exercise and resume the video. 48 00:07:20 --> 00:07:25 Plot (sin(x) into sin(x)) by x. 49 00:07:26 --> 00:07:32 1. Save the plot by the sin square by x.pdf in pdf format. 50 00:07:33 --> 00:07:36 Second one. Zoom and find the maxima. 51 00:07:37 --> 00:07:43 3. Bring it back to initial position. 52 00:07:44 --> 00:07:48 Now, let's revise quickly what we have learnt today- 53 00:07:49 --> 00:07:51 To Study Ipython with pylab. 54 00:07:52 --> 00:07:56 To Use the linspace function to create num equally spaced points in a region. 55 00:07:57 --> 00:08:00 To Find the length of sequences using len function. 56 00:08:01 --> 00:08:04 Plot mathematical functions using plot. 57 00:08:05 --> 00:08:07 To clear drawing area using clf. 58 00:08:08 --> 00:08:22 To Use the UI of plot for studying it better and using functionality like save, zoom and moving the plots on x and y axis. 59 00:08:23 --> 00:08:30 Create 100 equally spaced points between minus pi by 2 and pi by 2? 60 00:08:31 --> 00:08:35 Second one is- How do you clear a figure in ipython? 61 00:08:36 --> 00:08:42 The third one- How do you find the length of a sequence? 62 00:08:43 --> 00:08:44 And the answers are- 63 00:08:45 --> 00:09:02 1. We use the command linspace within brackets minus pi by 2 comma pi by 2 comma 100 to create 100 equally spaced lines between the points minus pi by 2 and pi by 2. 64 00:09:03 --> 00:09:10 Second one- We use clf closing bracket function to clear a figure. 65 00:09:11 --> 00:09:19 Third one- len within brackets 'sequence_name' is the function used to find out the length of a sequence. 66 00:09:20 --> 00:09:23 Hope you have enjoyed and found it useful. 67 00:09:24 --> 00:09:29 Thank you!