Transcripts
1. Welcome to the course: Hey, I welcome you to this amazing course on Nat Plot Lib for data visualization with Python. I'm a data scientist at the clicker data and I'll be instructor for this course. Data visualization is a highly effective methods for displaying and summarizing lousy amounts of numerical data. It is also tremendously useful for sewing trends, patterns, and relationships between various data points. No wonder a lot of scientific papers, as well as business reports consists of data visualizations. And this course you'll be learning everything about Matplotlib, Python library you specifically for data visualization, you'll learn the basic as well as advanced concepts related to Matplotlib. You'll also learn how to install and import matplotlib in Python creates quick and spectacular 2D and 3D Plots. Create subplots, add labels, annotations, etcetera. This course is aimed for developers, entrepreneurs, as well as researchers alike. So if you fall within any three of these categories, please feel free to join us. If you're ready, let us hop into the course and start coding.
2. Installing and Importing Matplotlib: Welcome to the first lesson of this course. Today, you'll be learning how to install as well as how to import matplotlib in Python. But before we dive into that, let's discuss a little bit about what is Matt got limb. Matt Bartlett is a data visualization library used for plotting static, animated as well as interactive visualizations with a high level of flexibility. It is written in Python and can be used in almost all ideas, including Jupyter Notebook by John spider is extra. I'll be using Jupyter notebook for this course, but you can use any of the idea that you are comfortable with. So to start this lesson, let's install macro short-lived using PIP, which is Python's back as mannerism. But first, make sure to have the latest version of Python installed. Then open up your command prompt or domino and write the following command. Pip install Mac dork level. If you are using Anaconda, is highly likely that Matt Gottlieb comes pre-installed. So I'm going to open up my command prompt now and write pip install matplotlib. So this will take a little bit of time, but it's quite flushed and death. So it shouldn't overhead that successfully installed matplotlib 3.3.1, and this is the version that we have just installed. So do check if it is actually working out. Nor let's import map Lord live in Python using the following goal. So we're importing the map plot lib library with a smaller notation or sort of notation as MPL. And this is the general convention when importing the library. So I'll just run this cell. And then to check the version of the install matplotlib library, we can print it out. So we have the version has 3.3.1. And since we do not have any areas where you're doing all of this, what successfully installed matplotlib as well as imported it in Python. So in the next lesson, we'll be learning the basics of matplotlib, as well as creating our first block using matplotlib. Sudan.
3. Basics of Matplotlib: Welcome to the second lesson of this course. In this lesson, we will learn about the basics of Makkot limb by creating a simple line plot. We will be using the pyplot module from the matplotlib library to create the line plot. It consists of many helper functions as well as methods which will help us easily. Chains are plot style as well as properties. So let us start with the listen by importing the pyplot module using the following line of code. One thing to note here is that we're not importing the entire map plotly library, but instead we're calling the pyplot module off of it and importing it as PLT. So if I run this line of code, I'm importing the pyplot module as PLT and Python grade. Now, since we need some data to plot, let us define two Python lists representing the x and y axis values of the plot. I've already defined this lists overhead. And I've assigned them to two variables, x and y. The X variable represents the x axis values photo plot, and the y variable represents the y-axis values for the blood. So I'll run this cell and I'll do the assignment. Then we can finally create a line plot using the pyplot module. For this, we'll be using two methods, deplored method and the show method. Deplored method takes in two parameters as X axis values and y axis values, denoted by the variables x and y, respectively. Then it creates a line plot based off this two values. The show method is used for displaying the blood onto the Jupyter Notebook on any other ID that you may have been using. Also, you can notice that we're calling these methods off of the pyplot module, which we had assigned the name PLD when importing. So if I run both of these lines of code, the output will be displayed. And this is our first map plot lib plot, which is a line plot. Although there's not a lot of information in this plot, I'll just quickly cover the essentials. The bottom part of the plot is the x axis values, and the left side of the plot is the y-axis values. And the plot is being made by taking a combination of the x axis values and the y axis values to form coordinates. This coordinates are being plotted onto the plot. And since this is a line plot, a straight line is being passed to all of these coordinates. So let us understand this by the example itself. So for the x axis values, we have defined 1234 as the x-axis values. And for the y axis values, the values are 2468. So here, full coordinates are possible by the combination of the x axis values and the y axis values. So the coordinates are one comma 22 comma four. Three comma 64, comma eight. So we have four coordinates. And if we look at the plot, we can see that the straight line is passing through one comma 22, comma 43, comma 64, comma eight. You may have also noticed that there are some filler values over here, so 1.52.53.5357. And although we did not specify this in our x-axis values or the y axis values. These are being put overhead by the pyplot module in order to create a much more neater, a much more spacious plot, and actually quite like this about matplotlib. Next, let's provide some more information to the plot by adding in a title as well as excess label. And I've already written down the goal. So as before, we're creating a line plot by calling the plot method and passing in the parameters as the x axis values and the y axis values. Then the exceptional thing that is happening overhead is we are adding in the title method, the x level method and the y level method to set the title x label and y label respectively. And we are passing in strings as parameters overhead. So if I run, this cell, will get the following plot. And we can see that we have the title, the x label, and the y label. You can change these strings by just changing the values of a hair. So if I put overhead line plot and maybe the x label will be X, X and the Y label will be y, x. So if I run this, the changes have been reflected in the plot. Title is now aligned clot Dax axis label is the ax, ax and the y-axis label is y, x. So I hope you get the point. We haven't done much till now, but for a short recap, I'll just go over it. So at the beginning of the lesson, we imported the pyplot module as BIT in Python. Then we created some dummy data as list and assign it to two variables, x and y, representing the x axis and the y axis values of the plot respectively. Then we created a line plot using the plot method and we displayed it onto the Jupyter notebook using the so method. Then, right now, we just added in the title, the x label, the y level using this three methods respectively. Ok, grid. I'll just revert back to what it was before so we can continue on with the lesson. Now, let's change the color of the line, as well as increases thickness so that we can play around with the plot a little bit. So to do that is quite simple. As before, we're doing everything like we had done and with the exception of adding this parameters and their properties. So in deplored method, we had only been specifying the x axis values and the y axis values. But right now, I'm also specifying the color diameter with the value green and the language parameter with the value five, which is the thickness of the line. So if I run this, the new plot. Has a line which is indeed green in color and has much more thickness than, than what we had before. So now, one thing that is quite bugging me is there is a lot of whitespace over him and the plot is quite blurry. So to fix this, we can choose the size of the figure as well as increase the resolution of the plot. So to do that, we'll be using a method call the figure method. So the figure method is responsible for changing the figure object upon which the plot is being made. So overhead when we choose the parameters of the figure method, but actually changing the parameters of the entire plot itself, which is the figure object and the plot itself because it is being plotted on top of the figure object. So if wrestle right now, let's have a look at this. So as before, we have all the code which we have written before overhead and this new line of code is present. The figure method is taking in two parameters, the fixed size and the DPI. The fixed size parameter represents the figure shape, or you can say the figure size. And it takes in a tuple of two values, the Wheaton inches and the height in inches of the plot. So though with overhead is ten inches and height of four inches and the DPI or the dots per inch, is the resolution of the blood. So 200 resolution is being set for as the DBI. And since everything else is the same, let's just run this piece of code. Okay, and now we have a much wider plot as well as a much sharper plot. Now, what we can do is we can also add some pics onto the plot, as well as some arrow. By some arrow I'm in one arrow for now. So we will be using the annotate method. The annotate method is responsible for doing this and I'll just show you the plot first. So the annotate method is useful for creating a text on top of the blood, as well as creating an atom. And this is useful many times. So let's have a look at the code with USDA figure method as before to change the size of the figure as well as the resolution. Then we've used a plot method to create a line plot with color green and line width five, which you have already covered. Then we have not covered this. And except that we have already covered the three methods over here as well as the sole method. So let's look at the annotate method and let's look at its diameter one by one. So the first parameter is a string, and this is actually a text that is written on the plot itself. So if I change the text over here, so I put this as a green line. This will be reflected onto the plot. Then the next spammy to x y is a coordinate or a tuple consisting of two elements. The first element is the x axis value, and the second element is the Y axis value. So this coordinate actually represents the coordinate where the head of the arrow is pointing at. So since the x-axis value is 2.5 and the wax is value is 5.5. We can see that the x, x is 2.5 is overhead and 5.5 is overhand. So this is where the head of the arrow is pointing at. So we can choose the value of the x-axis and the y-axis to make this arrow point to a different direction. So let's point it to maybe 3.57.5 for now. So if I run this, okay, now we're pointing to a 3.57.5. I hope that makes sense. So I'll just revert it back to what it was. Now on to the next parameter, x, y dx. The XY to expand meter is taking in to elements in a tuple as before. The first element is the x-axis value. The second element is the Y axis value. So that's why it takes, is taking in a coordinate of values. And this coordinate is responsible for determining the starting position of the text. So if we see we have 1.5 and the x axis, seven in the y axis. And if we look over here, you have 1.5 and the x axis and seven in the y-axis. So this is the starting point of the text. And we can choose the starting point by just changing the values of these coordinates. So for example, I'll put overhead 3.5. and output overhead may be six. So if I run this, we can see that the x axis, Wiley was 3.5 and the wax is value was six. So this is the starting point. And we can see that map Lord leap has dynamically changed the direction of the ADA as well in relation to the text. So this is a very neat feature in map plot lib. So I'll revert it back to what it was. Okay, now on to the next parameter. The next band meta is Addo props, which is a dictionary of properties. So this adder props family does is responsible for changing the arrow, which is present over hand. The first property, face color is given as black. So this is defining the color of the atom. So if I change from black to red, the adder will be soon like this. Okay? And the shrinked bam EDA is defining how much we want the length of the arrow to be shrunk. So if we put 0.06 over him when defining that we won the length of the arrow to be shrunk by 0.06. And if we run this code, indeed, the arrow is much more smaller right now. So again, how did you vote everything back to normal? And I hope you've catched up till now. So let's just look at the score for a brief second so that we not lose track of what we're doing. We're calling the figure method with changing the figure size and the resolution. We're plotting line plot using the x axis values, y axis values defined by the variables x and y, where giving it a color of green, we're giving it a language of five. Then we're using the annotate method to annotate the plot with text and an atom. And here we have the text as this is a green line. And we have XY, say Type 2.55.5, which represents where the arrowhead is pointing. Then X-Ray techs is set at 1.57, which represents the start of the text, which is 1.57. And the ADA props parameter specifying the face color, which is black, and the shrink size, which is 0.05. And we're adding the Title X label, like label by calling these methods. And finally, we are displaying the plot using the symmetric. Great. Now let's add another line plot into the same figure, and this is quite easy. So what we'll be doing is we'll be creating another dummy values so that we can plot the landlord onto the figure. So far that I've defined two lists as before with different values. So we had the values over here as 12342468. For the new line, I'll be defining new values which are 12341234. So E will be representing the x-axis values for the line plot. B will be representing the y-axis values for the line plot. And as before, we're calling the figure method and setting the figure size and resolution, we're creating a line plot using the x and y variable. But the difference now is that creating another Line Plot which is using the x-axis values and y axis values as eNB variables. And we're setting the colors red. And the language is as same as before. Five. Also, we've already looked at the annotate method and other methods, men's and overhead. So let's just run this cell and let's see what happens. Okay, we have blue line plots made on top of the same figure. And if you want to actually check if this is the right plot that is being made, let's look at the coordinates. One comma 12, comma three, comma 34, comma four. So if we look at this one, comma 12 comma three, comma 34, comma four. So this is indeed the line plot that we intended to make. And we can again decrease the thickness of the line. So let's say we can junzi to one so that it is shown as a different line. Okay? I hope you understood that. Now finally, for this lesson, as a good convention, let's add a listen to actually signify which line is what. So for this, again, everything is same as before. The only line that we adding and discord is this lesson method. The listen method takes in a parameter as a list consisting of the number of elements in accordance to the number of thoughts that were made. So since we are made to line plots, we're passing into elements. The first element represents the first plot that is being made, and the second element represents the second block that is being made. So if we run this cell, we can see that the listener has indeed appeared. And since the first element is the green line, which is being plotted, it is indeed showing that this is a green line. And similarly for the red line. I hope you've learned a lot in this lesson, and I hope that you look at this lesson again so that you get a hold of the basic concepts. And in the next lesson, we will be learning how to make subplots, as well as how to save the figures that we have made. So see you there.
4. Creating subplots and saving our figure in Matplotlib: Welcome to the third lesson of this course. In this lesson, we'll cover how to create subplots and how to save our figure in matplotlib. Sometimes it is necessary to plot multiple plots within a single figure. In such cases, we use the sub-plots method of the pyplot module. We may also want to save the plots that we make. And for this, we'll be using the specific method of the pyplot module as well. So let us get started with the lesson by plotting some subplots and matplotlib. The first line of code overhead simply imposed the pyplot module as PID in Python. So by running this line of code, I've successfully imported the pyplot module. Next, let's create a set of subplots like dividing the map Lord lives figure object into one row and two columns. And this can be done by using the sub-plots method, which is called off the pyplot module. And it takes in two parameters. The first parameter is the number of rows which are unspecified, has one, and the second parameter is the number of columns which are specified has two. Then the show method is being called to display the plot being made by the sub-plots metered onto the ID. So if I run both of these lines of code, we get the following plot as a result. And although no data has been plotted over here, this is enough to understand the concepts. So let creating a single figure and we're splitting it into one row and two columns. So as a result, we are getting two subplots in this figure. Take a guess at what would happen if we change the number of rows to two. How many subplots would be made? So I run both of these lines of code again. And we can see that we now have four subplots. This is because we're splitting the figure into two rows. This is the first row and the second rule, and we are also spitting the figure into two columns. The first column, and this is the second column. Okay, how about we remove both of these parameters from over here? So this will result in a single figure which is the entire subplot itself. And great, I hope you've understood the concept of subplots by now. And let's just return back to where we were to continue on with the lesson. One thing I have to mention right now is that whenever we call the sub-plots method, we get a tuple return back. A tuple will consist of two elements indicating the entire figure object itself. And the other element will be the area of access objects. So I'll just print the meter wireless being called to show you what the tuple will look like. Great. So we have a tuple of a hair of two elements, and all of these are of datatype object. The first element is the figure object itself with the size and the number of subplots defined. And the second element is an array of access object, which is used to access the sub-plots overhead when we want to plot the data. So the first element of the axis object is responsible for giving us access to the first subplot, whereas the second access object overhead is responsible for giving us access. And the second subplot. Okay, one other thing is whenever we get some values return from a function or a method, we generally assign it to variables. And so I'm assigning it to two variables overhead. So the first element, which is the figure object, will be assigned to the variable. And the second element, which is the axis object or the area of access object, will get assigned to the X variable. And I'm printing it out so we can see if this assignment is actually being done or not. And this is also called tuple unpacking if you're familiar with some advanced concepts of Python. So I'll run this. Okay, the fig variable, indeed, the figure object as we have printed it out over here. And the x variable is indeed the adding of access objects in. Great, now let's create some dummy data for plotting onto the subplot. We'll start by plotting onto the first sub plot, and then we'll move on to create a plot on the second subplot. So for this first subplot, I've already defined the dummy data and X1 variable represents the x-axis values for the first subplot, and Y1 represents the y-axis values for the first subplot. Now, let us plot the data. This can be simply done by selecting the axis object, which we had already seen overhead. Since we're using the, since we need access to the first subplot, we'll just select the first access objects in the area of access objects. Ok, so this is simple Python indexing. And then we're creating a line plot by calling a plotting method off of it. And we're passing in the x-axis values, the y-axis values, and the color, which I've specified as green, and the line width was specified as five. And finally, recalling the show method to display the plot onto the Jupiter notebook. So let's run all of this code once. So first, I'll assign the variables with their data. Then I'll plot the data onto the first subplot. Great, we have just plotted our data onto the subplot. Now, let's plot another batches of data on to the next plot. And this can be done by defining another other two variables, which I have defined here as X2 and Y2 with different values than what it was before. So now we can access the second subplot by just selecting the second axis object using Python indexing. And as before, we're just plotting the line plot using this line of code. So if I run this, okay, I forgot to assign these variables with the values. So I'll run this first, and then I'll run this again. Great. Now we have made the second plot as well in the second subplot. Also, you may want to assign the title for each of the subplots, as well as the individual x label and y label for both of the subplots. This can be done by using the set title, set x label and y label methods as I've sown overhead. So simply, we're just selecting the subplot that we want access to. And we're just calling the method off of it and passing in a string. So here we are manipulating our way, changing the first sub plot, and here we're changing the second subplot. So if I run all of this, we get the following figure. Great. One thing that we can do now is since the subplot method has been using the figure object itself, we can also change the figure size. So let's put it at fixed size equals two comma four, which is ten width of inches, ten inches of width, and four inches of height. And the resolution as DPI equals to 200. So if I run this, we get a wider figure, wet, a sharper resolution. Now, let's finally save the map plot lib plot tech that made till now. So this can be done by simply using the safest method. As I mentioned in the beginning of this lesson, the Pacific method simply takes in a file path. So I have a directory name save plots, and I wanted to save the whole plot as with the name subplots dot PNG. One thing to note over here is that you want to use this method at the end of your plot, just above the show method, so that all of these have actually been plotted onto the figure. So if I run this and if I check my local directory, we have subplots dot PNG file over here. And if I open this, we have our subplots. Great. We have learned how to create subplots, as well as how to save our figure and matplot limb. In the next lesson, we'll learn how to create some 2D plots. So see you there.
5. Plotting 2D plots in Matplotlib: Welcome to the fourth lesson of this course. In this lesson, we'll learn how to create various kinds of duty plots and Matlock live. The different types of 2D plots that we'll cover in this lesson are the scatterplot, the barplot, and the pipeline. So let's get started. The scatterplot uses dots to represent values in the plot. It can be made using the scatter method of the pyplot module. So let us import the pyplot module from Matplotlib to get started. I've already written the code of a hair and we're just importing the pyplot module from the matplotlib library as BID. I run this line of code to make the import happening. Great. Now let's create some dummy data for the x axis and the y axis values of the scatter plot. I've already defined the values overhead and I'm assigning them to do variables x and y. The x variable will be representing the x-axis values of the scatter plot. And the y variable will be representing the y-axis values of the scatter plot. So let's just run the cell and do the assignment. Now, let's plot the data in a scatter plot using the scatter method. So I'll just go line by line to explain what is happening. We have learned before. The figure method is being called the chance the figure size, as well as the resolution of the plot. And the new thing over haze that we're calling the scatter method off of the pyplot module and passing in the battery does as the x axis values and the y-axis values. So X variable defines the x axis values and y-variable defense to Y axis values. Then we're adding some title, some x label and the y label to the plot. And finally sewing the plot using the show method. So if I run all of this code right now, the result will be like this. So we have a basic scatter plot. And before we continue, I'd just like to point out one thing. So in the last chapter or in the last lesson, we had done that when we use the sub-plots module or the sub-plots method, we were returning back a tuple of elements. So similarly, when we call the plt.figure method, what happens is we actually return back or figure object. So if I print the method when calling it, you can see that we're returning back or figure object. And this is a figure size of 2 thousand by 800 with XHR axis because we're not plotting on this figure right now. So good convention is to assign a variable to whatever is being returned. In the, in this case we're returning a figure object. So here I'll just put it was to plt.figure so that we can return the figure object as a fig variable, grade. I. Run this again. And from now on we'll be using this convention. So moving on, let's change the size of the dots, as well as the colors of the dots in the scatter plot. And since we have five dots over here, we have five different value combinations or coordinates. So let's just define five different sizes for five of the dots and five different color values for d5 dots. So once I've done that, I'll just assign it. And
6. Plotting 3D plots in Matplotlib: Welcome to the fifth lesson of this course. In this lesson, we'll learn how to plot various kinds of 3D plots in math dot limb. A 3D plot is a plot where data is plotted on the x, y, and z axis. Till now, we were only dealing with 2D plots, which had only the x-axis and the y-axis. The different types of 3D plots will cover today are the 3D space plot, that 3D line plot, and the 3D scatter plot. To create a 3D plot, we'll need to import the access 3D object from the employer 3D model of the MPL underscore bool gets library as I've shown over here. And we will also be importing the pyplot module for plotting. So I'll just run this line of codes and the import is done. Now, the first plot that we're making is a 3D space block. To make a 3D space plot, we'll call the access method of the pyplot module and set the projection of the axis do 3D. And we'll call the show method to display the plot. So if I just run this, we have our 3D space block. Also. One general convention is that we assign the return value of the access method, which is the axis object to a variable named x. So I'll just write this and I'll run it again. Great. Now let's make a 3D line plot on top of this base plot. To make it, we'll be using the plot 3D function of PyPlot. So for plotting a 3D line thought will have to specify the data for the x-axis, y-axis, and z-axis as soon overhead. So where I've already defined the values for the three axes and I'm assigning them to the variables x, y, and z. And they will be representing the values for the x axis, y axis, and z axis respectively. So I'll just run this line of code and the assignment is done. Now, let's look at this. So we're calling the access method to set the projection of the access to 3D. And we're assigning it to the x variable, which is the axis object is being returned overhead and being assigned to the X variable. So we can use the axes object, which is the x variable overhead and thought on top of the axis object. So I'm just calling the plot 3D method of the axis object. And I'm passing in the values as the X axis values to Y axis values and the zed axis values. Then I'm also signing the x label, y label and zig level off the axis just by using the set underscore x label, set underscore y label and centre underscored zed level methods. Then I'm displaying the plot in this way. So I'll just run this and we have a line plot. So this is the x-axis, this is the y-axis, and this is the zed axis. And let's just look at how the plot is being made. So we have the data over here. So for a single coordinate, three different points, or the combination of the x axis values to Y axis values and the zed axis values are being taken. So we have a total of five coordinates overhead. The first coordinate is one comma, one comma four, another is two comma two comma ten, another is three comma three comma 20. And similarly, the remaining is as follows. Okay, let's look at the 1.1st comma one comma four. So in the x-axis, where at one, in the y axis we're also add one. But in the z-axis, where ad for this means way increasing in the height. And we're at number four over here from the z-axis. So this is vertically going upwards. And similarly, another point, let's say three comma, three comma 20 OR gate. So from the x-axis, we're going to the 0.3 and from the wax is, we're also going to the 0.3, which is overhead. And from the zed axis we're going straight up to the 0.28. And in this way, the pyplot module is passing the line through all of the coordinates that are being plotted over here. Great. Now let's make a scatterplot. Making a scatter plot is easy and we can just call the scatter 3D module by first assigning the axis object and calling the scatter 3D method off of it. Discarded 3D method also takes in three parameters, the x-axis values, y axis values, and the zed axis values. Also, we're setting the x label, y label and the zag level in the following way. Now, using the same method to display the plot, let's look at what happens. So we have our scatterplot and the points are being plotted in the same manner as we had discussed for the line plot. O, this quick lesson was enough for you to get a grasp about how to create 3D plots and matplotlib. And in the final lesson of this course, we'll learn how to work with images and display it on the Jupyter notebook or on any ID that you are using. See you there.
7. Plotting images in Matplotlib: Welcome to the sixth and last lesson of this course and the sort lesson, we'll learn how to plot MSS and matplotlib. Matplotlib provides an easy interface for plotting images and we'll be needing to map plot lib modules to do so. The emus model will be used to reading the images and the pyplot module will be used to plot the image. So I've already written down the quote of a head for importing these modules. So we're importing the images module as MPI Mz, and we're importing the pyplot module as BIT. So I'll run these lines of code and make the import happen. Now, let us read an image using the EM grid function of the image module by specifying the URL of the image. So over here, I'm using the read method off of the images module, and I'm passing, passing in a URL as a string. So this URL actually points to this image overhead. So this is the flag of Nepal and we're using this, you add o. Now if we run this line of code, what happens is we get returned back a numpy array, which we are assigning to the IMC variable. So if I run this, we now have the IMC variable assigned with the pixel values of the image as a non dyadic. So let's just look at the type of the image variable or which is the IMC variable. So if I run this, It's indeed a numpy AND edit. And since it is a known by adding, we can look at SIP as well. So here we can see that there are three different values for the same. This means that 165 rows of pixel, 135 columns of pixels, and for different color channels, the color channels represents the red, green, blue, and alpha, making four different color channels over here. Now, let's just plot this Numpy AND Adi and see if matplotlib can successfully saw this image over here. So if I run this, we get the flag of Nepal. Great, successfully ended our course on Nat Plot Lib. And I hope that you've learned all of the basics about matplotlib so that you can create your own amazing thoughts. Bye-bye.