-
Notifications
You must be signed in to change notification settings - Fork 0
/
contour_example.py
170 lines (99 loc) · 3.14 KB
/
contour_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# coding: utf-8
# In[1]:
import numpy as np
from __future__ import print_function
import matplotlib as mpl
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
# # Generate a mock dataset
# In[62]:
# first generate the centers and standard deviations of 3d gaussians
seed = 9
np.random.seed(seed)
ngauss = 25 # using 25 gaussians to make something seemingly complicated
centers = np.random.normal(scale=1., size=(ngauss, 3))
scales = np.random.uniform(low=0.2, high=0.7, size=ngauss)
# In[63]:
nrows = 3e5
nrows_per_gauss = int(nrows) // ngauss
samples = []
for i in np.arange(ngauss):
samples.append(np.random.normal(loc=centers[i], scale=scales[i], size=(nrows_per_gauss, 3)))
samples = np.vstack(samples)
samples[:, 2] += 10
# # Make histogram
# In[64]:
nbins = 100
# define the bins edges
x_edges = np.linspace(-3.5, 3.5, nbins+1)
y_edges = np.linspace(-3.5, 3.5, nbins+1)
bins = (x_edges, y_edges)
# In[65]:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist2d(samples[:, 0], samples[:, 1], bins=bins)
ax.set_title("Histogram plot")
ax.set_xlabel("X")
ax.set_ylabel("Y")
# # Make simple contour plot
# In[66]:
nbins = 100
# define the bins edges
x_edges = np.linspace(-3.5, 3.5, nbins+1)
y_edges = np.linspace(-3.5, 3.5, nbins+1)
bins = (x_edges, y_edges)
# calculate bin mid points as centers
xcens = x_edges[:-1] + np.diff(x_edges) / 2.
ycens = y_edges[:-1] + np.diff(y_edges) / 2.
xx, yy = np.meshgrid(xcens, ycens)
# In[67]:
fig = plt.figure()
ax = fig.add_subplot(111)
counts = np.histogram2d(samples[:, 0], samples[:, 1], bins=bins)[0]
ax.hist(counts.flatten(), bins=100)
ax.set_xlabel("Z")
ax.set_ylabel("#")
# In[68]:
fig = plt.figure()
ax = fig.add_subplot(111)
counts = np.histogram2d(samples[:, 0], samples[:, 1], bins=bins)[0]
contour_levels = np.linspace(0, 400, 10)
ax.contour(xx, yy, counts, contour_levels)
ax.set_title("Contour plot")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.grid(ls=":")
# # Contour plot of f(Z)
# In[82]:
nbins = 40
# define the bins edges
x_edges = np.linspace(-3.5, 3.5, nbins+1)
y_edges = np.linspace(-3.5, 3.5, nbins+1)
bins = (x_edges, y_edges)
# calculate bin mid points as centers
xcens = x_edges[:-1] + np.diff(x_edges) / 2.
ycens = y_edges[:-1] + np.diff(y_edges) / 2.
xx, yy = np.meshgrid(xcens, ycens)
# In[83]:
zz = np.zeros(shape=xx.shape)
for i in np.arange(nbins):
for j in np.arange(nbins):
index = ((samples[:, 0] > x_edges[i]) & (samples[:, 0] < x_edges[i + 1]) &
(samples[:, 1] > y_edges[j]) & (samples[:, 1] < y_edges[j + 1]))
# print()
zz[i, j] = samples[index, 2].sum()
# In[121]:
fig = plt.figure(figsize=(12, 12))
ax = fig.add_subplot(111)
ax.hist2d(samples[:, 0], samples[:, 1], bins=bins, cmap=plt.cm.terrain_r)
contour_levels = np.arange(0, 16000, 2000)
cs = ax.contour(xx.T, yy.T, zz, contour_levels, colors='k', fmt="s")
fmt = {}
strs = ["{:d}".format(int(level)) for level in contour_levels[1:]]
for l, s in zip(cs.levels, strs):
fmt[l] = s
ax.clabel(cs, cs.levels, fmt=fmt, fontsize=8, inline=True)
ax.set_title("Contour plot")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.grid(ls=":")