Watch this if you want to LEARN MATPLOTLIB for PYTHON!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so this is a video showing you how to do data visualization in matplotlib now matplotlib is one of my favorite Python libraries you can do things like this in MATLAB and this and even this yes you can look at images and manipulate images too with matplotlib it's a great Python library actually it's one of my favorites I really like the control it gives you over visualizing your data now it's not the only python library that does that they're Seabourn and Bach a as well and you will come across those in the future if you continue with your Python data visualization but matplotlib really is the go-to one and it is fantastic it's also the built-in one for pandas as well so it's really good for you to get to know it quite well now I've recorded a tutorial about it it's coming up next it's a bit longer than I hoped it's about 13 minutes I tried to keep it lower than that but I just couldn't give everything I wanted to in less time than that and cover all the things I wanted to cover it teachers matplotlib in the way that I wish I'd learnt it it talks about why we do data visualization in the first place and then it goes on to show you where you can learn more about it if these videos are helpful then please do subscribe to the channel and like this video that really does help with visibility it helps people find them which makes the videos more popular if you have any questions or comments add them in the comment section and I will see you after this tutorial section so I'm going to show you the basics of matplotlib and some of the things it's capable of but I also want to show you why data visualization is such a useful tool so I'm gonna start with that and then I'm gonna tell you about the well I think is the most important thing for a newcomer to learn about matplotlib and it's not something I learned until after I've been using it for a while and it was something that always confused me so we'll cover that then we'll look at how and where to learn matplotlib okay so it's start when you use matplotlib in jupiter notebooks if you want your graph to show up inside the notebook you have to put this line here matplotlib in line preceded by a percentage sign and then you import matplotlib pipe lot as PLT and then you import numpy as MP and this is just a style instruction here you don't really need to worry about that so let's run that cell and what I'm going to do is I'm going to show you some visualizations on some actual data now there's this really famous data set called the iris data set and I have a link to that here so the IRS flower data set or Fisher's iris data set is a multivariate data set introduced by the British statistician and biologist Ronald Fisher in 1936 in his paper and it's been used a lot and it's a good classification problem data set and it's a good data set to learn on so that's what we're going to use so let's go back to this notebook now and let's run this code and basically what this code is doing it's importing the data set from SK learn which is scikit-learn and it's also importing pandas so that we can have a look at the raw data itself so let's do that and you can see here now we've got a data frame with the sepal length sepal width petal length and petal width of different species of iris and the three species have different names and we'll come to those later but for now in this data they are labeled 0 1 & 2 so info tells us that there are 150 entries that they are mainly floats but the species is an integer and that's it the described method tells us a little bit more it tells us that the sepal length has a mean length of five point eight centimeters and various other useful numbers as well the petal length has the largest standard deviation of one point seven six but still it doesn't really give us that much insight into the numbers we've got some but we don't have that much so let's now plot this data using matplotlib and i've written some code here that will do that and now all of a sudden this gives us far more insight into what's going on so we know now here we can see that there are three different types of iris there Soto's diversity color and virginica and we can see that sir tozer is quite should be quite an easy classification problem because it's quite separate from the others but this cluster here of versicolor and virginica would be a little bit more difficult at first sight to classify a visualization is a very good way of communicating your data but it's so much more than that as well it gives you insight into that data which may help you decide how you might want to work on that data and if you were doing a classification problem for example it might help you decide how you'll go about doing that so let's now move down to the next plot that I've done and all I've done here it's a similar plot but I've made this now a 3d plot so it also includes now the petal length which it didn't before it had the sepal length the sepal width and it also had the petal width which was related to the size of the circle but this one now has the extra petal length and you can see now that this 3d plot just gives us a little bit more insight into the data so that's really the whys of data visualization that's why you should do it it's not just to communicate your data to other people although it's very useful for that it's to give you insight into your own data now what I wish I had known about matplotlib right from the start was that there are two ways of doing plots there's this way here using classes and then there's this way here using PLT the class method gives you far more control over what you want to do but the PLT method the pie plot method is is a sort of quick and dirty way of doing it and if you're starting out with matplotlib I would recommend you use this method here rather than this one once you've got to grips with the PLT method the pie plot method then you know move on to using the class method but they both essentially do the same thing the PLT method i think is supposed to be a bit more like MATLAB for those people that have come from MATLAB there's also another way of using nap plot lib and that's using PI lab but that is not recommended anymore so I'm not going to cover that here so if I run these - sells now you will see that essentially they do the same thing they I've added a little bit more to this one which I haven't added to the top one but they essentially give you two plots one of square numbers and one of cube numbers and let's look at what's going on here so in the sort of easy way of doing it you tell matplotlib that you want to plot something and you want a subplot and what this means is that you want two rows one column and that the one you're working on now is the first one so it's rows columns and then the actual plot you're working on and then to plot you just use PLT dot plot and then here you put the X and the y values now I've put list comprehension for the X values and a dist comprehension for the y values just to give us the square numbers but you could just put x and y here or you could just put actual numbers you could put a list of numbers and that returns this plot here and it has a title and that square numbers and i've also added on to this plot just to show you that you can plot more than one thing on a subplot i've added in a line here which is the line at y equals x and now i've put in PLT subplot and then again it's two rows one column and this time we're working on the second plot so everything that follows this now is related to this second plot and here we're just doing cubed numbers and then we do PLT and there it shows so you know if you want to write something very quickly I could write something else now here I could just do PLT dot plot and then your X numbers here one two three four and then your Y values here would be one two three four and then if we go down PLT dot show we get another plot there and so you can see that is a very easy way of plotting let me just explain how this works so you have the figure that you're creating this plot here and then everything that comes underneath it relates to this subplot until you write subplot again and then that relates to the next one and then when you write show that displays what you want to display so if you want to add anything to this first subplot you have to make sure you put it before the second subplot and then so on and so forth if you had more plots if you wanted to use this class method then the way to do that is you create a figure and you do that by using this style here so you put fig comment ax and then PLT subplots and then in here we've got two subplots but what I want to show you is what's going on in the background so if we now print the type for fig here you can see that this returns a class and this is a map plot lib figure now the figure is the whole space it's this whole diagram here but it's not the bit that you draw on or write on for that we need to go a little bit deeper into this and have a look if we return now what the type of AX is which is this variable here we can see that it's a class and it's a numpy array so we need to just go a little bit further down to find out what we're looking for it's actually matplotlib axes and it's the axes that you write on so when we want to do this plot here now we take the first axes which is the zero one and we can then do the plot in the same way that we plotted here and then when we want to plot on the second one we take the second one in this array here and we can plot just like we did so those are the two separate ways of doing things they give you the same results but that confused me for quite a while because sometimes especially if I was you know looking at other people's code or I was modifying code sometimes it would have been done like this and sometimes it was done like this and I didn't really understand what the difference was and and that's the difference so that's what I think you should know because then when you see other people's code and when you do your own searches online to find out more you will see these two styles and that might confuse you for a little while but hopefully it won't now so now I just want to show you a figure from the matplotlib website and it's really useful and it shows you the elements composing a figure so if we just run that so this is what a figure is in matplotlib and the figure is this whole space here but the axes with an e is where you display your graph or your data your information your plot so that is the plot goes on the axes you know not to be confused with the axis which is where you put the labels so this is quite a useful figure and diagram if should you ever get confused about what's going on when you're creating a figure in matplotlib this is very very helpful now as far as learning map plot lip is concerned if you go to the matplotlib website and click on the tutorials i've put a link below in the description you will see a whole range of tutorials on here and really trust me you don't need to spend money on books or courses in order to learn matplotlib you can do it all from their website and you can go here to the pie plot tutorial I would start there I mean it's going to depend on what you want to use matplotlib for but this takes you through the basics and right at the top here it explains the pie plot appiy is generally less flexible than the object-oriented Appy most of the function calls you see here can also be called as methods from an axis object we recommend browsing the tutorials and examples to see how this works so here they're using PI plot which is this simpler notation it has access to the same features but it's not as customizable and but it's a good place to start so here just to do a straight line they have done PLT dot plot and then the numbers 1 to 4 there's a wine label and then they show the results and you can see those there is they're in this next section they've used x and y data and then they plot a slightly different line and here they have changed the style of the plot by changing the labels on the x and y axes and then as we go down it shows you how you can start to customize things and they did some customization here this is telling the plot that you want the points to be red and to be shown as circles and then if we go down to the next one here we have an umpire array and on this section here they've plotted the numpy array in various different ways so in the first bit they've just plotted T against T and they want that to be a red - which is this line here and then they've plotted T against T squared which they want to be blue squares and you can see those here and then T against T cube which they want to be green triangles so you can see already with just a few lines and some variables you are starting to be able to control how your plot looks so that's where I would go next go to matplotlib and the tutorial section if you just want another source of information and help with matplotlib then i would highly recommend the python data science handbook you can buy it on Amazon but you don't have to do that it's free on github if you go to this link here and then you go down to chapter 4 there's a whole section here on matplotlib and it is superb now just going back to the iris data set using pandas which relies on matplotlib for its plotting functions I just wanted to show you what you can do very quickly in a few lines of code so let's just run this and you can see now that we've got a scatter matrix where we have scatter plots of different pairs of variables and a histogram shown in a matrix which is another very easy way of getting you know insight into your data and I found this on Stack Overflow so I hope that was useful as I said if you have any questions then do please let me know also if you have any suggestions for videos if there's anything particularly that you want to know about to do with Python then do ask and I'll make videos about that too that's all for this week I'll see you in the next one bye bye
Info
Channel: Python Programmer
Views: 53,271
Rating: undefined out of 5
Keywords: matplotlib, pyplot, data visualisation, python for beginners, python, learn data visualsation, learn matplotlib, iris flower data set, 2d plot, 3d plot, matplotlib tutorial
Id: 6rKe2IEIu8c
Channel Id: undefined
Length: 15min 34sec (934 seconds)
Published: Thu Jun 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.