 What I am going to do now is just zip through matplotlib, really literally going to zip through it in part because much of this matplotlib is really very easy to use. So we have done basic Python so far, now you can do arrays and array manipulations of various kinds. So the thing that remains is how do you actually plot these things. Matplotlib is basically a very easy to use scriptable matlab-like 2D plotting toolkit. Primarily written by a person called John Hunter and he was the first to build it and then they have a pretty large developer community right now. Primarily because it has been so successful in capturing the plotting market in Python. If you look at Python plotting, I think every self-respecting Python developer has his own plotting routines. I have just seen so many plotting routines, I have my own plotting routines. There is Grace plot, there is GNU plot and there is PL plot, there are whole bunch of plotting routines. And basically what John did was he gave it a matlab-like interface which really made it extremely easy to use and he supported NumPy arrays, numeric arrays from the get go. And it produced publication quality pictures and it is so easy to use. Basically if you want to plot something it is just a line, it is just a line of code pretty much, most often. Therefore it just made it so popular and he supported whole bunch of different back end. So it runs on its cross-platform, so it runs on Windows, Linux, Mac, you name it. It supports various GUI toolkit back ends. As you may know there are various GUIs, you can use GTK, you could use QT, you could use WX Python, you could use Windows native things. You may not want a UI at all, you may just want to generate PDFs or PNGs and he supported all of that. So basically it produces publication quality pictures, figures and has interactive capabilities. It makes plots, histograms, power spectra, bar charts, error charts, scatter plots and I will show you examples of these. It also does polar plots, contours, vector plots. So basically the entire 2D plotting market he has taken care of and that really makes it very popular. That is today in my opinion it is the most popular Python based plotting toolkit. It also does something very neat which is supporting tech markup. So often mathematical figures you want to show an integral or you want to show some mathematical symbol and latex happens to be one of the most popular ways of type setting mathematics and lots of people are aware of how to use it. So you can actually embed a latex string and it will actually render it and show you a nice mathematical formula on your figure. It supports a whole bunch of different image types, cross platform as I said and the other thing that has been done is it is integrated very well with IPython. So Fernando Perez was the creator of IPython and John sat together and made sure that it really works well. There is a plenty amount of information on matplotlib. These pages are particularly useful in fact the screenshots page is what I am going to use to show you various examples. Basically they have examples, they generate some data and then they have plots and on the web page you can actually see the pictures. So basic plotting with matplotlib, the way you start is you say I am doing IP demo because just a script so I get the colors right, you say IPython minus pi lab. So substitute IPython with IP, it might be demo with IPython. So it takes a little bit of time to start up but once you start up you have a bunch of symbols that are automatically imported. As I said before IPython supports profiles, pi lab is a special profile which does two things. It sets up a separate thread of execution for the UI interaction and it gives you a live interpreter on which you can work. And on that interpreter various names from pi lab and I think noom pi get imported. Actually not a noom pi. Various names from pi lab get imported. So you do not have to import things off the back. So for example I can say x is length space 2 star pi, let us say 1000 points and I want to plot this. I am going to change that to minus 2 pi. Now I want to plot this and let us say I want to do sine of x. So sine is also built in the sense that when you start up in pi lab mode you get these symbols for free. But when you are writing a script, a proper script you will not have these. So a good idea there is to say import pi lab and then prepend everything with pi lab dot. But if I do this, I get a window over here which has sine of x is plotted and I can if I press G on this, I get a grid, I can shut it off. I click on this, I can actually zoom with my mouse, zoom into any point. As I hover my mouse it shows me over here where my position is which is extremely handy. You can overlay different pictures on this and that is your sync function. So it is as simple as that, create an array, yes, you ssa, okay so the question is how does it run if you use a, if you run it from a remote terminal, okay that is a little complicated because it depends on how you connect to the other server. If you are running on an x server, if you are running on a Windows box, God help you. If you are running on a Unix machine with an x server running here, your ssh minus x and your ssh is set up okay such that you are able to listen for, you do x11 forwarding then it would work, it should work in theory. But you need to start your Python session with a threaded mode, that is pretty much it. If you want to just generate remote images without starting up a UI, that is not a problem at all, that should just work. Question was, is GNU plot using matplotlib, no, GNU plot is the, it has been around for way before, there is, as I said there are millions of plotting libraries, GNU plot is supported, there is also GNU plot, in fact earlier before matplotlib came on the scene, I used to use something called grace, sgr and there is a Python interface for that. That is very nice, it is not a bad toolkit at all. There is GNU plot, I have never used GNU plot from Python, but I know people, Fernando Perez has a special mode with IPython, something called doop.py, where you can use GNU plot from Python with a more Python interface. So there is a lot of plotting toolkits. But today matplotlib is the most popular, easiest to install, it is available and it has a huge feature set. Matplotlib is a very Python centric library, you cannot use it from say C or something like that. No, matplotlib is basically built to work with Python. All matplotlib plots are basically Python files. So basically from Python, if you want to plot, I would recommend that you use matplotlib. You could use anything else, no problems, it is the most, it is the most feature packed, the most active, the largest developer community. You want to go with tools that have that. But yes, there is GNU plot, there is xmgrace, there are whole bunch of different things you can use. Oh okay, matplotlib itself uses, again it depends on the back end. So the actual 2D drawing, okay. See the thing is when you are plotting, ultimately what are you doing? You are just drawing dots or you are using, if you are rendering into post script, you are writing in post script. So you have an API, like PyLab, when you say plot, plot has, is going to do something depending on the state of the matplotlib library. So if you say I want to plot only post script, I am not interested in a window. It is going to, whenever you say plot is going to generate lines or whatever it does, those will be translated to primitives in that particular language. So if it is a screen rendering, it is going to generate lines with whatever color, pen color, all of that. Or if it is a PDF, it is going to just write PDF code and dump that into a file or a file like object or whatever it does. So that is the internals. So ultimately matplotlib is a library that is being used from Python. Is that clear? It is a module, matplotlib is a module. So the point is you can plot simple things and complex things. So the first thing which you just looked at here is plot x sin of x and that produces a plot. Now it by default, oops, it produced this. So one function that you can remember is CLF which is clear figure, this is a figure and it clears the figure. Now if I wanted to plot sin of x with symbols, I wanted a red line with circular symbols and a line, I just type it this way and I get that. So the idea is you can actually create complicated looking plots by composing several of these small things and specify your x value, y value and a specification for what you want to plot and how you want to plot it. So there is a whole bunch of colors by default, a whole bunch of symbols, maybe 12, 13 different symbols and line specifications. So if you just do plot question mark on an IPython session, recommend that, it is huge, I am not going to sit and talk about those, you will find that it is straight forward and it is sufficiently interactive that you just figure it out, it is not a problem. You can set the axis, x axis, y axis dimensions using the axis command. You can set labels which I will do right now. So x label, say x, you have x there. The nice thing is if I want to say y and I want to put latex, I can say dollar, dollar is the symbol for mathematics in latex and you make it a raw string because you are going to put slash things. So I can say slash alpha, I am going to be, this is not right but I am going to just say it is sine of alpha, oh I said x label and I did not close the bracket. You notice here, I do not know if you can see it, it is actually plotted an alpha symbol. So that is one of the nice things about this. There are a bunch of other examples here that shows similar thing here. You can set a title and you can specify a font size if you want. You can change the color of labels and things like that. Again I would recommend that you look at x label and explore x label a little bit. Finally you can save a figure. So you can either do this, oh boy, there is a small save icon here, you click on that and then you can type out your file name or you can simply say save fig and say test.eng if you want, pdf if you want, it will figure out what you want and use the appropriate back end and I should have test.png there, so if I say open test.png, yeah, that is the png that it generated. So it is as simple as that. So you can make multiple plots in a figure as I showed, you can put sign and cause and something else, you can clear out a figure and then redraw it. There is a setting called hold which is not here which lets you turn on whether it is going to overlay plots or going to refresh the plot entirely. It is very similar to MATLAB, you can plot multiple plots in one single line like so in the last line you see here, plotting straight line, the x squared and x cubed with different symbols and then you can set the property of objects, you can say the line width. Every time you plot, if you notice it actually returned something, so when I plotted a line it said it returned a MATLAB clip lines.line2dr instance. So if I captured the return value that actually gives me the line instance and I can actually set properties of that line and the way to do that is to say set p, set p is another pi lab function which is in ipython, in ipython minus pi lab already exposed, if not you would have to say pi lab dot set p and you can set the properties, the same properties I set here I could set here and if you just say set p l it will list out all the properties. So again they have taken a lot of effort to make things as easy as you can possibly make it and as interactive and helpful as you can. You could also set the parameters directly by saying set underscore property, close will close the figure, you can open multiple figures it is not like you are stuck with one figure, every time you say figure it is going to open a new figure and you want to go back to one of the earlier figures, you say figure, so here I have created figure 1 plot sign x, figure 2 plot tan hyperbolic then I want to go back to figure 1 and set the title. So you just say figure 1 it will not create a new figure it will go to the existing figure number 1. You can also do things like subplots which means you can put multiple plots on the same particular plot. The way to do that is use the subplot, subplot is basically number of rows, number of columns followed by the current figure number that is what I suspect and you can always simplify it by saying 2 1 1 and it will interpret that as 2, 1, 1, they are just handy conveniences. You can also stick in, so let us say I am going to do this plot, close plot x sign of x and I want to label it saying sign and then I want to do cos you have these 2 and I want to show the legend it actually gives me, it takes the label that you provide with the plot but if you did not do labeling you could always say legend say, it is the same and it change that. So, you can change the legend set the legend you can also put text say say 50, 50 I do not know if that is going to work, okay, mistake, so I will make it 1, 1 at that particular location on the plot I want to say hello, oh boy it puts it right there because that is where 1, 1 is. So you can annotate your figures and remember you can still put those latex things in there, so you want to put an integral symbol saying something that you can do that. So here are a bunch of simple examples that show you the features you can do. This one basically it shows you what subplot does, in subplot you have 2 plots one on top one on the bottom and here all it is doing it is plotting an exponential times cosine and the cosine itself using different symbols it is turned on the grid saying grid true you could press G but typically you want to write a script that does everything for you in one shot. So this script is completely self contained it says the title it sets the y label the x plot and plots whatever is needed okay and subplot make sure that you have one plot on top of the other. You can also throw in error bars so if you have errors on the x and the y you can do both and do that and the way to do that is you say here is an error bar given the values on x, y and the specific errors on the x and the y you can do semi log and log log plots as well. Many of these examples are actually slightly modified from the matplotlib demos it is a zip file that is available on the matplotlib web page. So again here we have 3 plots so he says subplot 3 1 1 and then creates a semi log y a semi log x which means one of the axis is log the other is not semi log x or semi log y and a log log plot and he shows all of them on the same plot. Now you can also do histograms as I said and the way you do it again is so here all he does is he sets up the data the first line is the mean and the standard deviation and they have a function called rand n it is part of numpy I do not know if they have reimplemented or they just implode the numpy one which generates a set of random numbers which are distributed as per Gaussian and this one line basically shows them a no this actually generates a histogram given the scattered data and this one line just does the plotting of the histogram no no no I am sorry no his actually does the plotting right his does the plotting and returns 3 things it returns the number of number of bins bins and I do not know what patches it right now so you can just look at the docs of his I am sorry I have not used his stuff so his will tell you what the documentation of his is so I suggest you just try out these examples and again over here you notice there is again math symbols here which can be injected through a title note that when you are using latex you always need to ensure that it is a complete latex string you cannot put dollar in between you must say r quote dollar and if you want it in normal Roman font you have to use slash RM okay so if you do not know latex you may have to read up a little bit on latex elementary latex syntax you can do bar charts and here is an example for that running on short of time so I am going to run through these and again if you notice lots of the code here is just generating the data this is a complete example and the plotting itself is like one or two lines and setting attributes is another couple of lines this is a pie chart you can see and again it is a single line you just say pie the fractions the explode parameter here basically says how you are going to shift it here it is only shifted on the x axis sorry on the y axis if you notice the pie it is shifted up from here and that is the explode here then you can do scatter plots again it is a single line you say scatter x y so you have a bunch of points x values and the y values and the area of that of the symbol is given by an array which is here some particular thing which is like and you can also color it based on the volume so c equals volume in this case I have calculated some volume and given it a argument so if you gave it a specific color if you say c equals r it will all be red but if you give it an array it will look it up through a color map lookup table and if you say color bar at the end here it will actually show you the colors and which color is what value you can also do alpha blending which means you can give it an opacity so if you look here I can this red blob underneath that I can see the grid so it is not fully opaque so the opacity is a value between 0 and 1 and it is 0.75 here it does polar plots as well and again it is just polar theta r specify a color and a line width there are other parameters of course if you look at polar it will have a whole bunch of things you can specify I have just chosen these you can also do pretty complicated contouring with labeling and use different types of color maps to indicate your colors and this is slightly complicated example the I am show basically given an array will show you the array as a color contour not a contour color map image depending on what color map you use it will choose an appropriate lookup table. So let us say I have a scalar between 0 and 1 I want to know somehow denote each value inside some color so what you have is you have an array that has a bunch of colors and at every what do you call it every range of values say let us say if you want to show it with two colors or four colors as values from 0 to 1 I take anything between 0 and 0.25 and map it to red 0.25 to 0.5 I map it to blue so on and so forth so you define a lookup table which has which is a bunch of colors and you map whatever values you have through to the colors so given an array you can always visualize it in terms of colors so you can choose a whole bunch of different color maps so for example here I do not know there is a thing here where I say in the end bottom here it says hot. Hot is a particular color map if you do hot question mark I am sure there will be lots of information on the various color maps that matplotlib supports there are a whole bunch you can also do velocity vectors so you have a bunch of vectors and you want to show them in 2D yeah okay so mesh grid will basically generate a mesh of X values and Y values as a numeric array and now you can say cosine of X sine of and sine of Y to get the X and the Y the idea is generated arrays and you can actually generate in one stroke an entire block of area a field you can enter you can generate a complete vector field at each point you have a vector which is varying sinusoidally or cosine cosine function and it is just this these many lines and you can say quiver this function here basically generates arrows at each of those points and you can set properties of the arrows and so forth quiver key will actually say if you notice here you see a 1 meter per second and an arrow there so that basically gives you something like a key which says this arrow width indicates this kind of a length scale okay so the last thing is a map and I have never used this so this is just a picture from their screenshots and the example is pretty long so I have not shown you the code on the right side but the example can be found here but you can see that you can actually generate a map you need all the right data in order to show the boundaries to show the values and things like that and they have an example that shows you how to do that but what I am trying to indicate is matplotlib actually supports things as complicated as this so basically the whole range of 2D plotting with NumPy arrays is very easy to do and it is very efficient in the sense that once you know the basic syntax and you are comfortable with ipython and you set up ipython-pylab it is like just one line two lines and you can start experimenting and get a better feel. Now matplotlib is not just limited to what I showed you here it has an underneath the interface that I talked about today was Pylab. Pylab is the simple interface so that with one line two lines you can actually produce really good plots underneath it uses a full-fledged object oriented hierarchy and you can actually do more sophisticated plotting so in one situation I had to plot some circle with an ellipse sitting here and things like that these things can also be done but typically they are not used all the time so if you are really interested you can go in and look at the documentation they have a good mailing list as well so you can ask people there. They have a huge number of demos so it is a good idea to take a look at those and keep that handy whenever you are trying to plot something so that is it.