Skip to main content
Search form
  • 00:01

    2D Histograms

  • 00:04

    GARY WHITE: In this tutorial, we'regoing to be looking at using 2D histograms, so it's basicallyjust the next step after looking at the previous tutorialon histograms and how we can include multiple histogramsin a single plot.We can also create two dimensional histograms.So I imported matplotlib and numpy, as standard,and now we're going to create our values.

  • 00:27

    GARY WHITE [continued]: So we're going to set mean = (0, 0)and we're going to set the covariance equal to a list.And the first item in the list is going to be rate of 1-1,and the second item is going to be 1-2.

  • 00:47

    GARY WHITE [continued]: So this is the mean and covariancethat we're going to use when we're creating our 2Dhistogram, and the values that we'regoing to have are x and y.So we specify x, y = np.random.multivariate_normal.And in the multivariate normal, you

  • 01:08

    GARY WHITE [continued]: can see we're specifying the mean, covariance, and size.So we specify the mean, the covariance, and we want 1,000.And then we're going to transpose this.And then we're going to create our 2D histogram.So we're using plt.hist2d.

  • 01:33

    GARY WHITE [continued]: And in the 2D histogram, you need to specify the x and y.Then, like in the previous histogram tutorial,you can specify the number of bins.In this case, we're going to set it equal to 30.We're going to set our column up equal to blues,so cmap = "blues".

  • 01:56

    GARY WHITE [continued]: So this is just a range of different blues colorsthat we can use when creating the plot.And then we can set up our colorbarusing cb = plt.colorbar.And we can also set the label.

  • 02:17

    GARY WHITE [continued]: So cb.set_label-- so set underscore label--and we specify that to be equal to counts in bin.Then we can use, just to show what we've created.

  • 02:39

    GARY WHITE [continued]: And if we run that, now you can see this two dimensionalhistogram.And we have the counts in the binfor each of the different points in the axis.So you can see at around the central pointhere, which is what we would expect when they both havemeans of zero and zero.

  • 02:59

    GARY WHITE [continued]: So the covariance is slightly different for the x and yvalues, which is why it's sort of diagonalas opposed to being centered, but wecan see, as we would expect, that the largestnumber of points are around the 0, 0 mark.So we can also create a slightly different blushand it's using a hexbin.

  • 03:21

    GARY WHITE [continued]: So if we do plt.hexbin, we can specify the x,y, the grid size, and in this case,we're going to set the grid size equal to 30and the cmap equal to blues.So the color map equals to blues again.

  • 03:43

    GARY WHITE [continued]: And then we can specify the colorbar.So the cb = plt.colorbar, and thenwe specify label = "counts in bin".

  • 04:09

    GARY WHITE [continued]: And once we have created that, we can do the hexbin is very similar to the 2D histogram,but it's just using hexagonal shapes.So you can see here where these are each hexagonal shapes as

  • 04:29

    GARY WHITE [continued]: opposed to the squares that are created in the 2D histogram,so it just gives you a slightly different variation on the 2Dhistogram, and makes it look slightly cleaner.It can look a bit pixelated or blurred if you're using this,but the hexagonal shapes makes it looks a bit more cleaner.

  • 04:51

    GARY WHITE [continued]: So you can choose either one of these.They essentially do the same thing.So you can see that, again, centered around 0,0is where we're getting most of the counts in the binfor both of these plots, they just look slightly different.So that's how you do 2D histograms in Matplotlib.

Video Info

Series Name: Data Visualization With Matplotlib 3.x and Python

Episode: 20

Publisher: Gary White

Publication Year: 2020

Video Type:Tutorial

Methods: Data visualization, Python, Coding

Keywords: coding; data visualisation; graphical presentation of data; histogram

Segment Info

Segment Num.: 1

Persons Discussed:

Events Discussed:



Gary White explains how to create and set options for 2D histograms in Matplotlib.

Looks like you do not have access to this content.

2D Histograms

Gary White explains how to create and set options for 2D histograms in Matplotlib.

Copy and paste the following HTML into your website