• 20 hours ago
Turn a few lines of Python into clear, useful charts with Pandas. Using a sample ice cream ratings dataset, this hands-on tutorial demonstrates line plots, vertical and stacked bar charts, horizontal bars, scatter plots, histograms, box plots, area charts, and pie charts.

Learn how to choose chart types, select columns, set scatter-plot axes, adjust histogram bins, and customize titles, labels, figure size, and visual styles. A practical introduction to data visualization in Python for beginners learning Pandas, data analysis, and chart creation. Explore Pandas plotting examples and common chart types to visualize datasets, understand distributions, and build clearer data analysis workflows.
Transcript
00:00Hello, everybody. Today we're going to be building visualizations in pandas. In this video, we'll
00:04look at how we can build visualizations like line plots, scatter plots, bar charts, histograms,
00:10and more. I'll also show you some of the ways that you can customize these visualizations to
00:13make them just a little bit better. With that being said, let's go right over here, start
00:17importing our libraries, and we'll start with importing pandas as pd. And this one is really
00:23all you need to actually create the visualizations in pandas, but we may get a little bit crazy.
00:28And so we're going to do a few different ones as well, like import numpy as np. And then
00:35we're going to do import matplotlib.pyplot as plt. Now I may or may not use this. I just,
00:45you know, when I get into visualizations, I may want to change some different things.
00:48So we're going to at least have them here in case we do want to use them. Let's go ahead
00:52and run this. So now let's get our dataset that we're going to be using. So let's say
00:58data frame is equal to pd.read underscore CSV. And hit this in right here. Now we're going
01:06to be doing these ice cream ratings. Let's take a look at this really quickly. Now these
01:11values are completely randomly generated. They are not real in any way. But that's what
01:17we're going to be using. So I just wanted something kind of generic, something that
01:20wouldn't be too crazy confusing, just something that we could use. And you guys can understand
01:24that they're just numerical values. But let's also set that index really quick.
01:28So we'll say data frame dot set underscore index. And then we'll say date. And then we'll say
01:34that's equal to the data frame. And we have this date column right here as our index. So
01:40we have January 1st, 2nd, 3rd, 4th. And then we have our ratings right here. And again, these
01:45are all just integers. And they're pretty easy or really easy to demonstrate how you can visualize
01:50these. So that's why we're using it today. So the way that we visualize something in pandas
01:55is we use something called plot. Let's just take our data frame. We'll do data frame dot
02:00plot. And we'll do our parentheses. Now let's go in here really quickly. Let's hit shift tab.
02:06And this is going to come up. And this is pretty important. Because this kind of is going to tell
02:11us what we can do within this plot. And unfortunately, there isn't like a quick overview. We just have
02:17this doc string. But we have our parameters right here. These are what we can pass in to kind of
02:21customize our visualization. So the data is going to be our data frame. Then we have our x and y
02:28labels.
02:28We can specify the kind. And this one's important because we can specify what kind of visualization
02:34do we want. We can do a line plot, horizontal, a vertical bar plot, histogram, box plot, and then a
02:42few
02:42others including area, pi, density, all these other things. We can also specify if we want it to be
02:47a subplot. And a lot of these things that I'm specifying, you know, I'm going to show you how to
02:51do.
02:52You can use different indexes. You can add titles, add grids, legends, styles, all these different
02:58things. I mean, you can go through here because there are a lot. But you can specify and, you know,
03:03customize all of these things. We won't be going into all of them. But I will show you some of
03:08the ones
03:09that I probably use the most and that I think are the most useful to know right away. So let's
03:13get
03:13out of here. And we're just going to do df.plot. And when we run this, we'll get this right
03:18here.
03:18And that was super, super easy. We created a line plot by literally doing just about nothing.
03:24But by default, it's going to give us a line plot. So if we come up here,
03:30we say kind, and let me get that out of the way, is equal to line. And we run this.
03:36So by default, without us actually having to input anything, it's giving us that line plot
03:41as a default. So we can specify that it's a line plot. As you can see, we already have
03:46all of our data right here. We didn't have to specify anything. It kind of automatically took
03:50it in. It is visualizing all three of these columns. And it has this little legend right
03:57here. And we can specify where we want that. There is an argument to be able to do that.
04:01It also gave us these tick marks of 2, 4, 6, 8, 10. Again, it read in and said,
04:08it's only going from 0.0 to 1.0. That is kind of the peak. And so it kind of
04:14automatically gave
04:15us these ticks for us. Again, that's another thing that you can specify. We make it go up
04:19to 2, 5, 10, 1,000, whatever you want it to be. And then we're doing this based off of
04:24this date
04:24value right here. Really quickly, I wanted to give a huge shout out to the sponsor of this entire
04:28Pandas series. And that is Udemy. Udemy has some of the best courses at the best prices,
04:33and it is no exception when it comes to Pandas courses. If you want to master Pandas, this is
04:37the course that I would recommend. It's going to teach you just about everything you need to know
04:41about Pandas. So huge shout out to Udemy for sponsoring this Pandas series. And let's get back
04:44to the video. If we wanted to break these out by the actual column, we could go in here and
04:51say
04:52subplot is equal to true. And it's actually subplots. Whoops. And now we can run that.
04:59And then we can see each of those columns being broken out by themselves. Instead of them all being
05:03in one visualization, it's now three separate visualizations. Now let's go right over here.
05:09We're going to get rid of the subplots. I want to show you just some of the different arguments that
05:12you can use to make this look nice, because I don't want to do this on every single visualization.
05:17I just want to show you what you can do. So we have this one right here. We can add
05:22a title.
05:22Notice there's no title or anything really telling us what that is. So we can say comma,
05:27title, and we'll say ice cream ratings. If we run this, we now have this nice title right here.
05:35Now we can also customize the labels or the titles for the X and Y axis. It automatically took this
05:41date, which is right here. This is our date index. It automatically took that for us,
05:47but we can customize that if we'd like to. All we have to do is comma, and then we'll say
05:51X label is equal to. And so our X is this date one right here. And we can say daily
05:58rating.
06:00And then we can do the Y label. We'll say Y label is equal to. And for this one, we
06:06can say scores.
06:07I hope you cannot hear my dog in the background because they're being insane.
06:11But let's go ahead and run this. And now we have these daily ratings on the X axis. And on
06:16the Y
06:16axis, we have scores. Now let's go right down here and start taking a look at our next kind of
06:21visualization, which is going to be a bar plot. So we'll do df.plot. We'll do kind is equal to.
06:29And for this one, we're going to say bar. Now this is what your typical bar plot will look like.
06:34And a lot of the arguments that we just did on the line plot, you can also apply to this
06:38bar plot.
06:39Something that's unique to the bar plot is that you can also make it a stacked bar plot. All we
06:44have
06:44to do is go in here. We'll say comma, and we'll say stacked is equal to true. And now it's
06:50going to
06:50make it a stacked bar chart instead of just, you know, your regular bar chart. Let's go ahead and
06:55run this. And as you can see, this is now stacked on top of one another with each of these
06:59columns,
07:00all representing the values that they have. Now we don't always have to do every single
07:05column. We can also specify the column that we want. Let's take the flavor rating. For example,
07:10we could do flavor, oops, flavor rating, good night, flavor rating. And then it's only going to take in
07:20that flavor rating column. And if you notice, we don't have a legend. That's only when you have
07:25multiple values, which we are only looking at this one column. So all the values are right here.
07:30Now in this bar chart, it automatically defaults to a vertical bar chart, but you can change it to
07:35a horizontal bar chart. Let's go ahead and take a look at how to do that. Bring back all of
07:40them.
07:41We'll do df.plot dot, and then we'll say bar h. And I don't know if I can keep in
07:47that kind equals
07:48bar. Let me run this. Yeah, I need to get rid of that because the bar dot h is its
07:52own.
07:53This is its own function. So now I'm going to run this. It should just have a stacked bar chart,
07:59except now it should be horizontal. So now you can see this worked properly. It's basically the
08:05exact same thing as a vertical bar chart, just now horizontal, which may look better, especially
08:10depending on if you have values like this or, you know, something else that just looks better
08:15being horizontal. Now, the next one that we're going to take a look at is the scatter plot.
08:19So we're going to say df.plot.scatter. And if we run this, we're going to get an error. What
08:27we need
08:28in order to run this properly is we need to specify the x and the y axis in order for
08:33this scatter plot
08:33to work. So let's go here and we'll say x is equal to, and we can take any of our
08:41columns that we have
08:41up here. So we'll say x is equal to texture rating. And then y is equal to, and we'll do
08:52overall rating.
08:54Now, when we run this, it should work properly. Let's go ahead and take a look. Now, if we go
08:58in
08:58here and we do shift tab, we can also see some other things that we can specify. So let's go
09:05right
09:05down here. So we have our x and we have our y, and those are the ones that we just
09:08did. We can also
09:10pass through an s, which is going to tell us or change the size of the actual dots right here
09:16in our scatter plot. Then we can also do a c, which is the color of each point. Let's start
09:22with the s.
09:23Let's say s is equal to, and let's just do 100. We'll see what that looks like. So we have
09:29a much
09:29larger number. Let's do 500 and see what that looks like. So we can make these much larger on our
09:35visualization, depending on what you're looking for. We can also look at the color.
09:40Let's put comma C. So for color, we can say color is equal to, and let's do yellow.
09:47Let's see if this works. So now we've changed it to yellow. That looks absolutely terrible,
09:51but it does work. Now let's move on to the histogram. Histogram is always a good one.
09:57It's very similar to something like a bar chart, but what's great about a histogram is you can specify
10:02the bins. So let's go ahead and say df.plot.hist. Then we'll do an open parentheses and let's go
10:12ahead
10:12and hit shift tab in here. Take a look at this one as well. So some of our parameters are
10:19the actual
10:19columns of the data frames that we want to pull in. We can choose the bins and they have a
10:24default of 10
10:25in here. And so let's take a look at how this works. So we'll just run this as it is.
10:31So this
10:31is by default, what this histogram is going to look like. Let's go ahead and specify our bins.
10:38We'll just say it was 10 by default. Let's just do 20. See what that looks like. There are smaller
10:43columns right off the bat. And remember, histograms are really good for showing distribution of
10:49variables. You know, that's really what a histogram is for. But of course, since these are
10:53completely random numbers, this histogram isn't going to make any sense at all. But you can at
10:58least kind of see visually how it works. And if I didn't mention it before, which I should have,
11:02the bins represent how many kind of tick marks are down here. So if we just do one, it's only
11:09going
11:09to be one very large, you know, histogram. We could even go further down from 10 and do five. So
11:18now
11:18there's only one, two, three, four, five. So the distribution gets smaller and things get more
11:23compact. As you spread it out, again, like we did 100, it's going to spread it out a lot. And
11:31this is
11:31what it shows. You know, it's showing the distribution of those bins across however many you want. So the
11:3810 by default, you know, it usually is pretty good for a lot of different things. Now let's go down
11:42here
11:43and look at the box plot. And the box plot is a pretty interesting one. Let's go ahead and visualize
11:48it really quickly. And then I'll kind of explain how this one works. Let's do df.boxplot. Let's run
11:54this. And really what we're looking at is some different markers within our data. This line right
12:00here is the minimum value within that column. We also have the bottom of the box, which is the 25th
12:05percentile of all the values within just this column. This is 50%. Then we have 75%. And then
12:13up here, we have our maximum value. So I can take a glance at this and see that we have
12:17a low minimum, a high maximum, and it definitely skews towards the lower range. Whereas if I
12:23look over here, we have a lower minimum and a higher maximum. And you can see that this
12:28medium point is at 0.6 versus 0.4 over here. So this skews a lot higher. Now let's go
12:33down
12:34here and take a look at an area plot. We'll do df.plot.area. And let's just run this. This
12:42is what
12:42we're going to get by default. Now, something I wanted to show you earlier, I just haven't
12:46gotten around to. I want to show you something called figure size or fig size. So for this,
12:51it's, you know, it's just looks small, looks a little bit crammed. So let's say we want
12:54to increase the size of this and we'll say fig size, oops, fig size is equal to, and let's
13:01just do a parenthesis and say 10,5. That should be pretty large. This is going to make it a
13:07lot
13:07larger. Just something I wanted to throw in there. I look up these area charts as pretty
13:11similar to like a line chart. If we went and compared those, it'd be pretty similar, but
13:16they're different visually. And you know, you absolutely can use these for different types
13:20of visualizations, but I don't use this one a lot. If I'm being honest, that's why it's
13:25kind of towards the end of the video, but you definitely can do it. Let's go on to our very
13:29last one of the video. That's going to be the beautiful pie chart. Let's say DF dot plot
13:35dot pie. We'll do an open parentheses and let's run it. We're going to get this error. That's
13:41because we need to specify what column we're working with here. So let's just say the Y
13:46and that's what we need. Let me open this up for us. Right here, we have our Y and this
13:53is our, our label or our column that we're going to plot. That's really all we need. So we
13:57can just say Y is equal to labor rating. Oops, labor rating. Let's run this. Now we get this
14:06visualization right here. Make this one a little bit bigger. Big size is equal to 10, 6. Now
14:17it's a little bit bigger. It definitely depends. So this legend is going to auto populate. You
14:22know, you can make this as big as you want. And obviously it's going to look a little bit
14:26better if you do it larger and these colors auto populate. Now you can customize these colors.
14:30Although I found these ones to be just when you have a lot of them, it's harder to customize
14:34them as easily, but you know, definitely look into it. These are things that everything in
14:39here is almost something that you can customize in some way. Although it does get a little bit
14:44tricky. You definitely have to do some research and some Googling around just to kind of figure
14:48out how to do those things. Now, one last thing that I wanted to show and something, you know,
14:53I could have probably done at the beginning, is you can actually change what visual this
14:58is. And we can do that pretty easily. Within Matplotlib, there are different styles. And
15:04so let's go right here. Let's add a new row, a new cell. And we'll say print, we'll do
15:11PLT. So that's that Matplotlib right here. We'll do PLT.style.available. And what this is
15:19going to do, whoops, what this is going to do is show us all these different types of stylings
15:25that you can do to kind of change up this visualization. And then once we find the one that
15:30we like, we'll just do PLT.style.use. And then in the parentheses, we'll just specify which
15:38one we want. Now, there's all these Seabourn ones. And Seabourn is a really great, really
15:44great library. Let's try Seabourn Deep. I haven't tried this one at all. Let's go ahead and try
15:49this. It just changes some of the colors, some of the visuals. We can try something like 538.
15:57Let's try this. That looks quite a bit different. And let's try something like
16:04Classic. I don't know what this one looks like. Let's just try it.
16:08So you can try out all these different styles, find one that you like, find one that you think
16:12looks really nice, and you can run with it through all your visualizations. So this has been our video
16:17on visualizing data in pandas. I think it's a really good introduction on how you can visualize
16:21data within Python. And in future videos, we'll look at Matplotlib and Seabourn, which are some
16:26really great libraries for visualizing data, which I use a lot. So I hope that you enjoyed this video.
16:31If you did, be sure to check out all my other videos on Python and pandas, and I will see
16:36you in the next video.
16:38Bye.
16:41Bye.
16:46Bye.
16:47Bye.
16:48Bye.
16:49Bye.
16:49Bye.
16:49Bye.

Recommended