Hexbin
A hexbin plot is useful to represent the relationship of 2 numerical variables when you have a lot of data points. Without overlapping of the points, the plotting window is split into several hexbins. The color of each hexbin denotes the number of points in it. This can be easily done using the hexbin() function of matplotlib. Note that you can change the size of the bins using the gridsize argument. The parameters of hexbin() function used in the example are:
- x, y: The data positions
- gridsize: the number of hexagons in the x-direction and the y-direction
Libraries & Dataset
Let's start by import a few libraries and create a dataset:
| -0.802614 | -0.745659 |
| -0.861153 | 0.013962 |
| -1.847799 | 1.002161 |
| 0.263749 | 0.264361 |
| 2.192508 | 0.464232 |
Make the plot
Making an hexbin plot is quite straightforward with the hexbin() function from matplotlib
Color
It is possible to change the color palette applied to the plot with the cmap argument. Read this page to learn more about color palette with matplotlib and pick up the right one.
Colorbar and legend
Note that you can easily add a color bar beside the plot using colorbar() function.
Going further
You might be interested:
- how to create a contour plot, which is a smoothed version of the 2d histogram
- how to combine a 2d density/histogram plot with marginal plot