Note
Click here to download the full example code
03. mpl.hist2d
Make a 2D histogram plot.
See: https://jakevdp.github.io/PythonDataScienceHandbook/04.05-histograms-and-binnings.html
10 # Libraries
11 import numpy as np
12 import matplotlib.pyplot as plt
13
14 # Specific library
15 from sklearn.datasets import make_blobs
16 from sklearn.datasets import make_classification
17 from scipy.stats import gaussian_kde
18
19 # ----------------------------
20 # Load data
21 # ----------------------------
22 # Create data
23 mean = [0, 0]
24 cov = [[1, 1], [1, 2]]
25 x, y = np.random.multivariate_normal(mean, cov, 10000).T
26
27 # Create data
28 data, t = make_classification(n_samples=8000, n_features=2,
29 n_informative=2, n_redundant=0, n_repeated=0, n_classes=2,
30 n_clusters_per_class=1, weights=None, flip_y=0.00,
31 class_sep=1.0, hypercube=True, shift=0.0, scale=1.0,
32 shuffle=True, random_state=32)
33 x, y = data[:,0], data[:,1]
34
35
36 # ----------------------------
37 # Visualize
38 # ----------------------------
39 # Plot hist
40 f1 = plt.hist2d(x, y, bins=30, cmap='Reds')
41 cb = plt.colorbar()
42 cb.set_label('counts in bin')
43 plt.title('Counts (square bin)')
44
45 # Plot hex
46 plt.figure()
47 f2 = plt.hexbin(x, y, gridsize=30, cmap='Reds')
48 cb = plt.colorbar()
49 cb.set_label('counts in bin')
50 plt.title('Counts (hexagonal bin)')
51
52 # Plot density
53 data = np.vstack([x, y])
54 kde = gaussian_kde(data)
55
56 # Parameters
57 xmin, xmax = min(x), max(x)
58 ymin, ymax = min(y), max(y)
59
60 # evaluate on a regular grid
61 xgrid = np.linspace(xmin, xmax, 100)
62 ygrid = np.linspace(ymin, ymax, 100)
63 Xgrid, Ygrid = np.meshgrid(xgrid, ygrid)
64 Z = kde.evaluate(np.vstack([
65 Xgrid.ravel(),
66 Ygrid.ravel()
67 ]))
68
69 # Plot the result as an image
70 plt.figure()
71 plt.imshow(Z.reshape(Xgrid.shape),
72 origin='lower', aspect='auto',
73 extent=[xmin, xmax, ymin, ymax],
74 cmap='Reds')
75 cb = plt.colorbar()
76 cb.set_label("density")
77 plt.title("KDE estimation")
78
79 # Show
80 plt.show()
Total running time of the script: ( 0 minutes 2.034 seconds)