 Let us do a quick recap. We saw how to plot, very simple one line, describe the x and then simply say plot. We saw how to add some extra fittings to the plots like title, x axis, y axis, labels. We could change the color of the plot. We could change the thickness of the line of the plot. We could also add some annotations to inside the plots. Then we closed around the idea of interactive sessions, commands being available to us in a history and the ability to select, selectively save them into a script and run the script. That last part probably went away a little fast. So, let us do it again. We saved it. Then we run minus i. You will see the difference this time is the plot is not automatically shown. That is for a reason. When you interactively do anything, it is assumed that you want to see the result. When you run a script, it is assumed whatever you want to do, you will make it part of the script. So, there is a show command which you need to run in order to see the output. So, you can put the show command inside the script itself, but that is a very unlikely thing. That is not because the scripts by definition are not used for interactive work. Scripts are used to routinely get the thing done without having to go through step by step. Like I said before we broke, you rarely just want to show a plot a chart on the screen and leave it with that. You want to save it into a picture, paste it into a report and do something along those lines. For that we need the save fig command. Let us use it again and see. You could have given any name. The type of the file is decided by the extension you give. Save fig and save it as PNG, PDF, PS, EPS. Since you have done some latex, EPS files are the easiest to embed inside latex. You can see there is a file signed on PNG here. So, that file would be in your system also if you have saved it. Identify the default location where it saves. It will save in the current directory. You can of course specify a path and make it save somewhere else. So, this file can be now used in different context for you can embed it into a document, into a latex document, preparing a report etcetera. We made a comment till now whatever command you give is additive. It goes on adding to the existing command correct. Whatever output is there, any new command adds the output to the same. We did one thing in order to fix it. That was clear the picture using CLF. Let us take a slightly bad example. So that we can understand what point we are trying to make. So, what is this lint space all about? Starting from 0 to 50, 10 points. Now, the points are very, very coarse. As you can see it is a very funny sine curve because the number of points we have chosen are very, very small. So, it does not come out as smooth as we normally should get. Now, let us change it to instead of 10 points, we will change it to 500. So, it will be lot more points, lot more, lot smoother. You will find the picture is overlaid. This is the default behavior as we have noted, which is why we could add a title, we could add a label all separately. This is the default behavior of pile up to be specific. Often when we have multiple plots in the same picture, we need to explain what each is, which is where a legend comes into play. So, let us use a legend now. As you notice, it is almost always almost English commands. Title gives you a title and so on. Please note the square bracket. Why is it required? We already know square bracket is a list. Since a legend has to be a, has to be a list because there are multiple lines for each one of them there is to be a legend. So, it is a list. Please note we just said sine 10 and sine 5 and read. Python took care of identifying which one is which. So, this is a legend. The position of the legend can be changed. Sorry, I did not give the center in quotes. You have to give the center in quotes. You will find now the legend has moved to the center. So, there are different locations you can put it in. Sometimes you do not want to separate, you want to separate pictures. You do not want everything in the same place. For that we have a figure command. Let me clear this screen so that we can. Now, you notice there are two pictures. This is one, this is the other. One in blue and one in green. On a larger screen, you can see both of them side by side. So, the figure command essentially is a way of directing the commands. When I say figure one, it says whatever command I type now applies to figure one. If I say figure two whatever command I type now applies to figure two. So, if you look at the commands we have done, just look at it. It says figure one plot x comma sine x b, figure two plot x comma cos x g. So, this directed it. So, anything else I do. For example, I put a title here. It will apply to figure two. Well, the other one will not have a title because the command only applies to the figure for which you have chosen. That even includes the save command. Now, switch to figure one. Let us give a title. The close command will close the figure. Now, you can see there is only one picture. There is not any. The second one is not there because it has been closed. Now, you will not see a single picture. So, the close command is useful to this whole idea is multiple plots. The first type of multiple plots is to produce two separate plots. Sometimes, you want sub plots. The command to use is a sub plot. As you can see, it says two rows, one column, plot number one to be accessed. In the instead of figure one, now you say, you know what? I want a sub plot. There are going to be two rows, one column and the first one is what I am accessing. When we said figure one and figure two, they were completely separate plotting areas. When you say sub plot, it is in the same. As you can see, this is the idea of a sub plot. You can have four, two rows, two columns which will give you four different plots and so on as per your need. Sub plots are very, very useful. The idea is you want to show in one area two different behaviors rather than putting into the same plot. Now, let us get into the next topic in some sense, in the next sub topic that is plotting data. Till now, we have been plotting y equal to sin x, y equal to cos x and so on. Very, very rarely do we need to do that. More often than not, we will have a experimental data to be plotted. So, the data is what we normally plot. But remember, even though we said we have been plotting analytical functions, we have been plotting data only. It was generated by using an analytical function. Still, we were plotting using data. What do I mean by that? Let us see what you remember p is our lint space. Now, we plotted p and sin p. But when we type sin p, what actually is produced? As you can see, it is an array itself. So, the type of sin p is also the same as p. It is an array. So, in a sense, we have been plotting data even though we have looked like plotting analytical functions. But we are going to actually look at something compared to such generated data. We are going to plot more experimental data. There is a file called primes dot text, which is part of the content you should have. Yes, it is part of the test underscore files you should have downloaded. There is a file called primes dot txt. Let us look at what is there in it. It is there here. This is what you should see in the file 2, 3, 5, 7. All the primes below 100 is stored in that file. Now, this is typical for any data you want to work with. There are many ways of taking the data in. But the best and the fastest is using the function called load text. It is very powerful. So, this essentially loads the content of the file primes dot txt into that variable primes. So, let us print primes and see what it is. Let us first look at what is its type. It is an array. It is an array. As you can see, it is an array. It is an array. We saw the values are all floating point numbers, which is a characteristic of all computational things. Anything is deemed to be a floating point number only. We have another file pendulum dot txt that has two data, two column data, the length and the time period. When you are going to load such data, it should be ensured that both columns have the same number of items. So, let us load that pendulum file. Let us look at the file first. This is the data. As you can see, it has two columns of data and the time. Let us load this. So, you can see it is a array lists of lists. It has more complex structure than the first one. You can see the two brackets. So, it is a list of lists, each list containing the two data in the column in the each line of the file. Instead of storing in a list of lists, you can store it in two separate variables by using the unpack directive. Let us do that next. I can say, instead of getting a pinned array, I should get two variables l and t. I also have to say in addition unpack equal to true. That is when it will return two things rather than one list. This is a command I used. Now, you can look at l and t separately. We need to plot l and t square. There is a function called square. We can use that straight away. Since this is experimental data, does not make sense to plot a line. We will instead plot with dots. You should get something like this. Try it out. I will give you a couple of minutes. Try out and see that you get this sort of a chart. Please note, if you just plot it, you will get a slightly useless curve. So, a curve is not really useful for experimental data. Very rarely is a curve useful for experimental data. So, you are almost always better off starting with a, take a couple of minutes, produce this, convince yourself that the data from the file has now ended up and you have produced a plot. You should have got that. Another important plot is what is called a error bar plot. We want to, let us see what is this. Then we will get an idea. We want to plot instead of a point, the point plus some indication of the error in the experimental measurement we have. The two charts should explain to you the difference. The one we did has simply dots. The one on the screen shows that there is a certain error in each of the measurements. The estimated error should also be shown. So, how do you produce this sort of a chart? Like I said earlier, Python, the commands are simply English names. That is all. There is another file which contains. So, it contains the L values and the T values and the estimated errors in the L values and the T values. So, we are now loading this file which has four columns. Of course, unpack equal to true has to be given because we have four separate variables. Then the rest of the plotting proceeds as before. We generate TSQ instead of just plotting. Earlier, we said plot L versus TSQ. Instead, we say error bar and you get this. Try this out. Very useful because most often you need to produce charts with error bars rather than assuming the values are exact. So far, I have been plotting what we could call line plots. You know, either dots or a connected line, but there are other type of charts. You know that we will look at each of these. All right. One of the questions asked, I think it is from Rajalakshmi Chennai, is how to load string data? You are just now seeing the how to load data and specify a type in this scatter plot. So, you can make a guess from that. Make a guess. D type equal to type int is the one for converting it into an integer. But you should remember very rarely you need to load string data because you are not going to be doing any manipulation, any arithmetic or you cannot really plot string data. That said, if you are looking at using this as a mechanism to, for example, you may be doing some work on probably natural language processing or whatever, you want to upload into a variable all the string from a file. There are other fast, other mechanisms available, but normally you will have D type as the option. Another question asked is, how to use a saved figure? Saved figure is a file in the operating system. So, whatever your graphics image viewer in your operating system is, you use that to view it. That is all. And thanks for the questions. Let us get back to the way. Another question is, can we get data from an XLS file? Yes. There are two possibilities once again. You can export from XLS into a text file and use this or there are other libraries in Python available which will allow you to read and write XLS files. You cannot do a direct load text from a XLS file. If you want to use load text, it has to be a simple text file. So, the best option is go to Excel and export to, from Excel you can export to a fixed length format or a CSV format, whichever you like. A little bit of work is required in order to move from an Excel data to form acceptable for load text. But to answer a slightly different question, if you want to manipulate Excel sheets from Python, it is possible. There are two libraries called Excel RD and Excel WT which are available to open and edit and even create Excel worksheets. Now, let us go into the simplest of charts, a basic scatter plot. Now, if you look at here, you will see it is all integers. Earlier if you remember, even though there were primes was an integer data, the answer was shown after we did load text as floating point numbers. This is the way to get integers as integers scatter chart easiest. So, this is what is called a scatter chart. The very appearance tells you why it is called that, because data is scattered all over the place. When do you use it? You use it when you want to get a feel for how the data is, whether there is some large trend, you want to understand it before presenting or before making any conclusions and then deciding on a different way of looking at it. So, this is what we should have got. You can create different type of charts. A pie chart is very, pie chart is not the most appropriate for this, but we will just try to see how to generate one. Once again in Python, it is simplicity itself just type pi. But like I said, this is not the most appropriate use of a pie chart in this particular case. A bar chart is more, we have a time series, a bar chart is more appropriate. So, this you will get. Once again note the very simple direct bar for a bar chart, pi for a pie chart. Python has always aimed at being a very readable and easy to use language. As you can see the next exercise, is to plot a log log chart, y equal to phi x cubed. If you are connected online, you can look at matplotlib because matplotlib is the underlying engine which philab is using for generating all those graphs and charts. Matplotlib's power, we have not even scratched. It is a huge, hugely powerful package. So, this particular example, I am not going to type it in. Please go ahead, try it out for yourself. Remember that what is the most appropriate chart to use is in some sense your understanding of what the underlying data represents. Once again remember we are using analytical functions like y equal to phi x cubed here, but in real life you are going x and y will come from a data, measurement data and that would be most probably coming via a low text type of a command. Here we are trying to understand the log log that is also, we are using a simple analytical function, but it is very unlikely you will be doing log log of analytical functions. The whole idea of a log log is to produce. Log log tells you something has a certain behavior when looking at it. The most important data type for scientific computing in Python is arrays. Arrays look like lists, but you know lists are heterogeneous, arrays are restricted, arrays are homogeneous and arrays have a very high performance implementation. The same operations you can do using lists, it will be considerably slower. So, all computation when you use, you use arrays so that computation is very much faster. One of the simplest ways to create an array is from a list. So, essentially we use the command array which creates an array and uses the list as the provider of the basic data. It is important that we distinguish between an array and a list. For all practical purposes they look the same. Arrays are definitely significantly faster than any computation. Let us look at the similar to Linspace, we have an A range command. Let us look at the A range command. As you can see, the A range command gives you, does not include the last element as before. Now, the shape command, so instead of single set of 8 elements, we say it is a 2 by 4 array. You can see what happened before and after the shape command. Before the shape command, AR2 was a single array, one dimensional array. Now, AR2 dot shape, he said, no, we do not want to treat it as one list of 8 elements. You want it to be treated as two lists of 4 elements and we got this. Let us try another example. So, effectively you are folding a single long list into multiple lists, arrays. Once again, this is useful if you are going to be reading a multidimensional array from a file. A file is by definition unidimensional. You can read it in and then get it to the shape you want. Let us look at some special ways to get some special arrays, which produces an array which is essentially, you know what it is, I am sure. It is essentially the identity matrix. So, it will produce a 4 by 5 array, all filled with zeros. There is also a series of commands available, which end in like. Remember our old friend A3. Now, I can say zeros like A3. So, earlier, the earlier command he gave was zeros 4 comma 5. That produced a 4 by 5 array with the element 0. Now, this will produce a 3 by 3. That is what the idea of a zeros like is. You do not have to specify the dimensions. Instead, you give an actual array with the dimensions you want and zeros like will produce an array where all the elements of zero, its shape will be the same as the argument given. If you also notice the d type, the data type is also same as the original. It is not 0.0, it is 0 since array has integer elements. B has integer 0 rather than the floating point 0.0. Similarly, you have ones like these are useful in any context. We will see that how useful they are shortly. Let us look at the operations on the arrays. Please type out the commands on the screen and check and decide what actually is the meaning of say A1 plus 3. A1 is here. So, when you say A1 plus 3, what should happen? Test it out and see similarly A1 minus 7, A1 star 2, see what happens. Now, you would have seen that the plus operation is something that is applied to all the elements of the array. One of our students in earlier batch, get this nicely. In Python, you think wholesale, do not think retail. So, plus is every operation is applied to all of the elements. So, if I say A1 star 2, but note that A1 is unchanged. It returns a new array. A1 remains unchanged. So, you want to capture it. You have to store it in another, try out the operations also and see what happens. So, we have essentially updated the array in place. We can do that. You see a type called in 32. Now, it has become float 64. So far, we have been multiplying by LRs. We can of course, add arrays, multiply by arrays. But please note that this is element-wise multiplication. That is each element of A1 is multiplied by, so this is A1, this is A2. So, A1 star A2 is element-wise multiplication. Of course, you can store it in another variable and capture it and so on. So far, the only real difference you would have seen compared to a list is the operations and the fact that there is an underlying d type. Lists are heterogeneous, so there is no type of for a list. Arrays, there is an underlying type. Now, let us do something. How do we access elements? How do we change them? As usual, indexing starts from 0. How do you access an element? Like a list again, A of 2, same for two dimensional arrays. You can see in the last line of the slide that C, one element is being changed. Please type the A and C arrays, because we are going to have more examples using that. I am sure it will take a little bit of time to type the numbers. So, let us, you can access one of the biggest differences is that you can assign to a row. You can see on the screen, indexing is similar to a list, but there is a very interesting in the last two points. You can see that you can say C minus 1 equal to C minus 1 happens to be a row. C is a two dimensional array. So, C of 2 is essentially the third row. C of minus 1 is the last row. So, I can say C of minus 1 equal to 0 and make the last row 0 in one shot. So, accessing a row works very, very simply. Accessing a row is very simple. This gives me the first row and so on, but how do I access a column? In other words, how do I get 1, 4, 7? It is easy to get 1, 2, 3, 4, 5, 6, 7, 8, but how do I get 1, 4, 7? Now, the answer comes from the fact that you need two indices to access an individual element. The first is the row index and the second is the column index. Now, if I could say that the column index remains and the column index changes, I get a row, but for that I do not have to actually say the column index varies. I simply say A 3 of 0. For column, it is slightly different. If I say A 3, colon simply says all rows. I am using the A 3 because I do not want to type that C once again. Please, you try out with C. This, as you can see, is let us have A 3 inside, so that the last column is accessible this way. It is not very difficult. Simply remember, A comma B is the way to access a row and a column. Colon fills you all the values, that is all. So, for that matter, we will give you 4, 5, 6. So, there is nothing special. We simply, this also will give you 4, 5, 6. Some people even would suggest in order to be consistent, you should always use 1 comma colon even for rows, but that is very, that is a little unusual, but that brings it to the fact that there is nothing peculiar about accessing columns this way. Rows and columns are both accessed by saying give me a column number, allow the row number to vary completely. That gives me a column. Give me a row number and let give me all the columns in that row, gives me a row. So, both of which are on the screen, you can see that. Once again, you can assign all the elements in a column to a single value by using this notation. The next part in arrays is something to do with slicing and how to use arrays for very simple image processing. I do not want to start the topic now. You may not be able to complete it before lunch. Plus, I want sometimes spent on any questions from your end. I understood that the programming test performance was rather poor. We are a little disappointed. So, the plan of action is to today's afternoon session after lunch. We will do a review of programming for some time and continue with the advanced python tomorrow. We are fairly ahead of the schedule in these sessions. As you can see, we have done about 50 percent of the slides already. So, we will take advantage of that and use the rest of the afternoon for reviewing programming related topics. So, people should come prepared with some questions, anything you want to, when you do not understand to ask. Let me repeat. We will review programming in the afternoon, but please come prepared with questions, difficulties you have. We will break now for lunch. See you back at 2.