Intro to Data Visualization in Python with Matplotlib! (line graph, bar chart, title, labels, size)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys and welcome back to another video sorry that I have been gone for a while now I was really busy finishing up my master's degree but that is all done with now so I'm happy to be back and happy to be able to me making some more videos so in today's video we will go through the bat plot live library of Python and this is a super useful library for all things data visualization in Python so the video will start off with going through the matplotlib basics so as you see on the screen will be creating like lying graphs such as the one you see here you'll learn how to format these lines in different ways you'll be able to add a title you'll be able to add labels to your x and y-axes format your tick marks properly add a legend etc so all the kind of basic matplotlib things you want to know then we'll move into other types of graphs such as bar charts and we'll finish this video off with going through all sorts of like real world examples where you can kind of combine your data science skills that you might have using like pandas library and if you haven't learned anything about the pandas library I recommend you watch my video on that so with that the kind of real world examples will do stuff such as like plot the USA versus Canada gas prices off of data that we have little link to in a CSV we'll do some histogram stuff we'll do some pie chart stuff and we will do some box and whiskers chart stuff I can't get through every type of graph in this one video but if there's certain things that I didn't cover and you really want me to cover let me know in the comments that would be super duper yeah that's all I got to start this video let's just jump into it all right let's start out by loading in the libraries that we need for this tutorial so we're an import map plot live as our dot pie plot as PLT we're going to import numpy as NP and we're going to for the later examples import pandas as PD and run that so if you have all these us like already installed he won't get any higher so if you do get these get an error when you're to run among these you find need to install it so you can either do a pip install a map plot lib pip install numpy pip install pandas or you could install a Python distribution that already has these packages such as anaconda I'll link to how to do this in the description okay so now we have our libraries loaded let's now build our basic graph so I always think it's kind of the hardest part about anything with Python in general is starting with that blank blank space so what I recommend for matplotlib is always start at the documentation so I have a link for this in the description but here is the home page of the matplotlib PI plot documentation and what I recommend is like whenever we're trying to do something new just do a ctrl F and like look for it so if we look for plot here that's like we're just trying to get a line graph on the on the screen box plots not when we don't want it then plot ok this looks good plot y versus X as lines or markers so this is what we'll start off by doing as you can see right here and the examples it gives gives plot X comma Y so that's the command we'll have to run to make this work and just to know what x and y are if we go down to parameters they tell us that commonly these parameters are 1d arrays so we know we need to pass in 1d array to the plot function so if we do plot 1 2 3 that's going to be our x + 2 4 6 will be our y I can't type and run that plot is not defined so we imported pipe lot as plot so when we run this function we actually have to do P love T dot plot and then this is our X and the second array is I so let's see what happens there yay we get a graph and to clean this up a little bit I recommend moving our variables into removing our arrays into variables so X is 1 2 3 and Y is 2 4 6 and we can change now this to X Y and one thing that's annoying with the matplotlib if you're doing this in Jupiter notebook by default is you have this annoying little line here so you want to get rid of that you can just actually call plot dot show and that's the correct way to actually show this graph ok now that we have just a line on the screen let's start adding a little bit more to our graph so to do this let's go back to our documentation and go back to just the pie plot so I think this is a good spot to start because you always can kind of just control f and find something you're looking for so let's say we wanted to add a title to our graph if I type in title it points me right to a couple different places that I probably want to look at and right here is what we're looking for so set a title for the axes click that and this tells me all sorts of stuff about how I call this so it looks like I used to pie plot title so I can do plot title and let's just do our first graph and run that as you can see our title is now there and same thing for a lot of the different parameters so going back to the documentation if I want an X label on a white label I just type in right I guess I knew that but let's just say why I label nothing comes up Y or just even label C label label a contour plot that's not us the fig labels no I know title no X label Y label let's how we get there so you can keep doing this and really easily find what you're looking for I think it's a good place to go if you kind of just if you forget things because I always forget things in that plot lid okay so adding a label to x and y plus X label x axis and I guess I'm getting really enthusiastic because I keep adding exclamation points to all my stuff y axes yay see what happens yeah and I want to just get rid of these recommendations I don't need them okay so now we have a graph and we have a title and we have a y axis x axis in a real graph we would label these properly but for the sake of this example we're just kind of getting a feel for how we add these alright so now that we have our title and labels one thing we might want to do to them is resize them so if we look at the the documentation for title one thing you'll notice is that you can use you can pass in this font dictionary and it gives you a little bit of details about the Fondation area here but basically there's all sorts of options that we can do and like play around with our fonts so let's say I wanted to change the font of our title like a new font dict equals and I can pass in a font name and let's say I my good old trusty reliable comic sans ms will be our new font and if you look at that we got a nice new font on a graph and if you can't see it well then in that case I'll change the font size to be something like 20 now you should definitely be able to tell that I've changed the font type and in the description I listed I put a link to all the matplotlib fonts that you can use and if you look through the documentation you kind of find some different options you can do with this and you can also do the same thing with the labels I could do like font dict equals let's say I wanted to make this font arielle and that obviously might be the default but if I did it like Comic Sans as well you can tell that the x-axis now has changed but I want to just leave that this be for now okay so we did labels right so that one thing that's bothering me is the tick marks I want to let's say I just want to do integer numbers and I'm gonna add a couple more values to our graph so I'm gonna go to five I'm gonna start at zero and why would go from Y is always gonna be two times X so it's gonna look like that oh I would magically did it for us at that case I don't want it to magically do it for us okay let's say we went to four and we wanted just the integer values we didn't want any decimal value so how do we change our ticks so we can do that by doing plot X ticks and our X ticks will do 0 0 1 2 3 and I also just found remember this command by using the documentation and our white ticks will be have a 0 to 10 every two so it's 0 2 4 6 oh my gosh 10 as you can see that now we have the tick marks working properly and maybe I wanted to add 4 as well so now we labeled our ticks differently and one thing that's cool to know is that the graph will automatically resize based on how we set this so if I set one tick to a thousand well now our graph kind of like not too good but it's nice that you can easily do that for is better but and you can do the same thing with why I'd say when do they make the last one 100 as you can see it changes up the sizing of the graph one thing that's also cool though is find it even if I didn't even lis space these out if I did like seven here and then 7.5 as you can see it properly puts those at the right spot in the graph I'll leave it like this I'll leave it a little bit weird but yeah that looks good to me okay so we have our first graph let's uh add a legend and make this line a little bit more exciting color wise so if we wanted to add a legend we can do and I would actually kind of like showing how the documentation is helpful if I looked up legend Figg legend that might be work just this legend place a legend on the axes like by looking up legend I have figured out that the call is just legend and it gives me some examples so I always like to refer to this documentation okay so PLT dot legend and what happens when I do that no handles with labels fanned put in legends so it's complaining because we don't have anything to label what it's saying is how do we label this line and the way we do that is by passing in a label to our plot function so in this case our x and y we plotted X to be or Y to be 2 times X so I'm going to call the label here 2x and as you can see we get 2x right there okay let's say now that we wanted to change up the color of this line maybe make it thicker do some other things to it if we go to plot whereas plot will see all sorts of properties that we can do and plot so what I'm seeing right here is you can pass in all of these different parameters to our plot and it probably lists off what we can put maybe not it's not super specific but if you look hard enough you can see all these properties that you can pass into the plot function so one thing we could plot pass in is color so if I passed in color equals red will work and it will note I think the common color so if I did yellow if I did like yellow which is not a color it's gonna yell at me might even work with y yeah cool and we also can pass in hexadecimal colors so I could do like a b a b a b and we got like a grayish color you get this is helpful because you can pick use like a color picker tool and pick pretty much any color you want here but we'll stay with red for now the other parameters I could pass into that let's see what else we had I could do I think the line size equals let's say 12 oh that is not a prat parameter not remembered either go back to my documentation size marker size maybe that's it more precise oh I also see you lying with here so line width equals 2 ohmygosh line with equals 12 makes this big line maybe two it's a bit bigger than I think what we had we can also do we can label a marker so let's say I wanted to add dots into this as you can see you can't really see it too too well for me if I made the marker size a little bit bigger and I'm just kind of passing this in so you could kind of see what you can do oh that didn't work let's say 10 yeah as you can see nicely big dots on my graph I can also do marker edge color there's all sorts of properties I'm just like continually passing these in but now you see a different type of dot and whatnot so this is pretty cool that we can have all this customization sometimes takes a little while to get what we want but it's usually it just is nice to have this power and one final thing I'll pass in is line style so there's all sorts of line styles we can pass it and you can read all into the different ways you can do this in the documentation one thing that's pretty cool is that instead of listing all of these keyword arguments sometimes we just want to make a different type of line quickly so there's actually a shorthand that you can use to kind of do pretty much the same thing so I comment this out real quick we have no graph what you can do is there's a shorthand notation that allows you to kind of have a pretty good amount of customization quickly so the shorthand notation is color marker line style I guess so if we go and copy this line from above let's say we just passed in this now what we can do is before we add our label we can pass in the shorthand notation so I can do color will say read it I think just accepts letters you can look into the documentation see exactly what it does marker will do a dot and then line will do a dash so watch what happens when I do this gives us nice line with dots in it if I do two dashes gives us the dotted line if I do like a different type of marker you can pass in like a caret like that gives us a line with carrots in it it's kind of cool that you can do this shorthand notation and get different types of lines very easily just to follow up on the whole shorthand notation pulling in the docs for the plot function it shows all the different markers you can use and the different line Styles supported and the different colors supported so check out the plot documentation to see all that all right let's move on to our next thing and that will be I guess let's just out another line real quick to our graph so we have our first line let's add line number two and this one will make a little bit more interesting and the thing is like here we had a very straightforward like you have X any other Y but this is not always easy to do especially if you have like a function like sign where you want to plot it at like a bunch of values but those values are all like you know crazy decimals so I'm gonna show you how you can handle something like that so this is where we use numpy so nobody call this x2 I'm gonna say that x2 equals NP a range 0 to 4 and we'll go by 0.5 as our tick so if I print out x2 real quick you see that this gives us a numpy array with zero 0.5 1 1.5 to 2.5 3 to 3.5 I'll actually make it good a 4.5 so it goes at the same spot as our other graph because this is a exclusive here so we have that so that's gonna be our exes and so what I can do here is I can go ahead and plot that and I'm going to plot the square function for it so I'm gonna pass in x2 and then instead of passing in a why I'm just gonna do x2 and I'm gonna do squared what happens here so now you see we get x2 squared it goes a little bit higher than our original axis was set so I'm gonna change up the ticks or maybe I'll just kind of comment this out and see what happens for a tick marks yeah I'm fine with just leaving the default that it gave us looks good to me and now we have two blue colors so let's make this a different color maybe you make it red we want to give it a label as well so I'm going to do red I'm gonna say the label is equal to x-squared and one thing that's cool is that it automatically moved this legend to the top left because the positioning was better now that this goes up to the top right okay so we have two two lines up there and from the original image I showed there's some stuff that's still different one thing that's kind of cool that you could do is let's say you had a graph that stopped but you wanted to kind of show the projection of it not in the future so what I did to show that type of a phenomenon is I did like X to take the first four values here it'll plot that what did I do oh shoot first four so what I want I think that will work take the first floor and plot that and then for the last values so from let's say five or three are this make this a little bigger make this six or five bear with me here from four onward we'll make it a dash line and I had a little bit of overlap because I think they'll look better you'll see in a sec four - make this still the same function but this time we're gonna make it a dashed line so there you see and I'm gonna actually make this a little bit different there you see like maybe something ended here and then you make it - the rest of the way you could do something like that using the format I just showed all right what else do we have probably clean this code up a little bit another thing that I showed in the original graph that I showed when I was a introducing this video was a resize so let's say we wanted to resize our graph well we should do that by near the top of our graph so I'm going to go ahead and do resize your graph I'm going to go ahead and do plot figure pass in fig size and we're gonna say let's make the dimensions 5 by 3 it's kind of an arbitrary 5 by 3 that will give you kind of the ratio of your X to Y or Y but to specify how many pixels it is you're gonna want to pass in a second parameter which is dpi which basically is pixels per inch so I recommend using a value of around 300 here if you do smaller values you'll have more pixelated graphs so if you can afford having a bigger image having a bigger dpi is often nice so in this case we have pixels per inch of 300 so this is going to actually be 5 inches by 3 inches so that Tolle pixels will be 1500 by 900 here in this graph so now it's much much bigger maybe that's a little bit too big I didn't I'm gonna be blocking this graph so I'm gonna make it a little bit smaller but you get the idea then here so we'll make it like two by 1.5 that's not a good dimensions now though I mean just play around with this the the biggest issue I have right now is the graph looks so big because I have my screen super-high resolution or super the view is like very up close because I want to make sure you guys and see my code but yeah this would be a 1500 by 900 graph I'm gonna just for the sake of the graph decrease the DPI and make it more reasonable that the issue you can't really tell from this image but if you really zoom into the graph and maybe if you like printed the graph out it would look maybe a little bit pixelated with a dpi of 100 so probably try to keep this value higher if possible and then another thing you want to do is you'll want to save your graph so to say we can do plot dot savefig and when you do say buy graph dot PNG and one thing that's nice too is like it passing the DPI parameter here as well so let's say when I actually see if my graph wanted to be bigger but for the sake of being able to show you guys it in a smaller format I wanted the 100 above I can still go ahead and save it with 300 and then kind of solves both of my problems and so this then my graph top P&G will save in the current directory that this MATLAB matplotlib tutorial is in it was yeah well saving that current the current directory what ever fought whatever directory your file not lib tutorial as in this graph will also save there when we run this cool now we have that saved all right let me clean this up a little bit and just to note the reason I did this is because this is exclusive the six here so when I do five onwards it picks up this actually ends at five because it doesn't include six so it actually covers everything here all right now that we've shown line graphs a bit let's go and do quickly do bar charts and after bar charts I think we're going to just end this video short and I'll do the real-world examples in a video that I'll post next week I just think this video might get a bit too long if I include everything in just one video so all right so I want to do bar chart okay so bar charts are pretty straight for it as well let's say we had some labels a a B and C and some values that say 1 4 & 2 well if we wanted to do a bar chart where you just do plot dot bar of labels comma values at Friday they're wrong it's values comma labels and we get not what I was looking for okay let's see it is actually labels first then values ABC cool so that was pretty straightforward and I can do the plot touch show we're gonna do all the adding of titles and whatnot the same way as before in the line graph example let's skip over that for now it could also resize it let's say so I could do plot figure fig size equals it's a six by four it's gonna use whatever the default dpi is and then one thing that it's cool that you can do with bar charts is you could add different patterns to these so to do that and you can you can really play around with the documentation do all sorts of different I do some kind of following the graphs that I was showing the inner intro of my video so I wanted to just show kind of what different hash values look like so I can make first one have gonna say bars equal plug up ours there you go now I do bars zero set hatch that's gonna give me the lines that you see down here I can do the same thing with the second value let's make this circles and you can find all the hatch values in the documentation as well let's do the third value we'll do stars so now you get kind of a cool little bar chart I can also add a legend to this very easily and this will tell me right away we have a B and C but I mean their label right here some legends probably not necessary in this case alright let's see what else do I want to do with this and then if you wanted to make this a little bit neater if you had like many more I might get tiring to do this so you could do something like patterns equals /o and star and you could do instead of this notation you could do yeah for a bar and Baris bar dot set patch I guess we want an index as well we'll just do actually patterns dot pop first item off so this would get us our right one for each one should yeah look at that if I didn't pop regular I think it would take off the last so this will be stars instead of yep look at that yeah so basically what it did is it just popped off the first item for the first bar popped up the second item for the second bar and then popped off the final item for the third bar as you can see there so that's bar charts really quickly I think I'm going to end this video here at this the the real-world examples video will be very very useful and I'll post that next week I just want to like make this video too too long let me know your feedback hopefully this helped you a bit I'm happy to be back happy to be making videos just just trying to ease back into it with this if you enjoy this video make sure to throw a big thumbs up and subscribe if you don't mind yeah but yeah look look out for the real world examples video I'll be posting in the near future and let me know if you want to see anything else related to this library after that alright thanks guys for watching and peace out [Music]
Info
Channel: Keith Galli
Views: 114,605
Rating: 4.9800682 out of 5
Keywords: KGMIT, Keith Galli, MIT, Python 3, Matplotlib, python matplotlib, data visualization, python data science, python plotting, line graph, bar chart, plot data in python, how to use matplotlib, python, python programming, pandas, data, python data science tutorial, figures, resize python figures, add title matplotlib, label axis, jupyter notebook, how to make line graph in python, how to make bar chart in python, how to resize figure, font in matplotlib, add a title, rescale chart
Id: DAQNHzOcO5A
Channel Id: undefined
Length: 32min 33sec (1953 seconds)
Published: Sat Jun 01 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.