Locked lesson.
About this lesson
In this video, we explain what a hexplot is and how to create your own.
Exercise files
Download this lesson’s related exercise files.
Plotting HexPlots.docx57 KB Plotting HexPlots - Solution.docx
55.6 KB
Quick reference
Plotting HexPlots
A Hex plot shows a histogram of the number of occurrences of the observations of your data.
When to use
Use these to visualize a histogram of the number of occurrences of the observations of your data.
Instructions
The first method to create a hex plot:
my_df.plot(kind='hexbin', x='Mon', y='Tues')
To change the size of your hex plot, set the grid size:
my_df.plot(kind='hexbin', x='Mon', y='Tues', gridsize=10)
The second method for creating a hex plot:
my_df.plot.hexbin(x='Mon', y="Tues", gridsize=10)
Hints & tips
- my_df.plot(kind='hexbin', x='Mon', y='Tues', gridsize=10)
- my_df.plot.hexbin(x='Mon', y="Tues", gridsize=10)
- 00:05 Okay, in the last video we looked at box plots,
- 00:07 in this video we want to look at hex plots.
- 00:09 Hex plots are pretty easy.
- 00:10 We could just build one right out here.
- 00:12 Let's go my_df.plot and then kind=hexbin.
- 00:20 Now we also need to designate the x and y axis.
- 00:23 So if you remember our data, we have Monday, Tuesday, Wednesday as our columns.
- 00:29 So we need to designate two of these different columns that we want to run
- 00:32 a hex plot for.
- 00:33 So it doesn't really matter which ones, we could just pick any of them.
- 00:36 So let's go, let's say we want x equals
- 00:42 Monday and y to equal Thursday.
- 00:48 If we run this, we get this looks kind like a scatter plot, right?
- 00:52 But it's not.
- 00:53 So, actually let's come back up to our data and beef this up a little bit.
- 00:57 We've only got 50 points.
- 00:59 Let's make this 1000 really, so we can really see this.
- 01:03 So Shift Enter to run this, Shift Enter to run the data again and
- 01:06 let's come down here.
- 01:08 And we can run this again and it looks a little bit better now with more points,
- 01:12 but it's still not exactly what we want.
- 01:14 We want to make this bigger and so we could change the grid size.
- 01:17 So let's go grid size equals, say 10.
- 01:22 And when we do that, boom,
- 01:24 now we start to see these cool hexagons and showing the hexagonal bins, right?
- 01:29 We could change it if we wanted slightly smaller, we could go 25 and you can
- 01:34 see the darker areas just means there are more points in those areas from our data,
- 01:39 and where it's lighter there those are less points of our data, right?
- 01:43 So pretty cool.
- 01:45 Now we can change the color of this.
- 01:47 So cmap equals, and we can change this to anything we like here.
- 01:52 So let's just grab one, let's try to say cool warm maybe,
- 01:56 it doesn't really matter, for example purposes.
- 01:59 That's kind of cool looking.
- 02:00 Let's try one more just for
- 02:02 fun, I like going through these spectral spectral, what is that?
- 02:07 Okay, kind of cool and pretty neat.
- 02:10 So we can change this to Monday and Wednesday for instance,
- 02:13 data changes a little bit and that's cool.
- 02:16 So like other things here,
- 02:19 we could call my_df.plot.hexbin, just run like this.
- 02:25 We just run this we'll get an error because we didn't pass in our xs and
- 02:28 ys, right?
- 02:29 And let's just grab all of this and we can copy and paste this in and
- 02:34 we get the exact same thing.
- 02:37 And again, if you want to come over here and
- 02:40 let's just delete all this and Shift Tab.
- 02:44 We can see it takes the argument x and y and grid size, and
- 02:48 again if you are not quite sure what this is, you can read the dot string.
- 02:54 You can generate a hexagon binning plot of x versus y. If c is none, the default,
- 03:00 this is the histogram of the number of occurrences of the observations at
- 03:05 x of whatever and y of whatever.
- 03:07 If c is specified, that specifies that the values at a given coordinate or whatever.
- 03:15 These values are accumulated for each hexagonal bin and
- 03:18 then reduced according to reduce C function.
- 03:20 So more advanced topic, we're not going to get into that.
- 03:23 So basically you just want your x and your y, and your grid size and that's it.
- 03:29 These documentations are great.
- 03:30 I mean, if you're especially when you're first starting out,
- 03:33 you're going to forget what each of these things are.
- 03:36 Maybe you're not quite sure what a scatter plot is or whatever, just Shift Tab inside
- 03:41 of these parentheses and you'll get that data that you can read.
- 03:46 Now, if we come up here and do it inside of this, we're not going to get the same
- 03:50 thing because this function is just the plot function.
- 03:53 So we're going to get the help data for
- 03:54 the plot function, not for instance the hex bin function.
- 03:58 And we can do that just by let me just Shift Tab this real quick.
- 04:03 So like I said, we're getting this stuff for plotting in general, right?
- 04:09 And some of this is useful, it shows the grid and the title and the legend and
- 04:12 the style and different things.
- 04:13 If you forget what those are, font size, color,
- 04:16 map we've been using that one right, and some other things, but very cool.
- 04:21 Let me go ahead and paste this back in.
- 04:25 So those are hex plots, in the next video we're going to look at density plots.
Lesson notes are only available for subscribers.