youtube-transcript.ai

Learn Matplotlib in 1 hour! πŸ“Š

Watch with subtitles, summary & AI chat
Add the free Subkun extension β€” works directly on YouTube.
  • Watch
  • Subtitles
  • Summary
  • Ask AI
Try free β†’

This video provides a comprehensive introduction to Matplotlib, a powerful Python library for data visualization. It covers the basics of installing Matplotlib, creating various types of plots like line charts, bar charts, pie charts, and scatter plots, and customizing their appearance with different markers, colors, and line styles. The tutorial also delves into adding labels, titles, grid lines, and organizing multiple plots using subplots, demonstrating how to integrate Matplotlib with libraries like NumPy and Pandas for more advanced data analysis and visualization tasks.

Full Transcript

https://www.youtube.com/watch?v=c9vhHUGdav0

[00:00] Hey everybody.
[00:01] In this video, I'm going to teach you everything you need to know to get started working with Mattplot Lib.
[00:07] So, I encourage you to sit back, relax, and enjoy the show.
[00:12] Mattplot Lib is a Python library for creating charts and graphs.
[00:18] You can take plain numbers and turn them into visuals you can actually understand.
[00:22] With just a couple lines of code, you can make your first graph.
[00:25] Boom. Instant line graph.
[00:28] You can create line charts, bar charts, scatter plots, histograms, and my favorite, pie charts, because I like pie.
[00:34] You can customize the appearance of what your charts look like, too.
[00:37] Here, I went a little crazy with the customization, but I think you get the idea.
[00:41] What I would recommend before starting the series, be familiar with all Python subjects up to dictionaries.
[00:46] Knowing numpy and pandas are helpful, but not necessary for this series.
[00:50] I do have some free courses on YouTube to catch you up to speed if you're interested.
[00:53] Anyways, let's begin.
[00:56] Oh yeah, let's do this.
[00:58] To start working with mattplot lib, we have to be sure
[01:00] with matplotlib, we have to be sure that we have the library installed.
[01:02] One easy way to check that at the top of your IDE, if you were to import matplotlib.
[01:09] If it's underlined red, you can hover your cursor over it and install the package by clicking this link.
[01:17] Now, if this popup doesn't appear, if you're using PyCharm, you can open up a terminal.
[01:22] Alt F12 is the shortcut.
[01:26] Then use pip to install Matplotlib.
[01:31] Pip install Matplotlib.
[01:32] Enter.
[01:36] Give it a moment to install.
[01:38] And that's all you got to do.
[01:41] We can close out of this terminal.
[01:43] And that red underline should go away.
[01:47] To check the current version within a print statement, we will print access the library of matplotlib.
[01:55] Dot dunder meaning double underscore version dunder.
[01:59] Again, the current
[02:02] version dunder.
[02:05] Again, the current version of mattplot lib that I'm using is 3.10.6.
[02:09] We can delete this print statement.
[02:12] We can delete this print statement.
[02:15] There's a module called pipplot.
[02:17] It's found within the library of mattplot liib.
[02:19] It provides a userfriendly interface for plotting.
[02:22] We're going to import that module by following mattplot lib with pi plot.
[02:27] So al together import mattplot lib do import matt import mattplot.pipplot.
[02:39] That's a tongue twister.
[02:41] Just so we don't have to type this long name every time.
[02:45] We're going to give it an alias or nickname as plt for short.
[02:51] So in order to access here we go mattplot lib.pipplot.
[02:55] We're just going to give it a nickname of plt.
[02:59] Here's how we can create a basic plot.
[03:02] We'll need a set of x and ycoordinates.
[03:05] We'll need a set of x and y coordinates.
[03:05] We'll start with a basic Python list.
[03:07] We'll start with a basic Python list.
[03:07] For the x coordinates, we'll say x equals a Python list.
[03:13] A Python list.
[03:13] Here you can type in some numbers.
[03:16] I'm going to type in some years.
[03:18] 2023, 2024, 2025, and 2026.
[03:25] And then for the y-coordinates, this will be a separate Python list.
[03:27] Make up some numbers.
[03:30] Uh in this scenario, let's pretend that we have different class sizes each year.
[03:33] Starting with 2023, there's a class size of 15 students.
[03:34] Then for 2024, we have 25 students, then 30 and 20.
[03:38] Feel free to pick different numbers if you would like.
[03:42] We have a list of x coordinates and a list of y-coordinates.
[03:47] Each set of these elements is a pair of x and y coordinates.
[03:50] We have to be sure that they match.
[03:52] Now to create a basic plot, we're going
[04:06] Now to create a basic plot, we're going to access plt.
[04:09] To access plt, call the plot function and then pass in our coordinates.
[04:15] X, y.
[04:17] The first argument is for the points on the x-axis.
[04:21] The second argument is for the coordinates on the y-axis.
[04:25] Now, if I were to run this, nothing displays.
[04:27] You also need to call the show function of your plot.
[04:30] Here's how.
[04:34] When we would like to show our plot, we will access plt, call the show function in order to show our plot.
[04:44] Now I'm using PyCharm.
[04:44] My plot appears on the right hand side in a separate window called plots.
[04:49] Here's my x-axis with all of my years, but we'll have to do some customization in a future topic.
[04:58] Then here are all the y-coordinates.
[05:01] For 2023, we have 15.
[05:04] 2024 is 25.
[05:07] 2024 is 25.
[05:07] 2025 is 30.
[05:07] And 2026 is 20.
[05:12] And you can 2025 is 30.
[05:12] And 2026 is 20.
[05:12] And you can change these coordinates, too.
[05:14] I don't change these coordinates, too.
[05:14] I don't know.
[05:17] I'll set 30 to instead be a,000.
[05:17] Let's see what this looks like.
[05:20] And we get a different plot,
[05:22] but I'll change that back.
[05:25] You can also close out of past versions
[05:27] of plots by hitting this X button next
[05:29] to each.
[05:32] Now, for the plot function, if you only
[05:34] pass in a single parameter, this
[05:37] argument would be for points on the Y
[05:39] ais.
[05:40] And here's what that would look like.
[05:44] The default behavior for plot is to
[05:47] start at zero and place a point at each
[05:50] increment of one.
[05:50] 1 2 3.
[05:55] But each of these ticks, they increment by.5 each.
[05:59] The points on the xaxis will be provided
[06:01] to you.
[06:03] But we'll pass in both x and y.
[06:07] But we'll pass in both x and y.
[06:09] Now, if you're familiar with numpy, we could instead use numpy arrays because they're faster than python lists.
[06:14] To set that up, we will import numpy as np for short.
[06:22] We'll assign x to be a numpy array.
[06:25] nparray function and then pass in your Python list.
[06:31] Then do the same thing with y.
[06:35] nparray pass in your Python list.
[06:38] And that's all you have to do.
[06:41] Numpy arrays are faster than Python lists and give you more functionality.
[06:46] Your homework is in the comment section.
[06:48] either post two lists or two numpy arrays.
[06:51] Try and think of something creative if you can.
[06:53] All right, everybody.
[06:54] And that is an introduction to creating basic plots in mattplot lib.
[07:05] In this video, I'm going to show you how we can start customizing our plots.
[07:07] More
[07:09] We can start customizing our plots.
[07:11] More specifically, we'll be customizing the lines and markers.
[07:13] Here's how.
[07:15] Within our plot function, we can pass one of a few keyword arguments.
[07:18] One of which is marker.
[07:20] Marker equals a given character.
[07:22] Depending on the character that you set for marker, your marker will be one of a few symbols.
[07:28] For example, if your marker set to be a period, a point, each set of coordinates has a point, although it's a little hard to see.
[07:35] I'll try and zoom in.
[07:38] You can see that each set of coordinates has a point.
[07:41] Another is circle.
[07:44] That would be an O.
[07:47] Triangle down is a V.
[07:51] For a star, you can use an asterisk.
[07:53] These are a few markers you can use, but not all of them.
[07:55] If you read the documentation, there's a fairly extensive list for our project.
[07:58] For the marker, we'll set it to be a dot, a period.
[08:02] But then you can change the marker size if this is too small.
[08:04] That will be a separate keyword argument.
[08:09] For the keyword argument of marker size,
[08:13] For the keyword argument of marker size, pick a size.
[08:13] Here's what 10 looks like.
[08:17] Here's 20.
[08:20] Here's 20.
[08:20] 30.
[08:23] 30.
[08:23] What's a th00and look like?
[08:23] I'm curious.
[08:28] No, go back.
[08:28] Go back.
[08:28] Go back.
[08:31] No, go back.
[08:31] Go back.
[08:31] Go back.
[08:31] We'll set it to be 30.
[08:33] it to be 30.
[08:33] For the marker size, it can be abbreviated to MS.
[08:35] For the marker size, it can be abbreviated to MS.
[08:38] abbreviated to MS.
[08:38] MS meaning marker size.
[08:40] But for teaching purposes, I'll keep this as marker size just because it's easier to read and understand.
[08:43] purposes, I'll keep this as marker size just because it's easier to read and understand.
[08:44] just because it's easier to read and understand.
[08:44] Here's how to change the marker colors.
[08:46] understand. Here's how to change the marker colors.
[08:46] This is a separate keyword argument.
[08:48] marker colors. This is a separate keyword argument.
[08:52] Marker face color.
[08:53] Marker face color.
[08:53] And here you can pick a color.
[08:57] face color. And here you can pick a color.
[08:57] You can select the name of a color.
[08:59] color. You can select the name of a color.
[08:59] You can use RGB values, hexodimal values, or HSL values.
[09:02] color. You can use RGB values, hexodimal values, or HSL values.
[09:02] Let's start with something simple like the name of a color such as cyan.
[09:05] values, or HSL values. Let's start with something simple like the name of a color such as cyan.
[09:07] something simple like the name of a color such as cyan.
[09:11] The markers are now this color, cyan.
[09:14] The markers are now this color, cyan.
[09:16] But for better customization, I recommend looking up a color picker.
[09:19] So, here's a color picker that we could use.
[09:21] I would like a very specific shade of blue.
[09:24] Something like that's pretty good.
[09:29] I'll just select this hexadimal value.
[09:34] We're going to paste that hexadeimal value and rerun this program.
[09:36] And now we have a very specific shade of blue for the marker face color for this keyword.
[09:39] You can also shorten this to MFC.
[09:41] It's shorter but not as readable for a beginner.
[09:46] I'll keep it as is. Marker face color.
[09:48] Then for the edges, you can use marker edge color.
[09:50] I'll set it to be the same color, or you could pick a different color if you would like.
[09:52] We'll keep it the same.
[09:55] Now, the edges of each marker are the same color as the face.
[10:14] are the same color as the face.
[10:16] The abbreviated version of this word is M EC, in case you see it.
[10:20] Now, we'll start working with the lines.
[10:23] You can select a line style.
[10:26] The default is solid.
[10:28] Let's use a dashed line.
[10:32] Here's what it looks like.
[10:36] for the line.
[10:38] It's now dashed.
[10:41] Or we could do dotted dotted.
[10:44] dotted.
[10:47] We have a dotted line now.
[10:49] Or dash dot dash dot.
[10:53] dash dot.
[10:56] It's a combination of dashes and dots.
[10:58] Or for no line, if you just want the points, the markers, you can set this to be none.
[11:04] Then we have no line,
[11:08] but let's set it to be solid.
[11:11] Solid.
[11:15] Here's how you can change the line width.
[11:17] Here's how you can change the line width cuz right now it's fairly skinny.
[11:20] cuz right now it's fairly skinny.
[11:20] We will pass in a keyword argument of
[11:22] We will pass in a keyword argument of line width.
[11:24] Normally it's one, but here's what two looks like.
[11:29] I'll set this to be three.
[11:33] I'll set this to be three.
[11:33] four.
[11:35] four.
[11:35] And I don't know what's 10 look like.
[11:39] And I don't know what's 10 look like.
[11:39] That's a little too much.
[11:41] Let's set it to be four.
[11:44] to be four.
[11:44] Yeah, that's pretty decent.
[11:46] Here's how to change the line color.
[11:50] to change the line color.
[11:50] Set the color to be then pick a color.
[11:55] Set the color to be then pick a color.
[11:55] I'll use that color picker again.
[11:58] I'll use that color picker again.
[11:58] Let's see.
[12:01] I'll select something kind of dark blue like that.
[12:04] dark blue like that.
[12:04] Copy and paste the color value.
[12:07] color value.
[12:07] And here's what we got.
[12:11] Our line color is now a very specific shade.
[12:14] is now a very specific shade.
[12:14] Now, let's say we have more than one
[12:16] Now, let's say we have more than one line to customize.
[12:18] I'm going to create another numpy array.
[12:20] We'll rename y as y1.
[12:20] Plot x and y1.
[12:25] Then we will also have y 2.
[12:25] And I'll just do some copying and pasting.
[12:28] Y 2 will have some different values.
[12:31] Make up some values.
[12:33] I'll say, I don't know, 17, 23, 38, and I don't know, five.
[12:37] Again, your values don't have to be the same as mine.
[12:45] Feel free to pick some different numbers.
[12:48] We're plotting X and Y1, but we'll also plot X and Y 2.
[12:51] That set of coordinates.
[12:56] access plt.
[12:56] Call the plot function.
[13:00] Pass in x and y 2.
[13:05] So now we have two lines, but our second line doesn't have the same customizations as our first line.
[13:08] Here's one option, although I don't recommend it.
[13:10] You could copy and paste all of these keyword arguments and pass them
[13:17] these keyword arguments and pass them in.
[13:19] in.
[13:21] This would work, although here's a much better option.
[13:24] You could instead create a dictionary with all of these keyword arguments.
[13:26] And then when we plot a line, we can just unpack our dictionary.
[13:31] Here's how we can do that.
[13:33] Let's create a dictionary which we'll name line style.
[13:39] This will equal call the dictionary function.
[13:41] To create a dictionary, we'll cut all of these keyword arguments and then place them within this dictionary function.
[13:54] Now we could just pass in this dictionary when we plot a line.
[14:00] We just have to unpack it using double asterisks.
[14:05] Unpack the line style dictionary.
[14:08] Then it's as if we're passing in all of these individual keyword arguments.
[14:12] We'll do this for the second line as well.
[14:16] unpack our line style dictionary
[14:19] unpack our line style dictionary and it's the same.
[14:23] Let's create a third line.
[14:24] We'll name this line Y3.
[14:27] This will be another numpy array.
[14:31] Make up some values 13, then 15, 20, and 30.
[14:38] Then we'll have to plot this line.
[14:41] Access plt.
[14:44] Call the plot function. pass in our new coordinates x and y3.
[14:47] Then we will unpack our dictionary of line style.
[14:50] Then we should have a third line,
[14:53] which we do.
[14:55] All of these lines are the same color.
[14:57] That's because with our dictionary of lines style, we've set all the colors to be the same, but you could pass in an individual color.
[15:01] Think of this as the default color for all lines if we use the line style dictionary.
[15:04] Another option is that we can cut this keyword attribute of color and then pass that as a separate keyword argument as
[15:21] that as a separate keyword argument as well as the line style dictionary after unpacking it.
[15:23] So let's pass in a color then pick a color.
[15:27] I'll keep the first line to be blue.
[15:29] For the second line I'll pick a different color.
[15:32] Let's go with something green.
[15:34] That's good.
[15:37] We'll pass in a different color for the second line.
[15:40] Then for the third line, I'll pass in a color that's red.
[15:46] That's good.
[15:51] And now each of our lines has a different color.
[15:58] All right, everybody.
[16:00] So that is some basic plot customization.
[16:02] In the next video, we'll discuss labels.
[16:03] To help increase engagement with this video, comment down below what your favorite food is.
[16:05] And well everybody, that is an introduction to plot customization using Mattplot LIIB.
[16:07] And well everybody, that is an introduction to plot customization using Mattplot LIIB.
[16:09] And well everybody, that is an introduction to plot customization using Mattplot LIIB.
[16:11] And well everybody, that is an introduction to plot customization using Mattplot LIIB.
[16:13] And well everybody, that is an introduction to plot customization using Mattplot LIIB.
[16:15] And well everybody, that is an introduction to plot customization using Mattplot LIIB.
[16:22] All right, everybody.
[16:24] In this next section, we're going to customize various labels, our title, and the ticks of our chart.
[16:30] Here's how we can customize those.
[16:32] In this demonstration, I have various class sizes yearbyear, the years 2023 through 2026.
[16:38] These are all numpy arrays, but you can use Python lists, too.
[16:43] To set a title, we're going to access plt.
[16:46] Call the title function, and then we can pass in a title as a string.
[16:51] We'll set the title to be class size.
[16:55] Then we will run this, and we should have a title.
[17:00] class size. Although it's kind of small and hard to read.
[17:02] Here's how we can customize it further.
[17:04] We can pass in some keyword arguments.
[17:07] One of which is font size.
[17:09] To change the font size, I'll set that to be 20.
[17:15] Here's the new title.
[17:17] You can also change the font family, and it's just family.
[17:20] I'll pick Ariel.
[17:20] That's a
[17:24] family.
[17:24] I'll pick Ariel.
[17:24] That's a professional looking font.
[17:27] professional looking font.
[17:27] You can set the font weight to be bold.
[17:31] You can set the font weight to be bold.
[17:31] Font weight equals the word bold.
[17:38] Let me make the title a little bit
[17:39] Let me make the title a little bit bigger.
[17:39] Let's say 25.
[17:43] bigger.
[17:43] Let's say 25.
[17:46] That's better.
[17:46] Let's also set a color for our title.
[17:49] Let's also set a color for our title.
[17:49] Color equals.
[17:49] Here you can pick the name
[17:52] of a color.
[17:52] You can use RGB values.
[17:52] You
[17:56] can use RGB values.
[17:56] You can use hexodimal values.
[17:58] can use hexodimal values.
[17:58] I'll pick something kind of dark blue
[18:02] like that.
[18:02] I'll copy the hex value and
[18:07] then set the keyword argument of color
[18:08] to be that hexodimal value.
[18:08] And now our
[18:11] title should be a specific shade of
[18:13] blue.
[18:13] Now we'll set a label for the x-axis.
[18:15] Now we'll set a label for the x-axis.
[18:19] We will access plt call the function of
[18:22] x label.
[18:25] x label.
[18:25] We'll set the label to be year.
[18:30] And we should have a label on the x-axis.
[18:33] And we should have a label on the x-axis for year.
[18:33] Although it's kind of small.
[18:35] for year.
[18:35] Although it's kind of small.
[18:35] We'll change the font size.
[18:37] We'll change the font size.
[18:37] We'll copy the font size from our title,
[18:40] We'll copy the font size from our title, but set it to be 20.
[18:44] Here's the new font size.
[18:48] Let's change the font family.
[18:50] Let's change the font family.
[18:50] I'll pick Ariel again.
[18:54] I'll set the font weight to be bold.
[19:00] And then I'll pick a color.
[19:03] And then I'll pick a color.
[19:03] I'll pick a lighter shade of blue.
[19:07] I'll pick a lighter shade of blue.
[19:07] Let's try that.
[19:10] Color equals
[19:13] Color equals that specific shade of blue.
[19:16] that specific shade of blue.
[19:16] Yeah, not bad.
[19:19] Yeah, not bad.
[19:19] The label is kind of close to the border.
[19:21] We'll be adjusting the margin a little bit later in this topic.
[19:23] To create the label on the Y-axis, I'm just going to copy
[19:27] Y-axis, I'm just going to copy everything within this X label function.
[19:30] Everything within this X label function. Paste it. Change X to Y.
[19:33] Paste it. Change X to Y.
[19:36] We'll say the text will be let's change the text of the label to be students as in number of students.
[19:41] In number of students. We should have a similar label on the Y axis.
[19:46] Ais. Now we're going to customize the ticks of our graph.
[19:47] Now we're going to customize the ticks of our graph. Each of these ticks is incrementing by.5 each.
[19:53] Incrementing by.5 each. You can kind of see that here.
[19:55] You can kind of see that here. You can force ticks to only display at these values for the x-axis.
[19:58] You can force ticks to only display at these values for the x-axis. Here's how.
[20:03] Here's how. We'll access plt. Call the x tick function.
[20:07] We'll access plt. Call the x tick function.
[20:09] Function. X tick.
[20:11] X tick. Then pass in our list or numpy array that we're using for the x-axis.
[20:13] Then pass in our list or numpy array that we're using for the x-axis.
[20:16] That we're using for the x-axis. So now we have ticks only at each given year 2023, 2024, 2025, 2026.
[20:19] So now we have ticks only at each given year 2023, 2024, 2025, 2026.
[20:25] Now to customize the ticks, we will
[20:28] Now to customize the ticks, we will access PLT.
[20:30] Access PLT call the tick params function, meaning call the tick params function, meaning tick parameters.
[20:34] Tick parameters.
[20:37] To select an axis, you'll select the keyword argument of access.
[20:39] You can select X, you can select Y, or you can select both.
[20:41] We'll select both.
[20:44] We're going to select colors plural, not singular.
[20:48] I'll select the same color as my labels.
[20:53] Now, the ticks should be blue.
[20:58] All right, everybody.
[21:01] So, that is how to add various labels to your plots in Mattplot Lib.
[21:02] Hey everybody, I have a super quick video today on how to create grid lines using the grid function using Mattplot LIIB.
[21:04] The grid function creates grid lines.
[21:12] They help make plots easier to read by adding reference lines.
[21:13] We'll need some sample data to work with.
[21:15] We'll just make something up.
[21:18] On the
[21:29] We'll just make something up.
[21:29] On the xaxis, we'll have 1 2 3 4 5.
[21:35] On the xaxis, we'll have 1 2 3 4 5.
[21:35] On the y-axis, we'll have y-axis, we'll have 5 10 15 20 25.
[21:38] It really doesn't matter.
[21:42] 5 10 15 20 25.
[21:42] It really doesn't matter.
[21:42] We're more focused on the grid lines We're more focused on the grid lines today.
[21:44] We'll need a plot access plt.
[21:47] today.
[21:47] We'll need a plot access plt.
[21:51] Call the plot function.
[21:51] Pass in x.
[21:51] Pass in y.
[21:55] Then access plot.
[21:55] Call the show function to show our plot.
[21:59] Here's our sample plot.
[21:59] Now, let's say you would like to create some grid you would like to create some grid lines.
[22:02] For better readability, here's how.
[22:03] lines.
[22:06] For better readability, here's how.
[22:08] how.
[22:08] We're going to access plt call the grid function.
[22:11] function.
[22:11] Let's see what happens if we don't pass in any arguments.
[22:13] Let's see what happens if we don't pass in any arguments.
[22:14] These are the grid lines with no customization.
[22:17] We have grid lines on the x-axis and the y-axis.
[22:19] customization.
[22:19] We have grid lines on the x-axis and the y-axis.
[22:21] If you would just like a single axis, you can specify that with the keyword argument axis equals.
[22:25] If you would just like a single axis, you can specify that with the keyword argument axis equals.
[22:27] you can specify that with the keyword argument axis equals.
[22:28] argument axis equals.
[22:32] argument axis equals.
[22:32] We'll pick the x-axis to begin with.
[22:36] We'll pick the x-axis to begin with.
[22:36] We will only have vertical lines then on the x-axis by setting the axis to x.
[22:42] If we were to set this to y,
[22:44] we would have grid lines on the y-axis.
[22:48] You can change the line width.
[22:51] Line width equals.
[22:51] Normally, it's one, but here's two.
[22:57] The lines are a little bit thicker.
[22:59] Here's three.
[23:02] Let's set that back to two.
[23:05] You can change the color.
[23:08] Color equals, then select a color.
[23:08] I'll just pick light gray to keep it simple.
[23:17] The lines are now light gray.
[23:21] You can specify a line style.
[23:23] Normally, it's solid.
[23:26] Let's try dashed.
[23:29] Here's dashed.
[23:32] Here's dashed.
[23:32] Here's dotted.
[23:35] Here's dotted.
[23:35] Here's dash dot.
[23:41] But let's set this back to dashed.
[23:46] All right, everybody.
[23:48] That is a super quick lesson on how to create grid lines using mattplot lib.
[23:58] All right, everybody.
[24:00] For this topic, we're going to create a bar chart.
[24:02] A bar chart compares categories of data by representing each category with a bar.
[24:07] Here's an example that I thought of.
[24:09] You know the food pyramid?
[24:11] We'll create a bar chart to represent daily consumption of categories of foods.
[24:16] Here are the different categories.
[24:17] This will be a Python list.
[24:22] Categories equals.
[24:24] We'll create a Python list.
[24:26] The first element within this list will be grains such as bread.
[24:30] Then fruit,
[24:33] Then fruit, vegetables, protein, dairy, dairy, and sweets.
[24:48] We'll create another Python list of values for each of these categories.
[24:55] What value should each of them have?
[24:56] Let's pretend that these values are for daily servings.
[25:00] How many in each category did I consume today?
[25:05] We'll make up some numbers.
[25:05] For grains, this will be four.
[25:09] Fruit, three.
[25:09] Vegetables, I don't eat very many vegetables, that will be two.
[25:12] Lots of protein, that's a five.
[25:15] Dairy, three.
[25:15] And sweets, one.
[25:18] But feel free to make up your own numbers.
[25:21] Now, although it's not necessary, I'm going to convert these Python lists to be numpy arrays.
[25:31] All right, we are ready to make a bar
[25:33] All right, we are ready to make a bar chart.
[25:33] Categories will be on the xaxis.
[25:36] chart. Categories will be on the xaxis, values will be on the y-axis.
[25:39] Rather values will be on the y-axis.
[25:39] Rather than using a plot such as plt called the plot function, we're instead going to use bar to make a bar chart.
[25:47] use bar to make a bar chart.
[25:49] First pass in what's going to be used for the x-axis categories, comma, then the y-axis of values.
[25:56] the y-axis of values.
[25:56] And then you do have to show your bar chart.
[25:57] And then you do have to show your bar chart.
[26:03] chart. plt call the show function.
[26:03] And let's see what we have.
[26:07] Here we go.
[26:07] A bar chart compares categories of data by representing each category with a bar.
[26:12] by representing each category with a bar.
[26:14] Here are the categories that we've stated.
[26:17] stated. grains, fruit, vegetables, protein, dairy, sweets.
[26:19] And here are the values on the lefth hand side for each.
[26:23] values on the lefth hand side for each.
[26:23] Let's add a title and two labels to our bar chart.
[26:27] bar chart.
[26:27] PLT call the title function, then pass in a title as a string.
[26:30] PLT call the title function, then pass in a title as a string.
[26:30] We'll
[26:33] then pass in a title as a string.
[26:33] We'll say daily consumption.
[26:37] Let's make sure that works.
[26:40] Yes, it does.
[26:42] We'll add a label to the x-axis.
[26:45] We'll add a label to the x-axis.
[26:46] plt x label.
[26:49] x label.
[26:49] We'll say the word food.
[26:53] Yep, it says food.
[26:53] And you can change the size if you want for these labels,
[26:58] but that might be overkill for this lesson.
[27:01] We've already covered that in the previous topic.
[27:03] PLT call the Y label function.
[27:09] We'll pass in a string that says quantity
[27:14] and it should say that on the lefth hand side.
[27:15] Now let's change the color of our bar chart.
[27:18] When we call the bar function, we can set the color to be a color of your choice.
[27:21] You can pick a color name.
[27:23] You can use hexodimal values.
[27:25] I'll just say sky blue.
[27:27] And the bar colors should change.
[27:34] And the bar colors should change.
[27:36] Now, if you would like a horizontal bar chart, I'll comment this line of code out.
[27:43] Copy it, paste it, you're going to instead call the bar H function for a horizontal bar chart, which looks a little something like this.
[27:51] But honestly, for me, I prefer the vertical bar chart.
[27:57] All right, everybody. So that is how to create a basic bar chart using mattplot lib.
[28:08] All right everybody we are now going to create a bar chart using mattplot liib.
[28:13] A bar chart it's a circular chart divided into slices to show percentages of the total.
[28:18] It's good for visualizing distribution among categories.
[28:24] In this demonstration, we're going to create a fictional college.
[28:26] There will be four classes, freshman, sophomore, junior, senior.
[28:30] We'll create a Python list of categories.
[28:36] We'll have freshman, sophomores, I think I misspelled that, juniors, and seniors.
[28:57] We'll also create a Python list of the number of students in each class.
[29:02] Values equals a Python list. We'll have 300 freshmen in our college, 250 sophomores, 275 juniors, and 225 seniors.
[29:21] If you're familiar with numpy, you do have the option to convert any numerical Python lists to numpy arrays.
[29:26] Although for a list of four values, it's not really too beneficial.
[29:31] If this were a larger data set, we would benefit from numpy arrays more, but it would be a good habit for us to develop.
[29:37] good habit for us to develop.
[29:39] Now, Python list of strings don't really benefit from numpy arrays.
[29:41] We'll keep that as is.
[29:44] We have the two collections that we need, categories and values.
[29:47] To create a pie chart, we will access plt,
[29:50] call the pi function,
[29:52] pass in our values.
[29:55] Let me show you what just passing in values will look like.
[29:57] We will also call the show function.
[30:00] plt.show function.
[30:03] We do have the distributions, but we don't have any labels.
[30:07] Here's how to set the labels.
[30:10] When we call the pi function, we will pass in a few keyword arguments.
[30:14] The first will be labels.
[30:16] What are the labels of our pie chart?
[30:19] The labels will be the categories that we have.
[30:22] Labels equals categories.
[30:24] And we should have some categories for each of these pie slices.
[30:29] Freshman, sophomores, juniors, seniors.
[30:32] You can add some percentages to each of these pie slices.
[30:36] Here's how.
[30:36] That's going to be another keyword argument.
[30:39] going to be another keyword argument.
[30:44] The keyword argument is auto PCT.
[30:47] This means auto percentage.
[30:48] We have to set this equal to a formatting instruction.
[30:50] We're going to use percent one f.
[30:55] Let's see what we have.
[31:00] All right. We don't need all these digits after the decimal.
[31:02] Let's limit that.
[31:05] After one, we'll add 0.1 to display one digit after the decimal.
[31:12] 28.6 23.8 8 26.2 21.4.
[31:17] To add a percent sign after, we'll use double percent signs cuz normally using one percent sign is a format specifier.
[31:26] Okay, now there's a percent sign after each of these numbers.
[31:29] Here's how to set the colors for each pie slice.
[31:33] We'll create another list for convenience, which I will name colors.
[31:35] Colors equals.
[31:38] Then in order of your categories,
[31:40] categories, place a color within here.
[31:42] You can place a color within here.
[31:46] You can choose a color name, RGB values, choose a color name, RGB values, hexodimal values.
[31:48] We'll just keep it simple and pick some color names.
[31:50] simple and pick some color names.
[31:53] Freshman will be red, sophomores will be yellow,
[31:55] yellow, juniors will be blue,
[31:58] juniors will be blue, seniors will be green.
[32:01] seniors will be green.
[32:02] And then we do have to pass that in as a keyword argument.
[32:04] keyword argument.
[32:09] Set colors equal to our list of colors.
[32:11] And the colors should change.
[32:11] Freshman are red, sophomores are yellow, juniors are red, sophomores are yellow, juniors are blue, seniors are green.
[32:18] are blue, seniors are green.
[32:21] Let's add a title to our pie chart.
[32:23] PLT called the title function. Pass in a title.
[32:27] My college will be named Broode College because I think I'm cool.
[32:32] College because I think I'm cool.
[32:35] You can explode one of these pie slices.
[32:38] Explode means to displace it slightly.
[32:38] If you need to highlight a pie slice,
[32:40] If you need to highlight a pie slice, we'll explode seniors.
[32:45] For the keyword argument of explode, we'll set this equal to a Python list.
[32:52] We have four categories. We'll need four values.
[32:54] I'll set each of these to be zero.
[32:56] I need to explode seniors. I'll set this to a value that's not one.
[32:58] Let's start with 0.1.
[33:00] The greater the number, the further the distance that pi slice will explode.
[33:08] Here's 0.1.
[33:11] Let's change that to 0.2.
[33:13] The distance traveled should be a little bit greater.
[33:16] Here's.3.
[33:17] We'll set that to be 02.
[33:21] That's pretty good.
[33:23] And 0.1.
[33:26] We can also add a drop shadow.
[33:29] shadow equals true.
[33:33] And there should be a drop shadow for each pie slice.
[33:37] To rotate your pie
[33:41] each pie slice.
[33:46] To rotate your pie chart, you'll set start angle to be some angle.
[33:49] If I were to set this to be 180 for 180Β°, my pie chart is going to rotate by 180Β°.
[33:56] I'll just set that to be let's check out 90.
[34:00] That's pretty good.
[34:02] All right, everybody.
[34:04] So that is how to create a basic pie chart using mattplot liib.
[34:12] All right everybody in this video we're going to discuss scatter graphs in mattplot lib.
[34:16] Scatter graphs show the relationship between two variables.
[34:19] They help to identify a correlation positive negative or no correlation.
[34:21] For example the more hours you study the higher your test scores tend to be.
[34:24] That's a positive correlation.
[34:27] Scatters help us identify those correlations.
[34:30] In this demonstration, I'll create the following data set.
[34:32] You can either follow along and type what I'm about to type or look
[34:42] and type what I'm about to type or look in the comment section.
[34:44] I'll try and post this data set.
[34:47] We'll have X, which will be a Python list.
[34:50] These will be hours studied for various students.
[34:52] We'll have 10 values.
[34:55] The first student didn't study at all.
[34:57] They studied zero hours.
[34:57] Another studied 1 hour.
[35:04] 1 2 3 4 5 6 7 7 8.
[35:09] Feel free to make up some numbers too.
[35:11] It really doesn't make a difference.
[35:14] Then y will be the test scores.
[35:15] This will be another Python list.
[35:18] The student that didn't study at all, they got a 55% on their test score.
[35:21] The first value will be 55.
[35:24] Then another got 60.
[35:24] 65.
[35:28] 62.
[35:30] 68
[35:32] 70
[35:34] 75
[35:36] 78
[35:38] 82
[35:40] 85
[35:40] 87.
[35:40] That's good enough for now.
[35:44] 87. That's good enough for now.
[35:45] To create a scatter graph, we're going to access PLT.
[35:48] Call the scatter function.
[35:50] Pass in X.
[35:55] Then pass in Y.
[35:59] X is for the hours studied.
[36:03] Y is for the grades.
[36:04] What grade did they get on their test score?
[36:06] Then in order to show this graph, we have to call the show function.
[36:10] plt.show function.
[36:14] Let's see what we have.
[36:17] Here is our scatter graph.
[36:19] You can see that there's a positive correlation between our studied and the exam scores.
[36:22] Let's add some labels to our scatter graph.
[36:27] For the X label access plt, call the X label function.
[36:33] Then pass in a string to serve as the label.
[36:36] Hours studied is good.
[36:37] Let's make sure that that works.
[36:40] Yes, it does.
[36:43] We have a label for hours studied.
[36:45] For the y-axis, call the y label function.
[36:48] Then pass in a string.
[36:52] We'll say grades or grade singular.
[36:56] Let's make sure that that works.
[36:59] Yes, it does. Here's the grade.
[37:02] Then let's add a title plt.
[37:05] Call the title function.
[37:07] I'm going to say test scores just as a general title for our scatter graph.
[37:11] Let's add a little bit of customization.
[37:15] Let's change the color.
[37:17] This will be a keyword argument.
[37:20] color equals some color.
[37:23] You can pick a color name.
[37:25] You can use RGB values.
[37:28] I'll just say sky blue to keep it simple.
[37:31] And the color should change, which it does.
[37:34] You can also change the alpha.
[37:36] Alpha is related to the transparency.
[37:39] If I set alpha to be 0.5, that's 50%.
[37:42] The transparency is going to change, although it's a little hard to see.
[37:44] If I
[37:46] although it's a little hard to see.
[37:48] If I set alpha to be 0.1, then the transparency is 10% and you can barely see it.
[37:51] I'll keep it at 50%.
[37:54] Let's change the size of our dots on our scatter graph.
[37:56] That would be S for size.
[37:59] Let's check out 100.
[38:02] That's bigger.
[38:07] Here's 200.
[38:09] Yeah, we can keep it at 200.
[38:12] That's a good size.
[38:14] Okay, let's create another data set.
[38:16] The following set of scores will be for one class.
[38:19] We'll add another class of scores.
[38:21] Although it's not necessary, I forgot to make these numpy arrays.
[38:23] You can if you would like.
[38:25] Let's rename x to be x1, y to be y1.
[38:26] Pass in x1, and y1.
[38:33] We're going to create a whole another set.
[38:37] Let's copy and paste x1 and y1.
[38:39] Rename them as X2 and Y2.
[38:48] And Y2. And let's change some of these values.
[38:50] And let's change some of these values around.
[38:53] For the second set, we'll have 0 1 2 2 3 4 5 6 7 8.
[39:02] Then for the exam scores for the second class, we'll have 50 58.
[39:09] 65 70 72.
[39:11] 70 72 78.
[39:14] 78 83.
[39:15] 83 88.
[39:17] 88 92.
[39:19] 92 95.
[39:21] 95 97.
[39:22] 97.
[39:25] We're going to call the scatter function.
[39:26] We're going to call the scatter function again.
[39:28] Again.
[39:30] We'll copy and paste what we had previously, but pass in X2 and Y2, the second set of coordinates.
[39:34] Second set of coordinates.
[39:36] Let's change the color to be red.
[39:41] Let's change the color to be red.
[39:44] The blue set of dots is for class one.
[39:47] The red set of dots is for class 2.
[39:48] Based on this graph, we can't tell without a legend.
[39:51] The legend will tell you which class is which.
[39:55] But we'll need some labels.
[39:56] We'll pass those in as keyword arguments.
[39:59] For class one, we'll set label equal to class A.
[40:06] And then for our second class, this will be class B.
[40:10] Label equals class B.
[40:14] But then we have to show the legend because it's not visible.
[40:21] Access PLT.
[40:21] Call the legend function.
[40:28] Then we should have a legend within our scatter graph.
[40:31] Class A is the blue dots.
[40:34] Class B is the red dots.
[40:34] All right, everybody.
[40:36] So those are scatter graphs.
[40:38] They show relationships between two variables.
[40:41] For example, hours studied and exam grades.
[40:44] The more hours you study, the better grade you tend to get.
[40:46] That's a positive correlation, which we
[40:49] That's a positive correlation, which we can see from this graph here.
[40:51] And well everybody those are scatter graphs using mattplot liib.
[40:59] Yo hey everybody in this video I'm going to explain histograms in mattplot lib.
[41:03] Histograms are a visual representation of the distribution of quantitative data.
[41:09] In short we can visualize the distribution of data.
[41:11] Histograms group values into bins.
[41:14] Think of these like intervals.
[41:17] will count how many fall in each range in each bucket or bin.
[41:20] Histograms are great for statistics and probability.
[41:22] In many cases, you'll see a bell curve distribution.
[41:25] Now, in this demonstration, I recommend using NumPy cuz we'll need a lot of data to work with.
[41:29] With NumPy, we can generate some random numbers.
[41:32] Here's how we can do that.
[41:33] We're going to create some test scores.
[41:35] We're a teacher and we have 100 students that each took a test.
[41:37] We'll create a visual to display the distribution of scores.
[41:39] We'll create a numpy array of scores.
[41:42] Scores equals np
[41:53] numpy array of scores.
[41:56] Scores equals np accessis random. Then call the normal function for a normal distribution.
[41:59] There's a few keyword arguments here we'll pass in.
[42:01] The first is loc.
[42:06] LOC equals loc is the location of the center, the median of our data.
[42:09] For the center, we'll say that the median is 80.
[42:11] 80%. All of the test scores are going to cluster around this score of 80.
[42:14] Most students will do pretty well.
[42:17] If this were a lower score like 50, that means most of the students failed.
[42:19] That's because the most frequent score is going to be around 50%.
[42:21] We'll set that to be 80.
[42:23] Now, we'll need a scale.
[42:26] This is another keyword argument.
[42:27] Scale equals scale is the standard deviation, the spread.
[42:30] How far away are the scores going to deviate from the center?
[42:32] The higher the number, the greater the deviation.
[42:36] Let's start with 10.
[42:38] And then we need a size.
[42:40] How many numbers do we want to generate?
[42:43] We'll set size equal to 100.
[42:45] We have 100 test scores.
[42:48] Let's
[42:57] to 100. We have 100 test scores.
[42:59] Let's test what we have currently. We'll test what we have currently.
[43:03] We'll access plt. Call the hist function for a histogram.
[43:06] Pass in our numpy array of scores.
[43:09] Then we do have to call the show function to show our histogram.
[43:12] access plt called call the show function.
[43:16] Here's what a histogram looks like.
[43:18] We set the center to be 80.
[43:21] Scores around that number are going to be the most frequent.
[43:22] And we've set the scale to be 10.
[43:25] 10 is the standard deviation.
[43:28] That's a statistics and probability term.
[43:30] How far away will the scores deviate from the center?
[43:32] Now, if I set the scale to be a small number like one,
[43:35] the scores are going to cluster more closely to 80.
[43:39] The lowest score was 77.
[43:41] The highest score was 82.
[43:44] If I set scale to be a big number, like 50.
[43:47] Well, then the scores really deviate from the center of 80.
[43:49] We have some students that scored below zero and
[43:59] students that scored below zero and others above 200.
[44:02] others above 200.
[44:02] Let's set that back to 10.
[44:07] Just to ensure that no scores go below
[44:09] Just to ensure that no scores go below zero or above 100, we can use the clip
[44:13] zero or above 100, we can use the clip function.
[44:15] function.
[44:15] We'll reassign our numpy array of
[44:17] We'll reassign our numpy array of scores.
[44:17] Access numpy.
[44:17] Call the clip
[44:21] scores.
[44:21] Access numpy.
[44:21] Call the clip function.
[44:24] function.
[44:24] We have to pass in our numpy array of
[44:27] We have to pass in our numpy array of scores.
[44:28] scores.
[44:28] For any scores below zero, we'll set
[44:30] For any scores below zero, we'll set them to be zero.
[44:30] We'll clip them.
[44:30] For
[44:33] them to be zero.
[44:33] We'll clip them.
[44:33] For any scores above 100, we'll clip them
[44:36] any scores above 100, we'll clip them down to 100.
[44:39] down to 100.
[44:39] It's kind of like we're setting the
[44:40] It's kind of like we're setting the bounds, the minimum and the maximum.
[44:47] All scores below zero will be set to
[44:49] All scores below zero will be set to zero.
[44:49] All scores above 100 will be set
[44:51] zero.
[44:51] All scores above 100 will be set to 100.
[44:53] to 100.
[44:53] Let's set the scale back to something
[44:55] Let's set the scale back to something massive like 50.
[44:58] massive like 50.
[44:58] Yep, there's no scores below zero and no
[45:00] Yep, there's no scores below zero and no scores above 100.
[45:03] scores above 100.
[45:03] But we don't need a scale that big.
[45:07] But we don't need a scale that big.
[45:07] Let's add some customization.
[45:09] You can set the number of bins that you want as another keyboard argument.
[45:11] This will be within the hist function for a histogram.
[45:17] I'll set the number of bins to be 10.
[45:22] We should now have 10 bins or intervals.
[45:25] 1 2 3 4 5 6 7 8 9 10.
[45:30] If I were to set this to 20, we would now have 20.
[45:33] But I think 10 is a good number.
[45:38] Let's change the color.
[45:41] That's another keyword argument.
[45:44] Color equals pick a color.
[45:47] I'll just set my histogram to be light green.
[45:52] There we go.
[45:52] To better define the edges, you can set an edge color because right now it kind of looks like one massive blob.
[46:02] Edge color equals I'll set it to be black.
[46:08] We should get a nice outline around each interval or bin.
[46:11] Let's add some labels because well, I forgot to do that.
[46:16] Let's add a title.
[46:18] plt called the title function.
[46:20] We'll say exam scores.
[46:25] Let's make sure that that works.
[46:27] Yes, it does.
[46:31] Let's set a label for the x-axis.
[46:35] plt call the x label function.
[46:37] We'll say score.
[46:41] Here we go. Score.
[46:46] And for the y-axis, plt call the y label function.
[46:51] Number of students.
[46:54] Number of students is on the y- axis.
[46:55] All right, everybody.
[46:59] So, that's a histogram.
[47:01] It's a visual representation of the distribution of quantitative data.
[47:04] data.
[47:04] We're displaying the distribution of data.
[47:07] And well, everybody, those are histograms in Mattplot Lib.
[47:13] Yo.
[47:15] Hey everybody.
[47:15] In this video, I'm going to show you how we can create subplots using Mattplot Lib.
[47:19] There's a couple definitions we'll need to cover.
[47:21] One of which is a figure.
[47:24] A figure is the entire canvas in which we add a plot to.
[47:27] Then we have an ax.
[47:29] An ax is also known as a subplot.
[47:32] It's a single plot within a figure.
[47:35] A figure can contain multiple axes or subplots.
[47:38] We're going to call a subplots function.
[47:40] It's going to return a tpple that has a few things inside of it.
[47:45] Just to demonstrate temporarily, I'm going to print access plt call the subplots function.
[47:53] Within the subplots function, we're going to determine the layout of our figure.
[47:57] How many rows and columns of subplots will be within our figure?
[48:00] For example, we could have a 2x two, two
[48:06] example, we could have a 2x two, two rows, two columns.
[48:06] Or 2x3, two rows, three columns, or a 3x two, three rows, two columns.
[48:16] two columns.
[48:16] Let's stick with the 2x two for now.
[48:19] I want to show you what this function returns exactly.
[48:22] It's a little complex, but we'll break it down.
[48:27] The subplots function, it will return a tpple.
[48:30] The tpple has two entities, a figure object, which is our canvas, and a two-dimensional array of axes objects.
[48:37] When we call this function, we're going to unpack this tupler right away.
[48:43] Rather than use print, we're going to assign what's returned to us and unpack it.
[48:49] We'll create a figure object and an axis object.
[48:55] figure and axes equals call the subplots function.
[49:06] Figure is our canvas.
[49:08] Figure is our canvas.
[49:10] We won't actually be using it in this topic, but we're provided with it by the subplots function.
[49:12] We'll print this two-dimensional list right away.
[49:14] Here's how we'll plot each of these subplots, but we won't add any data quite yet.
[49:16] Not until later.
[49:19] If I was to call the show function, we can see all the subplots that we have.
[49:22] access plt.
[49:24] Call the show function.
[49:26] We have four subplots, two rows, two columns.
[49:29] If this were a 1x two, one row, two columns, we have two subplots.
[49:32] Or a 2x 1, which would be two rows, one column.
[49:35] A 3x3 would be three rows, three columns.
[49:37] We don't need that many subplots, but I think you get the idea.
[49:40] We'll have a 2x two.
[49:43] Two rows, two columns.
[49:46] We'll fill each of these individual subplots in.
[49:48] Here's how.
[49:50] We'll start with the top left corner.
[50:06] We'll start with the top left corner.
[50:06] Row 0, column 0.
[50:09] Row 0, column 0.
[50:09] We'll access our 2D list of axes at row 0, column 0, and then plot it calling the plot function.
[50:19] We will need some data.
[50:19] We'll keep it simple.
[50:24] We'll say x equals a numpy array with a list that contains 1 2 3 4 5.
[50:34] We'll plot x.
[50:34] But to keep it simple, we'll just say y will be x * 2.
[50:44] Let's take a look.
[50:48] We've selected the subplot at row 0, column 0.
[50:53] So only this one is filled in with our data.
[50:56] Let's customize it.
[50:56] Let's set the color to be red with a keyword argument.
[51:04] The color is now red.
[51:06] The color is now red.
[51:06] And let's add a title.
[51:09] And let's add a title.
[51:09] We need to select that specific subplot.
[51:11] We need to select that specific subplot using a set of indices.
[51:13] using a set of indices axes at row 0, column 0.
[51:18] Call the set title function.
[51:18] Then pass it a title.
[51:21] title function.
[51:21] Then pass it a title.
[51:21] We'll say x 2 for the title.
[51:25] We'll say x 2 for the title.
[51:25] And there's the title for this specific subplot.
[51:27] And there's the title for this specific subplot.
[51:29] subplot.
[51:29] Now we'll plot the top right subplot.
[51:33] Now we'll plot the top right subplot.
[51:33] We need to select that specific one.
[51:36] We need to select that specific one.
[51:36] That would be row 0, column 1.
[51:39] We'll call the plot function.
[51:39] Pass in x.
[51:44] call the plot function.
[51:44] Pass in x.
[51:44] We'll say x to the^ of two for the y-axis.
[51:46] We'll say x to the^ of two for the y-axis.
[51:48] y-axis.
[51:48] Select a color.
[51:48] I'll select blue.
[51:53] Select a color.
[51:53] I'll select blue.
[51:53] Then set the title axis at row 0 column 1.
[51:56] Then set the title axis at row 0 column 1.
[51:56] We will set the title to be x to the^ of two.
[52:01] at row 0 column 1.
[52:01] We will set the title to be x to the^ of two.
[52:04] to be x to the^ of two.
[52:07] x to the of two.
[52:09] The top right subplot should now be plotted.
[52:11] All right. Let's select the bottom left corner that will have different indices.
[52:17] axes at row one, column zero. We'll call the plot function.
[52:22] Pass in x. For y, we'll say x to the power of three.
[52:25] We'll set the color to be green.
[52:33] Then set a title axis at indices of 1, 0.
[52:38] Call the set title function.
[52:42] This will be x to the of three.
[52:50] Oops, looks like I misspelled color.
[52:53] There we go.
[52:55] Some of these subplots are overlapping.
[52:59] You can add a tight layout so everything fits.
[53:02] Access plt. Call the tight layout function. And now they should all fit in
[53:09] function.
[53:10] And now they should all fit in place, place, which they do.
[53:13] Let's finish this bottom right corner.
[53:15] I'll just copy and paste the last two lines.
[53:18] That would be axes at row one, column 1.
[53:22] This will be x ^ 4.
[53:26] The color will be purple.
[53:30] Axes at row 1, column 1.
[53:33] We'll set the title to be x to the^ of 4.
[53:36] There we go.
[53:39] We have now filled in the bottom right corner.
[53:44] Now, if this were a 3x3, well, then only these four subplots are still filled in, but we really don't need that many.
[53:50] Now, you don't necessarily need to use plots.
[53:53] You could use different graphs instead such as a bar chart or whatever else you would like.
[54:00] All right, everybody.
[54:02] And that is how to create subplots using mattplot lib.
[54:09] Yo.
[54:09] Hey everybody.
[54:11] In this video, we're going to integrate both pandas and mattplot lib.
[54:16] In my panda series, we've been working with the CSV file that contains the stats for the original 150 PokΓ©mon.
[54:24] We'll be reusing that in this series.
[54:26] Or if there's another set of data that you would like to work with, feel free to use that, too.
[54:29] I'll post this in the comment section on YouTube and you can just copy and paste it if you would like.
[54:34] Just be sure to put it in a CSV file.
[54:37] To work with pandas, we'll need to import it.
[54:37] Import pandas.
[54:41] We'll give it a nickname as pd for short.
[54:47] When we read a CSV file, we're going to assign it to be a dataf frame, which we will name df.
[54:53] df equals access pandas as pd.
[54:58] call the read CSV function and then pass in the name of that file or a file path.
[55:04] The name of my file is data.csv.
[55:07] And it's right next to my main Python file.
[55:09] I don't need a file path beforehand.
[55:11] beforehand.
[55:13] Let's be sure that it works.
[55:13] I'm going to print my data frame.
[55:17] Here are all the stats of the original 150 PokΓ©mon.
[55:21] It's a truncated view though, but that's good enough.
[55:24] We'll create a bar chart that shows the quantity of the primary types.
[55:29] For example, there's grass types, there's fire types,
[55:34] dragon types, so on and so forth.
[55:37] We'll be selecting only a single column, a single series, type one.
[55:42] There is a secondary type of type two, but we won't be concerned with that in this video.
[55:47] To select a single column, we'll be using the subscript operator, which is a set of straight brackets.
[55:54] Then select the column.
[55:57] We will select type one.
[55:59] That is the primary type.
[56:02] We'll be returned with a single series, a single column.
[56:06] There is a built-in function of series.
[56:09] It's called value counts.
[56:09] And this is a function.
[56:13] counts. And this is a function.
[56:15] This function will return the number in each category.
[56:17] each category.
[56:20] So, if I were to run this, we would have a total of 28 water types, 22 normal types, 14 poison types, 12 fire types, and a whole bunch more.
[56:27] fire types, and a whole bunch more.
[56:31] We'll plot the quantity of each type.
[56:34] The index we'll put on the y-axis with a horizontal bar chart, and the values we'll place on the x-axis.
[56:37] horizontal bar chart, and the values we'll place on the x-axis.
[56:43] Now for convenience I'm going to assign our value counts.
[56:46] our value counts.
[56:48] We'll say type count equals this series.
[56:51] count equals this series.
[56:54] this series.
[56:57] Type count will be a series.
[56:59] It will hold the amount of each type.
[57:03] Now to create a bar chart, we'll access plt.
[57:05] plt.
[57:07] Call the bar function.
[57:10] We'll create a horizontal bar chart in just a moment.
[57:12] For the x-axis, we'll
[57:15] Just a moment.
[57:15] For the x-axis, we'll access our series of type count, but I would like just the index.
[57:22] We don't need the entire series.
[57:25] And for the y-axis, we'll need the values.
[57:31] Take our series of type count. Access the values.
[57:34] Let's see what we have currently.
[57:37] We'll do a test run.
[57:37] Be sure to show your plot because I forgot to do that.
[57:42] There we go.
[57:44] Here's our bar chart.
[57:46] But we have a little bit of work to do.
[57:48] All of these index names are grouped together.
[57:51] Let's place them on the x-axis with a horizontal bar chart.
[57:55] We'll use a horizontal bar chart with bar h.
[57:59] That's a lot better.
[58:02] Now they're in ascending order.
[58:04] I would like the values with the most on the top rather than the bottom.
[58:09] Well, when we create our type count series, we can pass in a keyword argument.
[58:14] Ascending equals true.
[58:17] Ascending equals true.
[58:17] And they should now be in reverse order.
[58:20] And they should now be in reverse order.
[58:21] You can change the colors if you would like.
[58:25] Pass in a keyword argument of color.
[58:28] Then pick a color.
[58:30] I'll use a hex color picker.
[58:32] I'll pick something blue.
[58:35] That's pretty decent.
[58:38] I'll copy the hex value and then paste it within that color keyword argument.
[58:41] color keyword argument.
[58:43] Not bad.
[58:47] I'll also add an edge color of black.
[58:54] Edge color equals black.
[58:56] Let's add a title and some labels.
[58:59] PLT call the title function.
[59:02] This title will be number of Pokemon by primary type.
[59:07] Let's make sure that it works.
[59:10] Yes, it does.
[59:12] For the x-axis, we'll say count.
[59:12] plt. Call the x label function.
[59:18] count. plt. Call the x label function.
[59:18] Pass in the word of count.
[59:23] There we are.
[59:23] Then for the y-axis axis.
[59:26] There we are.
[59:26] Then for the y-axis axis plt call the y label function.
[59:29] Pass in a plt call the y label function.
[59:29] Pass in a word of type.
[59:35] Just to make sure everything fits.
[59:37] We're going to call the tight layout function.
[59:39] so that everything fits.
[59:39] plt call the tight layout function.
[59:43] so that everything fits.
[59:43] plt call the tight.
[59:44] tight layout function.
[59:47] layout function.
[59:47] And now everything should fit within our figure.
[59:51] figure.
[59:51] All right everybody.
[59:51] So that served as a final project for us.
[59:52] final project for us.
[59:52] That's how to use both pandas and mapplot lib together.
[59:55] final project for us.
[59:55] That's how to use both pandas and mapplot lib together.

Cite this page

If you're using ChatGPT, Claude, Gemini, or another AI assistant, paste this URL into the chat:

https://youtube-transcript.ai/docs/learn-matplotlib-in-1-hour-vhhx55rqf7

The full transcript and summary on this page will be retrieved as context, so the assistant can answer questions about the video accurately.