1"""
2============
3Scatter Hist
4============
5
6"""
7import numpy as np
8import matplotlib.pyplot as plt
9from matplotlib.ticker import NullFormatter
10
11# Fixing random state for reproducibility
12np.random.seed(19680801)
13
14
15# the random data
16x = np.random.randn(1000)
17y = np.random.randn(1000)
18
19nullfmt = NullFormatter()         # no labels
20
21# definitions for the axes
22left, width = 0.1, 0.65
23bottom, height = 0.1, 0.65
24bottom_h = left_h = left + width + 0.02
25
26rect_scatter = [left, bottom, width, height]
27rect_histx = [left, bottom_h, width, 0.2]
28rect_histy = [left_h, bottom, 0.2, height]
29
30# start with a rectangular Figure
31plt.figure(1, figsize=(8, 8))
32
33axScatter = plt.axes(rect_scatter)
34axHistx = plt.axes(rect_histx)
35axHisty = plt.axes(rect_histy)
36
37# no labels
38axHistx.xaxis.set_major_formatter(nullfmt)
39axHisty.yaxis.set_major_formatter(nullfmt)
40
41# the scatter plot:
42axScatter.scatter(x, y)
43
44# now determine nice limits by hand:
45binwidth = 0.25
46xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))
47lim = (int(xymax/binwidth) + 1) * binwidth
48
49axScatter.set_xlim((-lim, lim))
50axScatter.set_ylim((-lim, lim))
51
52bins = np.arange(-lim, lim + binwidth, binwidth)
53axHistx.hist(x, bins=bins)
54axHisty.hist(y, bins=bins, orientation='horizontal')
55
56axHistx.set_xlim(axScatter.get_xlim())
57axHisty.set_ylim(axScatter.get_ylim())
58
59plt.show()
60